uni-tr.vue 4.1 KB

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