table-checkbox.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 {} as UTSJSONObject
  36. }
  37. }
  38. },
  39. watch:{
  40. checked(newVal){
  41. if(typeof this.checked === 'boolean'){
  42. this.isChecked = newVal as boolean
  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. $uni-primary: #007aff !default;
  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. /* #ifdef H5 */
  89. cursor: pointer;
  90. /* #endif */
  91. // 多选样式
  92. .checkbox__inner {
  93. /* #ifndef APP-NVUE */
  94. flex-shrink: 0;
  95. box-sizing: border-box;
  96. /* #endif */
  97. position: relative;
  98. width: 16px;
  99. height: 16px;
  100. border: 1px solid $border-color;
  101. border-radius: 2px;
  102. background-color: #fff;
  103. z-index: 1;
  104. .checkbox__inner-icon {
  105. position: absolute;
  106. /* #ifdef APP-NVUE */
  107. top: 2px;
  108. /* #endif */
  109. /* #ifndef APP-NVUE */
  110. top: 2px;
  111. /* #endif */
  112. left: 5px;
  113. height: 7px;
  114. width: 3px;
  115. border: 1px solid #fff;
  116. border-left: 0;
  117. border-top: 0;
  118. opacity: 0;
  119. transform-origin: center;
  120. transform: rotate(45deg);
  121. box-sizing: content-box;
  122. }
  123. &.checkbox--indeterminate {
  124. border-color: $uni-primary;
  125. background-color: $uni-primary;
  126. .checkbox__inner-icon {
  127. position: absolute;
  128. opacity: 1;
  129. transform: rotate(0deg);
  130. height: 2px;
  131. top: 0;
  132. bottom: 0;
  133. margin: auto;
  134. left: 0px;
  135. right: 0px;
  136. bottom: 0;
  137. width: auto;
  138. border: none;
  139. border-radius: 2px;
  140. transform: scale(0.5);
  141. background-color: #fff;
  142. }
  143. }
  144. &:hover{
  145. border-color: $uni-primary;
  146. }
  147. // 禁用
  148. &.is-disable {
  149. /* #ifdef H5 */
  150. cursor: not-allowed;
  151. /* #endif */
  152. background-color: #F2F6FC;
  153. border-color: $border-color;
  154. }
  155. // 选中
  156. &.is-checked {
  157. border-color: $uni-primary;
  158. background-color: $uni-primary;
  159. .checkbox__inner-icon {
  160. opacity: 1;
  161. transform: rotate(45deg);
  162. }
  163. // 选中禁用
  164. &.is-disable {
  165. opacity: $disable;
  166. }
  167. }
  168. }
  169. }
  170. </style>