uni-tr.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 = Number(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. if (this.widthThArr.length > 0) {
  94. const selectionWidth = this.selection === 'selection' ? 50 : 0;
  95. this.root.minWidth = Number(this.widthThArr.reduce((a, b) => Number(a) + Number(b))) + selectionWidth;
  96. }
  97. },
  98. // 选中
  99. checkboxSelected(e) {
  100. let rootData = this.root.data.find(v => v[this.root.rowKey] === this.keyValue)
  101. this.checked = e.checked
  102. this.root.check(rootData||this, e.checked,rootData? this.keyValue:null)
  103. },
  104. change(e) {
  105. this.root.trChildren.forEach(item => {
  106. if (item === this) {
  107. this.root.check(this, e.detail.value.length > 0 ? true : false)
  108. }
  109. })
  110. },
  111. /**
  112. * 获取父元素实例
  113. */
  114. getTable(name = 'uniTable') {
  115. let parent = this.$parent
  116. let parentName = parent.$options.name
  117. while (parentName !== name) {
  118. parent = parent.$parent
  119. if (!parent) return false
  120. parentName = parent.$options.name
  121. }
  122. return parent
  123. }
  124. }
  125. }
  126. </script>
  127. <style lang="scss">
  128. $border-color: #ebeef5;
  129. .uni-table-tr {
  130. /* #ifndef APP-NVUE */
  131. display: table-row;
  132. transition: all 0.3s;
  133. box-sizing: border-box;
  134. /* #endif */
  135. }
  136. .checkbox {
  137. padding: 0 8px;
  138. width: 26px;
  139. padding-left: 12px;
  140. /* #ifndef APP-NVUE */
  141. display: table-cell;
  142. vertical-align: middle;
  143. /* #endif */
  144. color: #333;
  145. font-weight: 500;
  146. border-bottom: 1px $border-color solid;
  147. font-size: 14px;
  148. // text-align: center;
  149. }
  150. .tr-table--border {
  151. border-right: 1px $border-color solid;
  152. }
  153. /* #ifndef APP-NVUE */
  154. .uni-table-tr {
  155. ::v-deep .uni-table-th {
  156. &.table--border:last-child {
  157. // border-right: none;
  158. }
  159. }
  160. ::v-deep .uni-table-td {
  161. &.table--border:last-child {
  162. // border-right: none;
  163. }
  164. }
  165. }
  166. /* #endif */
  167. </style>