import {inject, h, resolveComponent} from "vue" import type {columnProps, tableDataProps} from "../../types" import {ElTableColumn} from "element-plus" /** * 从自己开始往上查找指定的插槽 * @param slotName 插槽名称 */ function findParentSlot ($self:object, slotName:string="default") { // console.log("往上找节点的插槽", that.$slots) const slot = $self.$slots[slotName] const parent = $self.$parent return slot || (parent ? findParentSlot(parent, slotName) : null) } /** * 生成插槽 * @param slotName 插槽名称 * @param scope 插槽作用域 * @returns */ function generateColumnSlot (slotName:string, scope:object) { const tempSlot = findParentSlot(this, slotName) return tempSlot ? tempSlot(scope) : undefined } /** * 生成列的属性 * @param data * @param rhTableStore * @returns */ function genColumnProps (data:columnProps, rhTableStore:tableDataProps) { const obj = { "label": data.label, "prop": data.prop, "width": data.width, "minWidth": data.minWidth, "fixed": data.fixed, "align": data.align || rhTableStore.align, } Object.keys(obj).forEach(key=> { if (!obj[key]) { delete obj[key] } }) return obj } export default { name: "RenderColumnList", components: { ElTableColumn, }, setup() { const rhTableStore = inject("rhTableStore") // 渲染列的信息 function renderColumnList(columns=[]) { const $self = this return columns.map((column)=> { let slot = {} // 自定义表头 if (column.header) { slot.header = (scope)=> { return generateColumnSlot.call($self, column.header, scope) } } slot.default = (scope)=> { const {row, $index} = scope if (column.multiHeader && Array.isArray(column.list) && column.list.length) { return renderColumnList.call($self, column.list) // 多级表头,递归调用 } else if (column.render && typeof column.render === "function") { return h("span", column.render(row, $index)) } else if (column.slot) { return generateColumnSlot.call($self, column.slot, scope) } else if (column.prop) { return row[column.prop] || "-" } else { return generateColumnSlot.call($self, "default", scope) } } return h(ElTableColumn, genColumnProps(column, rhTableStore), slot)} ) } return { renderColumnList, } }, render() { const {columns} = this.$attrs const {renderColumnList} = this const $self = this return renderColumnList.call($self, columns) }, }