tableRow.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <style lang="scss" scoped>
  2. .u-table-row {
  3. display: flex;
  4. flex-direction: row;
  5. position: relative;
  6. }
  7. // 添加border样式支持
  8. .u-table-border {
  9. border-top: 1px solid #ebeef5;
  10. border-left: 1px solid #ebeef5;
  11. border-right: 1px solid #ebeef5;
  12. .u-table-cell {
  13. border-right: 1px solid #ebeef5;
  14. }
  15. .u-table-cell:last-child {
  16. border-right: none;
  17. }
  18. }
  19. .u-table-cell {
  20. flex: 1;
  21. display: flex;
  22. flex-direction: row;
  23. align-items: center;
  24. padding: 10px 1px;
  25. font-size: 14px;
  26. white-space: nowrap;
  27. overflow: hidden;
  28. text-overflow: ellipsis;
  29. line-height: 1.1;
  30. border-bottom: 1px solid #ebeef5;
  31. &.u-text-left {
  32. justify-content: flex-start;
  33. text-align: left;
  34. }
  35. &.u-text-center {
  36. justify-content: center;
  37. text-align: center;
  38. }
  39. &.u-text-right {
  40. justify-content: flex-end;
  41. text-align: right;
  42. }
  43. }
  44. .u-table-row-zebra {
  45. background-color: #fafafa;
  46. }
  47. .u-table-row-highlight {
  48. background-color: #f5f7fa;
  49. }
  50. .u-table-empty {
  51. text-align: center;
  52. padding: 20px;
  53. color: #999;
  54. }
  55. // 隐藏被合并的单元格
  56. .u-table-cell-hidden {
  57. opacity: 0;
  58. }
  59. // 合并单元格样式
  60. .u-table-cell-merged {
  61. z-index: 1;
  62. }
  63. </style>
  64. <template>
  65. <view class="u-table-row u-table-row-child"
  66. :class="[highlightCurrentRow && currentRow === row ? 'u-table-row-highlight' : '',
  67. rowClassName ? rowClassName(row, rowIndex) : '',
  68. stripe && rowIndex % 2 === 1 ? 'u-table-row-zebra' : ''
  69. ]" :style="{height: rowHeight}" @click="handleRowClick(row)">
  70. <view v-for="(col, colIndex) in columns" :key="col.key"
  71. class="u-table-cell"
  72. :class="[col.align ? 'u-text-' + col.align : '',
  73. cellClassName ? cellClassName(row, col) : '',
  74. getFixedClass(col),
  75. getCellSpanClass(rowIndex, colIndex)
  76. ]"
  77. :style="[cellStyleInner({row: row, column: col, rowIndex: rowIndex, columnIndex: colIndex, level: level}), getCellSpanStyle(rowIndex, colIndex)]">
  78. <!-- 复选框列 -->
  79. <view v-if="col.type === 'selection'">
  80. <checkbox :checked="isSelected(row)"
  81. @click.stop="$emit('toggleSelect', row)" />
  82. </view>
  83. <template v-else>
  84. <!-- 在mainCol列显示展开图标 -->
  85. <view v-if="col.key === computedMainCol && hasTree"
  86. @click.stop="toggleExpand(row)" :style="{width: expandWidth}">
  87. <view v-if="row.children && row.children.length > 0">
  88. {{ isExpanded(row) ? '▼' : '▶' }}
  89. </view>
  90. </view>
  91. <slot name="cellChild" :row="row" :column="col" :prow="parentRow"
  92. :rowIndex="rowIndex" :columnIndex="colIndex" :level="level">
  93. <view class="u-table-cell_content">
  94. {{ row[col.key] }}
  95. </view>
  96. </slot>
  97. </template>
  98. </view>
  99. </view>
  100. <!-- 递归渲染更深层的子级 -->
  101. <template v-if="isExpanded(row) && row[treeProps.children] && row[treeProps.children].length">
  102. <template v-for="(rowChild, childIndex) in row[treeProps.children]" :key="rowChild[rowKey] || childIndex">
  103. <table-row
  104. :row="rowChild"
  105. :rowIndex="childIndex"
  106. :parent-row="row"
  107. :columns="columns"
  108. :tree-props="treeProps"
  109. :row-key="rowKey"
  110. :expanded-keys="expandedKeys"
  111. :cell-style-inner="cellStyleInner"
  112. :is-expanded="isExpanded"
  113. :row-class-name="rowClassName"
  114. :stripe="stripe"
  115. :cell-class-name="cellClassName"
  116. :get-fixed-class="getFixedClass"
  117. :highlight-current-row="highlightCurrentRow"
  118. :current-row="currentRow"
  119. :handle-row-click="handleRowClick"
  120. :toggle-expand="toggleExpand"
  121. :level="level + 1"
  122. :rowHeight="rowHeight"
  123. :hasTree="hasTree"
  124. :selectedRows="selectedRows"
  125. :expandWidth="expandWidth"
  126. :computed-main-col="computedMainCol"
  127. :span-method="spanMethod"
  128. @toggle-select="$emit('toggleSelect', $event)"
  129. @row-click="$emit('rowClick', $event)"
  130. @toggle-expand="$emit('toggleExpand', $event)"
  131. >
  132. <template v-slot:cellChild="scope">
  133. <slot name="cellChild" :row="scope.row" :column="scope.column" :prow="scope.prow"
  134. :rowIndex="scope.rowIndex" :columnIndex="scope.columnIndex" :level="level">
  135. </slot>
  136. </template>
  137. </table-row>
  138. </template>
  139. </template>
  140. </template>
  141. <script>
  142. export default {
  143. name: 'tableRow',
  144. props: {
  145. row: {
  146. type: Object,
  147. required: true
  148. },
  149. rowIndex: {
  150. type: Number,
  151. required: true
  152. },
  153. parentRow: {
  154. type: Object,
  155. default: null
  156. },
  157. columns: {
  158. type: Array,
  159. required: true
  160. },
  161. treeProps: {
  162. type: Object,
  163. required: true
  164. },
  165. rowKey: {
  166. type: String,
  167. required: true
  168. },
  169. expandedKeys: {
  170. type: Array,
  171. required: true
  172. },
  173. cellStyleInner: {
  174. type: Function,
  175. required: true
  176. },
  177. isExpanded: {
  178. type: Function,
  179. required: true
  180. },
  181. rowClassName: {
  182. type: Function,
  183. default: null
  184. },
  185. stripe: {
  186. type: Boolean,
  187. default: false
  188. },
  189. cellClassName: {
  190. type: Function,
  191. default: null
  192. },
  193. getFixedClass: {
  194. type: Function,
  195. required: true
  196. },
  197. highlightCurrentRow: {
  198. type: Boolean,
  199. default: false
  200. },
  201. currentRow: {
  202. type: Object,
  203. default: null
  204. },
  205. handleRowClick: {
  206. type: Function,
  207. required: true
  208. },
  209. toggleExpand: {
  210. type: Function,
  211. required: true
  212. },
  213. level: {
  214. type: Number,
  215. required: true
  216. },
  217. // 添加computedMainCol属性
  218. computedMainCol: {
  219. type: String,
  220. required: true
  221. },
  222. expandWidth: {
  223. type: String,
  224. required: true
  225. },
  226. hasTree: {
  227. type: Boolean,
  228. required: false
  229. },
  230. selectedRows: {
  231. type: Array,
  232. required: false
  233. },
  234. rowHeight: {
  235. type: String,
  236. required: true
  237. },
  238. // 添加spanMethod属性
  239. spanMethod: {
  240. type: Function,
  241. default: null
  242. }
  243. },
  244. emits: ['rowClick', 'toggleExpand', 'toggleSelect'],
  245. methods: {
  246. isSelected(row) {
  247. return this.selectedRows.some(r => r[this.rowKey] === row[this.rowKey]);
  248. },
  249. // 获取单元格的合并信息
  250. getCellSpan(rowIndex, columnIndex) {
  251. if (typeof this.spanMethod !== 'function') {
  252. return { rowspan: 1, colspan: 1 };
  253. }
  254. const row = this.row;
  255. const column = this.columns[columnIndex];
  256. const result = this.spanMethod({
  257. row,
  258. column,
  259. rowIndex,
  260. columnIndex
  261. });
  262. if (Array.isArray(result)) {
  263. const [rowspan, colspan] = result;
  264. return { rowspan: rowspan != null ? rowspan : 1, colspan: colspan != null ? colspan : 1 };
  265. } else if (typeof result === 'object') {
  266. return {
  267. rowspan: rowspan != null ? rowspan : 1,
  268. colspan: colspan != null ? colspan : 1
  269. };
  270. }
  271. return { rowspan: 1, colspan: 1 };
  272. },
  273. // 获取单元格的样式类
  274. getCellSpanClass(rowIndex, columnIndex) {
  275. const span = this.getCellSpan(rowIndex, columnIndex);
  276. // 如果rowspan为0或colspan为0,表示该单元格被合并,需要隐藏
  277. if (span.rowspan === 0 || span.colspan === 0) {
  278. return 'u-table-cell-hidden';
  279. } else if (span.rowspan > 1 || span.colspan > 1) {
  280. // 如果有合并,添加合并样式类
  281. return 'u-table-cell-merged';
  282. }
  283. return '';
  284. },
  285. // 获取单元格的样式
  286. getCellSpanStyle(rowIndex, columnIndex) {
  287. const span = this.getCellSpan(rowIndex, columnIndex);
  288. const style = {};
  289. // 设置rowspan
  290. if (span.rowspan > 1) {
  291. // 正确计算合并后的高度
  292. const currentHeight = parseInt(this.rowHeight);
  293. if (!isNaN(currentHeight)) {
  294. style.height = `${span.rowspan * currentHeight}px`;
  295. }
  296. }
  297. // 设置colspan
  298. if (span.colspan > 1) {
  299. style.flex = span.colspan;
  300. }
  301. // 如果rowspan为0或colspan为0,表示该单元格被合并,需要隐藏
  302. if (span.rowspan === 0 || span.colspan === 0) {
  303. style.display = 'none';
  304. }
  305. return style;
  306. }
  307. }
  308. }
  309. </script>