|
2 | 2 | * @description table组件 |
3 | 3 | * @author 阿怪 |
4 | 4 | * @date 2021/8/23 11:30 上午 |
5 | | - * @version v0.0.3 |
| 5 | + * @version v0.0.4 |
6 | 6 | * |
7 | 7 | * 一个极度简单的table组件 |
8 | 8 | * beta版 支持 slot width 设置 |
|
12 | 12 | * v0.0.1-beta.2 新增header fixed功能 |
13 | 13 | * v0.0.2 header样式根据height变换 |
14 | 14 | * v0.0.3 添加无数据提示和empty插槽 |
| 15 | + * v0.0.4 添加index参数、添加两个异常提醒,添加v-for支持 |
15 | 16 | */ |
16 | | -import { defineComponent, h, VNode } from 'vue' |
| 17 | +import { defineComponent, Fragment, h, VNode } from 'vue' |
17 | 18 | import { isEmpty, notEmpty } from "../../dependents/_utils/tools"; |
18 | 19 | import Printer from "../../other/printer/Printer"; |
19 | 20 | import { props } from "./api"; |
| 21 | +import MTableColumn from "./MTableColumn"; |
20 | 22 |
|
21 | 23 |
|
22 | 24 | const img = h('td', { class: 'm-table-tbody-img' }); |
@@ -81,26 +83,40 @@ export default defineComponent({ |
81 | 83 | if (notEmpty(slots.default())) { |
82 | 84 | const defaultSlot: any[] = slots.default(); |
83 | 85 | const tableColumn: columnType[] = []; |
| 86 | + |
| 87 | + const initByTableColumn = (info: { param: string, label: string, width?: number | string }, children: any) => { |
| 88 | + const { param, label, width } = info; |
| 89 | + tableColumn.push({ |
| 90 | + key: param, |
| 91 | + children |
| 92 | + }); |
| 93 | + // 构造thead |
| 94 | + theadThList.push(h('th', { class: 'm-th', width }, label)); |
| 95 | + } |
| 96 | + |
84 | 97 | // 遍历column |
85 | 98 | defaultSlot.forEach(s => { |
86 | | - if (s.type.name !== 'MTableColumn') { |
87 | | - error('列表子节点必须传入m-table-column,否则将会被过滤。'); |
| 99 | + |
| 100 | + // 如果是Fragment类型,那大概率是v-for生成的,其他场景暂时无法判断 |
| 101 | + if (s.type === Fragment) { |
| 102 | + s.children.forEach((c: typeof MTableColumn) => { |
| 103 | + initByTableColumn(c.props, c.children); |
| 104 | + }); |
88 | 105 | return; |
89 | 106 | } |
90 | 107 |
|
| 108 | + |
| 109 | + if (s.type.name !== 'MTableColumn') { |
| 110 | + error(`传入子节点:${s.type.name},列表子节点必须传入m-table-column,否则将会被过滤。`); |
| 111 | + return; |
| 112 | + } |
91 | 113 | if (isEmpty(s.props)) { |
92 | 114 | error('m-table-column必须传入props属性'); |
93 | 115 | return; |
94 | 116 | } |
95 | | - |
96 | | - const { param, label, width } = s.props; |
97 | | - tableColumn.push({ |
98 | | - key: param, |
99 | | - children: s.children |
100 | | - }); |
101 | | - // 构造thead |
102 | | - theadThList.push(h('th', { class: 'm-th', width }, label)); |
| 117 | + initByTableColumn(s.props, s.children); |
103 | 118 | }) |
| 119 | + |
104 | 120 | props.data.forEach((d: any, i) => { |
105 | 121 | tbodyTrList.push(dataTrRender(d, i, tableColumn)); |
106 | 122 | }) |
|
0 commit comments