uni-tr.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <template>
  2. <!-- #ifdef H5 -->
  3. <tr class="uni-table-tr">
  4. <th v-if="selection === 'selection' && ishead" class="checkbox" :class="{ 'tr-table--border': border }">
  5. <table-checkbox :checked="checked" :indeterminate="indeterminate" :disabled="disabled" @checkboxSelected="checkboxSelected"></table-checkbox>
  6. </th>
  7. <slot></slot>
  8. <!-- <uni-th class="th-fixed">123</uni-th> -->
  9. </tr>
  10. <!-- #endif -->
  11. <!-- #ifndef H5 -->
  12. <view class="uni-table-tr">
  13. <view v-if="selection === 'selection' " class="checkbox" :class="{ 'tr-table--border': border }">
  14. <table-checkbox :checked="checked" :indeterminate="indeterminate" :disabled="disabled" @checkboxSelected="checkboxSelected"></table-checkbox>
  15. </view>
  16. <slot></slot>
  17. </view>
  18. <!-- #endif -->
  19. </template>
  20. <script>
  21. import tableCheckbox from './table-checkbox.vue'
  22. /**
  23. * Tr 表格行组件
  24. * @description 表格行组件 仅包含 th,td 组件
  25. * @tutorial https://ext.dcloud.net.cn/plugin?id=
  26. */
  27. export default {
  28. name: 'uniTr',
  29. components: { tableCheckbox },
  30. props: {
  31. disabled: {
  32. type: Boolean,
  33. default: false
  34. },
  35. keyValue: {
  36. type: [String, Number],
  37. default: ''
  38. }
  39. },
  40. options: {
  41. virtualHost: true
  42. },
  43. data() {
  44. return {
  45. value: false,
  46. border: false,
  47. selection: false,
  48. widthThArr: [],
  49. ishead: true,
  50. checked: false,
  51. indeterminate:false
  52. }
  53. },
  54. created() {
  55. this.root = this.getTable()
  56. this.head = this.getTable('uniThead')
  57. if (this.head) {
  58. this.ishead = false
  59. this.head.init(this)
  60. }
  61. this.border = this.root.border
  62. this.selection = this.root.type
  63. this.root.trChildren.push(this)
  64. const rowData = this.root.data.find(v => v[this.root.rowKey] === this.keyValue)
  65. if(rowData){
  66. this.rowData = rowData
  67. }
  68. this.root.isNodata()
  69. },
  70. mounted() {
  71. if (this.widthThArr.length > 0) {
  72. const selectionWidth = this.selection === 'selection' ? 50 : 0
  73. this.root.minWidth = this.widthThArr.reduce((a, b) => Number(a) + Number(b)) + selectionWidth
  74. }
  75. },
  76. // #ifndef VUE3
  77. destroyed() {
  78. const index = this.root.trChildren.findIndex(i => i === this)
  79. this.root.trChildren.splice(index, 1)
  80. this.root.isNodata()
  81. },
  82. // #endif
  83. // #ifdef VUE3
  84. unmounted() {
  85. const index = this.root.trChildren.findIndex(i => i === this)
  86. this.root.trChildren.splice(index, 1)
  87. this.root.isNodata()
  88. },
  89. // #endif
  90. methods: {
  91. minWidthUpdate(width) {
  92. this.widthThArr.push(width)
  93. },
  94. // 选中
  95. checkboxSelected(e) {
  96. let rootData = this.root.data.find(v => v[this.root.rowKey] === this.keyValue)
  97. this.checked = e.checked
  98. this.root.check(rootData||this, e.checked,rootData? this.keyValue:null)
  99. },
  100. change(e) {
  101. this.root.trChildren.forEach(item => {
  102. if (item === this) {
  103. this.root.check(this, e.detail.value.length > 0 ? true : false)
  104. }
  105. })
  106. },
  107. /**
  108. * 获取父元素实例
  109. */
  110. getTable(name = 'uniTable') {
  111. let parent = this.$parent
  112. let parentName = parent.$options.name
  113. while (parentName !== name) {
  114. parent = parent.$parent
  115. if (!parent) return false
  116. parentName = parent.$options.name
  117. }
  118. return parent
  119. }
  120. }
  121. }
  122. </script>
  123. <style lang="scss">
  124. $border-color: #ebeef5;
  125. .uni-table-tr {
  126. /* #ifndef APP-NVUE */
  127. display: table-row;
  128. transition: all 0.3s;
  129. box-sizing: border-box;
  130. /* #endif */
  131. }
  132. .checkbox {
  133. padding: 0 8px;
  134. width: 26px;
  135. padding-left: 12px;
  136. /* #ifndef APP-NVUE */
  137. display: table-cell;
  138. vertical-align: middle;
  139. /* #endif */
  140. color: #333;
  141. font-weight: 500;
  142. border-bottom: 1px $border-color solid;
  143. font-size: 14px;
  144. // text-align: center;
  145. }
  146. .tr-table--border {
  147. border-right: 1px $border-color solid;
  148. }
  149. /* #ifndef APP-NVUE */
  150. .uni-table-tr {
  151. ::v-deep .uni-table-th {
  152. &.table--border:last-child {
  153. // border-right: none;
  154. }
  155. }
  156. ::v-deep .uni-table-td {
  157. &.table--border:last-child {
  158. // border-right: none;
  159. }
  160. }
  161. }
  162. /* #endif */
  163. </style>