table-checkbox.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <view class="uni-table-checkbox" @click="selected">
  3. <view v-if="!indeterminate" class="checkbox__inner" :class="{'is-checked':isChecked,'is-disable':isDisabled}">
  4. <view class="checkbox__inner-icon"></view>
  5. </view>
  6. <view v-else class="checkbox__inner checkbox--indeterminate">
  7. <view class="checkbox__inner-icon"></view>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. name: 'TableCheckbox',
  14. props: {
  15. indeterminate: {
  16. type: Boolean,
  17. default: false
  18. },
  19. checked: {
  20. type: [Boolean,String],
  21. default: false
  22. },
  23. disabled: {
  24. type: Boolean,
  25. default: false
  26. },
  27. index: {
  28. type: Number,
  29. default: -1
  30. },
  31. cellData: {
  32. type: Object,
  33. default () {
  34. return {}
  35. }
  36. }
  37. },
  38. watch:{
  39. checked(newVal){
  40. if(typeof this.checked === 'boolean'){
  41. this.isChecked = newVal
  42. }else{
  43. this.isChecked = true
  44. }
  45. },
  46. indeterminate(newVal){
  47. this.isIndeterminate = newVal
  48. }
  49. },
  50. data() {
  51. return {
  52. isChecked: false,
  53. isDisabled: false,
  54. isIndeterminate:false
  55. }
  56. },
  57. created() {
  58. if(typeof this.checked === 'boolean'){
  59. this.isChecked = this.checked
  60. }
  61. this.isDisabled = this.disabled
  62. },
  63. methods: {
  64. selected() {
  65. if (this.isDisabled) return
  66. this.isIndeterminate = false
  67. this.isChecked = !this.isChecked
  68. this.$emit('checkboxSelected', {
  69. checked: this.isChecked,
  70. data: this.cellData
  71. })
  72. }
  73. }
  74. }
  75. </script>
  76. <style lang="scss">
  77. $checked-color: #007aff;
  78. $border-color: #DCDFE6;
  79. $disable:0.4;
  80. .uni-table-checkbox {
  81. display: flex;
  82. flex-direction: row;
  83. align-items: center;
  84. justify-content: center;
  85. position: relative;
  86. margin: 5px 0;
  87. cursor: pointer;
  88. // 多选样式
  89. .checkbox__inner {
  90. /* #ifndef APP-NVUE */
  91. flex-shrink: 0;
  92. box-sizing: border-box;
  93. /* #endif */
  94. position: relative;
  95. width: 16px;
  96. height: 16px;
  97. border: 1px solid $border-color;
  98. border-radius: 2px;
  99. background-color: #fff;
  100. z-index: 1;
  101. .checkbox__inner-icon {
  102. position: absolute;
  103. /* #ifdef APP-NVUE */
  104. top: 2px;
  105. /* #endif */
  106. /* #ifndef APP-NVUE */
  107. top: 2px;
  108. /* #endif */
  109. left: 5px;
  110. height: 7px;
  111. width: 3px;
  112. border: 1px solid #fff;
  113. border-left: 0;
  114. border-top: 0;
  115. opacity: 0;
  116. transform-origin: center;
  117. transform: rotate(45deg);
  118. box-sizing: content-box;
  119. }
  120. &.checkbox--indeterminate {
  121. border-color: $checked-color;
  122. background-color: $checked-color;
  123. .checkbox__inner-icon {
  124. position: absolute;
  125. opacity: 1;
  126. transform: rotate(0deg);
  127. height: 2px;
  128. top: 0;
  129. bottom: 0;
  130. margin: auto;
  131. left: 0px;
  132. right: 0px;
  133. bottom: 0;
  134. width: auto;
  135. border: none;
  136. border-radius: 2px;
  137. transform: scale(0.5);
  138. background-color: #fff;
  139. }
  140. }
  141. &:hover{
  142. border-color: $checked-color;
  143. }
  144. // 禁用
  145. &.is-disable {
  146. /* #ifdef H5 */
  147. cursor: not-allowed;
  148. /* #endif */
  149. background-color: #F2F6FC;
  150. border-color: $border-color;
  151. }
  152. // 选中
  153. &.is-checked {
  154. border-color: $checked-color;
  155. background-color: $checked-color;
  156. .checkbox__inner-icon {
  157. opacity: 1;
  158. transform: rotate(45deg);
  159. }
  160. // 选中禁用
  161. &.is-disable {
  162. opacity: $disable;
  163. }
  164. }
  165. }
  166. }
  167. </style>