uni-tr.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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" class="checkbox" :class="{ 'tr-table--border': border }">
  14. <table-checkbox :checked="checked" :indeterminate="indeterminate" :disabled="disabled" @checkboxSelected="checkboxSelected(checked)"></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: [] as Array<number>,
  49. ishead: true,
  50. checked: false,
  51. indeterminate:false,
  52. root: Object,
  53. head: Object,
  54. rowData: Object
  55. }
  56. },
  57. created() {
  58. this.root = this.getTable()
  59. this.head = this.getTable('uniThead')
  60. if (this.head != null) {
  61. this.ishead = false
  62. // #ifdef APP-NVUE
  63. let head = this.head as UniTheadComponentPublicInstance
  64. head.init(this as UniTheadComponentPublicInstance)
  65. // #endif
  66. }
  67. let root = this.root as UniTableComponentPublicInstance
  68. this.border = root != null ? root?.border as boolean : false;
  69. this.selection = root != null ? (root?.type!=null ? true : false) : false;
  70. let trChildren = root != null ? root?.trChildren as UniTrComponentPublicInstance[] : [];
  71. trChildren.push(this)
  72. const rowData = root?.data.find(v => v[root.rowKey] === this.keyValue)
  73. if(rowData!=null){
  74. this.rowData = rowData
  75. }
  76. let table = this.root as UniTableComponentPublicInstance
  77. table.isNodata()
  78. },
  79. mounted() {
  80. if (this.widthThArr.length > 0) {
  81. const selectionWidth = this.selection === true ? 50 : 0
  82. let root = this.root as UniTableComponentPublicInstance;
  83. root.minWidth = this.widthThArr.reduce((a, b) => a as number + b as number) + selectionWidth
  84. }
  85. },
  86. // #ifndef VUE3
  87. destroyed() {
  88. const index = this.root.trChildren.findIndex(i => i === this)
  89. this.root.trChildren.splice(index, 1)
  90. this.root.isNodata()
  91. },
  92. // #endif
  93. // #ifdef VUE3
  94. unmounted() {
  95. let root = this.root as UniTableComponentPublicInstance;
  96. const index = root.trChildren.findIndex(i => i === this)
  97. root.trChildren.splice(index, 1)
  98. root.isNodata()
  99. },
  100. // #endif
  101. methods: {
  102. minWidthUpdate(width: number) {
  103. this.widthThArr.push(width)
  104. },
  105. // 选中
  106. checkboxSelected(checked:Boolean) {
  107. let root = this.root as UniTableComponentPublicInstance
  108. let rootData = root.data?.find(v => v[root.rowKey] === this.keyValue)
  109. this.checked = checked
  110. root.check(rootData!=null?rootData:this, checked,rootData!=null? this.keyValue as string:null, false)
  111. },
  112. change(e: any) {
  113. let root = this.root as UTSJSONObject
  114. let trChildren = root?.['trChildren'] as UTSJSONObject[] ?? null
  115. let event = e as UTSJSONObject
  116. //let val = event != null && event['detail'] != null ? event['detail']?.['value'] as string : ''
  117. let val = (event['detail'] as UTSJSONObject)?.['value'] as string ?? '';
  118. trChildren?.forEach(item => {
  119. if (item === this) {
  120. let cls = this.root as UniTableComponentPublicInstance;
  121. cls.check(this, val!=''? true:false, this.keyValue as string, false)
  122. }
  123. })
  124. },
  125. /**
  126. * 获取父元素实例
  127. */
  128. getTable(name = 'uniTable') {
  129. let parent = this.$parent
  130. let parentName = parent?.$options?.name ?? ''
  131. while (parentName !== name) {
  132. parent = parent?.$parent ?? null
  133. if (parent==null) return false
  134. parentName = parent?.$options?.name as string
  135. }
  136. return parent
  137. }
  138. }
  139. }
  140. </script>
  141. <style lang="scss">
  142. $border-color: #ebeef5;
  143. .uni-table-tr {
  144. /* #ifndef APP-NVUE */
  145. /* #ifndef APP-ANDROID */
  146. display: table-row;
  147. /* #endif */
  148. transition: all 0.3s;
  149. box-sizing: border-box;
  150. /* #endif */
  151. }
  152. .checkbox {
  153. padding: 0 8px;
  154. width: 26px;
  155. padding-left: 12px;
  156. /* #ifndef APP-ANDROID */
  157. display: table-cell;
  158. vertical-align: middle;
  159. font-weight: 500;
  160. /* #endif */
  161. /* #ifdef H5 */
  162. font-size: 14px;
  163. color: #333;
  164. /* #endif */
  165. border-bottom: 1px $border-color solid;
  166. // text-align: center;
  167. }
  168. .tr-table--border {
  169. border-right: 1px $border-color solid;
  170. }
  171. /* #ifndef APP-NVUE */
  172. .uni-table-tr {
  173. ::v-deep .uni-table-th {
  174. &.table--border:last-child {
  175. // border-right: none;
  176. }
  177. }
  178. ::v-deep .uni-table-td {
  179. &.table--border:last-child {
  180. // border-right: none;
  181. }
  182. }
  183. }
  184. /* #endif */
  185. </style>