uni-tr.vue 4.2 KB

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