table-checkbox.vue 3.4 KB

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