import {inject, h, resolveComponent} from "vue" // import RenderContent3 from "./render-content-3" import type {columnProps, tableDataProps} from "../../types" import {ElTableColumn} from "element-plus" /** * 从自己开始往上查找指定的插槽 * @param slotName 插槽名称 */ function findParentSlot (that, slotName="default") { // console.log("往上找节点的插槽", that.$slots) if (that.$slots[slotName]) { return that.$slots[slotName] } that = that.$pareent if (that) { return findParentSlot(that, slotName) } return null } /** * 生成插槽 * @param slotName 插槽名称 * @param scope 插槽作用域 * @returns */ function generateColumnSlot (slotName, scope) { const tempSlot = findParentSlot(this, slotName) const {row,$index} = scope return tempSlot ? tempSlot({row,$index}) : 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: "RenderContent", components: { // RenderContent3, ElTableColumn, }, /** * * @param props 没有数据 * @param param1 * @returns */ setup(props, context) { const {attrs, slots} = context // console.log('props', props, context) // console.log("slots", props, slots.default?.({ // row: {}, // $index: -1, // columns: {}, // })) // const {data} = props // return { // data // } // return h('el-table-columns', []) const rhTableStore = inject("rhTableStore") // 渲染列的信息 const renderHeader = (list=[]) => { return list.map(item=> { if (item) { } }) } return { rhTableStore, } // return ()=> "哈喽,我是露水晰123!" }, render() { // console.log("引用组件ElTableColumn---:", this.$attrs.label, this.$attrs.prop, this.$attrs) const data = this.$attrs const {rhTableStore} = this let children = [] // console.log('头是否存在', data.slot, data.slot && findParentSlot(this, data.slot)) // if (data.header) { // console.log("存在头") // // 还要给其传入数据 // const tempSlot = findParentSlot(this, data.header) // if (tempSlot) { // children.push(tempSlot()) // } // } if (data.slot) { const tempSlot = findParentSlot(this, data.slot) if (tempSlot) { children.push(tempSlot()) } } // console.log('children', children) // return h("h3", { // ".lable": data.label, // }, "你好,我是露水晰123!") // 引用外部组件 resolveComponent(按名称手动解析已注册的组件), 还要给自定义组件传入插槽的, 还要使用这个组件的数据 // const ElTableColumn = resolveComponent("ElTableColumn") const result = h(resolveComponent("ElTableColumn"), genColumnProps(data,rhTableStore), { // 给组件ElTableColumn,传递默认插槽 default: (scope)=> { // console.log(22222222222,scope, this.$attrs) console.log('888', data.label, data.prop) const {row, $index} = scope if (data.header) { return generateColumnSlot.call(this, data.header, scope) } else if (data.multiHeader && Array.isArray(data.list) && data.list.length) { // 支持多级表头 // console.log("----支持多级表头", data.label, data.prop) // return data.list.map(childColumn=> h(RenderContent3, {...childColumn})) // return data.list.map(childColumn=> h(ElTableColumn, {...childColumn})) // 可以的 // return [h(resolveComponent("ElTableColumn"), data.list[0]), // h(resolveComponent("ElTableColumn"), data.list[1]) // ] // return data.list.map(item=> h(resolveComponent("ElTableColumn"), item)) // return data.list.map(item=> h("RenderContent", item)) // console.log('渲染函数------', data.list.map(item=> h(resolveComponent("RenderContent"), item))) return data.list.map(item=> h(RenderContent, item)) // 递归调用自己 } else if (data.render && typeof data.render === "function") { return h("span", data.render(row, $index)) } else if (data.slot) { // console.log('111', data.slot) // return "哈六" return generateColumnSlot.call(this, data.slot, scope) } else if (data.prop) { // console.log('111', data.prop, row) return row[data.prop] || "-" } else { return generateColumnSlot.call(this, "default", scope) } console.log('未知') }, }) console.log('整体的渲染函数------', result) return result }, }