uni-tr.vue 4.3 KB

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