table-checkbox.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. 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. this.$emit('checkboxSelected', {
  70. checked: this.isChecked,
  71. data: this.cellData
  72. })
  73. }
  74. }
  75. }
  76. </script>
  77. <style lang="scss">
  78. $checked-color: #007aff;
  79. $border-color: #DCDFE6;
  80. $disable:0.4;
  81. .uni-table-checkbox {
  82. display: flex;
  83. flex-direction: row;
  84. align-items: center;
  85. justify-content: center;
  86. position: relative;
  87. margin: 5px 0;
  88. cursor: pointer;
  89. // 多选样式
  90. .checkbox__inner {
  91. /* #ifndef APP-NVUE */
  92. flex-shrink: 0;
  93. box-sizing: border-box;
  94. /* #endif */
  95. position: relative;
  96. width: 16px;
  97. height: 16px;
  98. border: 1px solid $border-color;
  99. border-radius: 2px;
  100. background-color: #fff;
  101. z-index: 1;
  102. .checkbox__inner-icon {
  103. position: absolute;
  104. /* #ifdef APP-NVUE */
  105. top: 2px;
  106. /* #endif */
  107. /* #ifndef APP-NVUE */
  108. top: 2px;
  109. /* #endif */
  110. left: 5px;
  111. height: 7px;
  112. width: 3px;
  113. border: 1px solid #fff;
  114. border-left: 0;
  115. border-top: 0;
  116. opacity: 0;
  117. transform-origin: center;
  118. transform: rotate(45deg);
  119. box-sizing: content-box;
  120. }
  121. &.checkbox--indeterminate {
  122. border-color: $checked-color;
  123. background-color: $checked-color;
  124. .checkbox__inner-icon {
  125. position: absolute;
  126. opacity: 1;
  127. transform: rotate(0deg);
  128. height: 2px;
  129. top: 0;
  130. bottom: 0;
  131. margin: auto;
  132. left: 0px;
  133. right: 0px;
  134. bottom: 0;
  135. width: auto;
  136. border: none;
  137. border-radius: 2px;
  138. transform: scale(0.5);
  139. background-color: #fff;
  140. }
  141. }
  142. &:hover{
  143. border-color: $checked-color;
  144. }
  145. // 禁用
  146. &.is-disable {
  147. /* #ifdef H5 */
  148. cursor: not-allowed;
  149. /* #endif */
  150. background-color: #F2F6FC;
  151. border-color: $border-color;
  152. }
  153. // 选中
  154. &.is-checked {
  155. border-color: $checked-color;
  156. background-color: $checked-color;
  157. .checkbox__inner-icon {
  158. opacity: 1;
  159. transform: rotate(45deg);
  160. }
  161. // 选中禁用
  162. &.is-disable {
  163. opacity: $disable;
  164. }
  165. }
  166. }
  167. }
  168. </style>