tableRow.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <style lang="scss" scoped>
  2. .u-table-row {
  3. display: flex;
  4. flex-direction: row;
  5. align-items: center;
  6. border-bottom: 1px solid #ebeef5;
  7. overflow: hidden;
  8. position: relative;
  9. }
  10. // 添加border样式支持
  11. .u-table-border {
  12. border-top: 1px solid #ebeef5;
  13. border-left: 1px solid #ebeef5;
  14. border-right: 1px solid #ebeef5;
  15. .u-table-cell {
  16. border-right: 1px solid #ebeef5;
  17. }
  18. .u-table-cell:last-child {
  19. border-right: none;
  20. }
  21. }
  22. .u-table-cell {
  23. flex: 1;
  24. display: flex;
  25. flex-direction: row;
  26. padding: 10px 10px;
  27. font-size: 14px;
  28. white-space: nowrap;
  29. overflow: hidden;
  30. text-overflow: ellipsis;
  31. line-height: 1.1;
  32. }
  33. .u-table-row-zebra {
  34. background-color: #fafafa;
  35. }
  36. .u-table-row-highlight {
  37. background-color: #f5f7fa;
  38. }
  39. .u-table-empty {
  40. text-align: center;
  41. padding: 20px;
  42. color: #999;
  43. }
  44. .u-text-left {
  45. text-align: left;
  46. }
  47. .u-text-center {
  48. text-align: center;
  49. }
  50. .u-text-right {
  51. text-align: right;
  52. }
  53. </style>
  54. <template>
  55. <view class="u-table-row u-table-row-child"
  56. :class="[highlightCurrentRow && currentRow === row ? 'u-table-row-highlight' : '',
  57. rowClassName ? rowClassName(row, rowIndex) : '',
  58. stripe && rowIndex % 2 === 1 ? 'u-table-row-zebra' : ''
  59. ]" :style="{height: rowHeight}" @click="handleRowClick(row)">
  60. <view v-for="(col, colIndex) in columns" :key="col.key"
  61. class="u-table-cell"
  62. :class="[col.align ? 'u-text-' + col.align : '',
  63. cellClassName ? cellClassName(row, col) : '',
  64. getFixedClass(col)
  65. ]"
  66. :style="cellStyleInner({row: row, column: col, rowIndex: rowIndex, columnIndex: colIndex, level: level})">
  67. <!-- 复选框列 -->
  68. <view v-if="col.type === 'selection'">
  69. <checkbox :checked="isSelected(row)"
  70. @click.stop="$emit('toggleSelect', row)" />
  71. </view>
  72. <template v-else>
  73. <!-- 在mainCol列显示展开图标 -->
  74. <view v-if="col.key === computedMainCol && hasTree"
  75. @click.stop="toggleExpand(row)" :style="{width: expandWidth}">
  76. <view v-if="row.children && row.children.length > 0">
  77. {{ isExpanded(row) ? '▼' : '▶' }}
  78. </view>
  79. </view>
  80. <slot name="cellChild" :row="row" :column="col" :prow="parentRow"
  81. :rowIndex="rowIndex" :columnIndex="colIndex" :level="level">
  82. <view class="u-table-cell_content">
  83. {{ row[col.key] }}
  84. </view>
  85. </slot>
  86. </template>
  87. </view>
  88. </view>
  89. <!-- 递归渲染更深层的子级 -->
  90. <template v-if="isExpanded(row) && row[treeProps.children] && row[treeProps.children].length">
  91. <template v-for="(rowChild, childIndex) in row[treeProps.children]" :key="rowChild[rowKey] || childIndex">
  92. <table-row
  93. :row="rowChild"
  94. :rowIndex="childIndex"
  95. :parent-row="row"
  96. :columns="columns"
  97. :tree-props="treeProps"
  98. :row-key="rowKey"
  99. :expanded-keys="expandedKeys"
  100. :cell-style-inner="cellStyleInner"
  101. :is-expanded="isExpanded"
  102. :row-class-name="rowClassName"
  103. :stripe="stripe"
  104. :cell-class-name="cellClassName"
  105. :get-fixed-class="getFixedClass"
  106. :highlight-current-row="highlightCurrentRow"
  107. :current-row="currentRow"
  108. :handle-row-click="handleRowClick"
  109. :toggle-expand="toggleExpand"
  110. :level="level + 1"
  111. :rowHeight="rowHeight"
  112. :hasTree="hasTree"
  113. :selectedRows="selectedRows"
  114. :expandWidth="expandWidth"
  115. :computed-main-col="computedMainCol"
  116. @toggle-select="$emit('toggleSelect', $event)"
  117. @row-click="$emit('rowClick', $event)"
  118. @toggle-expand="$emit('toggleExpand', $event)"
  119. >
  120. <template v-slot:cellChild="scope">
  121. <slot name="cellChild" :row="scope.row" :column="scope.column" :prow="scope.prow"
  122. :rowIndex="scope.rowIndex" :columnIndex="scope.columnIndex" :level="level">
  123. </slot>
  124. </template>
  125. </table-row>
  126. </template>
  127. </template>
  128. </template>
  129. <script>
  130. export default {
  131. name: 'tableRow',
  132. props: {
  133. row: {
  134. type: Object,
  135. required: true
  136. },
  137. rowIndex: {
  138. type: Number,
  139. required: true
  140. },
  141. parentRow: {
  142. type: Object,
  143. default: null
  144. },
  145. columns: {
  146. type: Array,
  147. required: true
  148. },
  149. treeProps: {
  150. type: Object,
  151. required: true
  152. },
  153. rowKey: {
  154. type: String,
  155. required: true
  156. },
  157. expandedKeys: {
  158. type: Array,
  159. required: true
  160. },
  161. cellStyleInner: {
  162. type: Function,
  163. required: true
  164. },
  165. isExpanded: {
  166. type: Function,
  167. required: true
  168. },
  169. rowClassName: {
  170. type: Function,
  171. default: null
  172. },
  173. stripe: {
  174. type: Boolean,
  175. default: false
  176. },
  177. cellClassName: {
  178. type: Function,
  179. default: null
  180. },
  181. getFixedClass: {
  182. type: Function,
  183. required: true
  184. },
  185. highlightCurrentRow: {
  186. type: Boolean,
  187. default: false
  188. },
  189. currentRow: {
  190. type: Object,
  191. default: null
  192. },
  193. handleRowClick: {
  194. type: Function,
  195. required: true
  196. },
  197. toggleExpand: {
  198. type: Function,
  199. required: true
  200. },
  201. level: {
  202. type: Number,
  203. required: true
  204. },
  205. // 添加computedMainCol属性
  206. computedMainCol: {
  207. type: String,
  208. required: true
  209. },
  210. expandWidth: {
  211. type: String,
  212. required: true
  213. },
  214. hasTree: {
  215. type: Boolean,
  216. required: false
  217. },
  218. selectedRows: {
  219. type: Array,
  220. required: false
  221. },
  222. rowHeight: {
  223. type: String,
  224. required: true
  225. }
  226. },
  227. emits: ['rowClick', 'toggleExpand', 'toggleSelect'],
  228. methods: {
  229. isSelected(row) {
  230. return this.selectedRows.some(r => r[this.rowKey] === row[this.rowKey]);
  231. }
  232. }
  233. }
  234. </script>