uni-tr.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. destroyed() {
  77. const index = this.root.trChildren.findIndex(i => i === this)
  78. this.root.trChildren.splice(index, 1)
  79. this.root.isNodata()
  80. },
  81. methods: {
  82. minWidthUpdate(width) {
  83. this.widthThArr.push(width)
  84. },
  85. // 选中
  86. checkboxSelected(e) {
  87. let rootData = this.root.data.find(v => v[this.root.rowKey] === this.keyValue)
  88. this.checked = e.checked
  89. this.root.check(rootData||this, e.checked,rootData? this.keyValue:null)
  90. },
  91. change(e) {
  92. this.root.trChildren.forEach(item => {
  93. if (item === this) {
  94. this.root.check(this, e.detail.value.length > 0 ? true : false)
  95. }
  96. })
  97. },
  98. /**
  99. * 获取父元素实例
  100. */
  101. getTable(name = 'uniTable') {
  102. let parent = this.$parent
  103. let parentName = parent.$options.name
  104. while (parentName !== name) {
  105. parent = parent.$parent
  106. if (!parent) return false
  107. parentName = parent.$options.name
  108. }
  109. return parent
  110. }
  111. }
  112. }
  113. </script>
  114. <style lang="scss">
  115. $border-color: #ebeef5;
  116. .uni-table-tr {
  117. /* #ifndef APP-NVUE */
  118. display: table-row;
  119. transition: all 0.3s;
  120. box-sizing: border-box;
  121. /* #endif */
  122. }
  123. .checkbox {
  124. padding: 0 8px;
  125. width: 26px;
  126. padding-left: 12px;
  127. /* #ifndef APP-NVUE */
  128. display: table-cell;
  129. vertical-align: middle;
  130. /* #endif */
  131. color: #333;
  132. font-weight: 500;
  133. border-bottom: 1px $border-color solid;
  134. font-size: 14px;
  135. // text-align: center;
  136. }
  137. .tr-table--border {
  138. border-right: 1px $border-color solid;
  139. }
  140. /* #ifndef APP-NVUE */
  141. .uni-table-tr {
  142. ::v-deep .uni-table-th {
  143. &.table--border:last-child {
  144. // border-right: none;
  145. }
  146. }
  147. ::v-deep .uni-table-td {
  148. &.table--border:last-child {
  149. // border-right: none;
  150. }
  151. }
  152. }
  153. /* #endif */
  154. </style>