uni-tr.vue 4.1 KB

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