el-table 简单封装 (新手学习)
// 子组件
<template>
<div class="nq-table">
<el-table :data="tabList" height="600" border style="width: 100%">
<template v-for="(item, index) in tabHeader">
<!-- 判断是否有slot -->
<el-table-column
v-if="!item.slot"
:prop="item.prop"
:label="item.label"
>
</el-table-column>
<el-table-column v-else :prop="item.prop" :label="item.label">
<template slot-scope="{ row, $index }">
<slot
v-if="item.slot"
:name="item.slot"
:row="row"
:index="$index"
/>
</template>
</el-table-column>
</template>
</el-table>
</div>
</template>
<script>
export default {
name: "NqTable",
props: {
tabList: {
type: Array,
required: true,
default: () => [],
},
tabHeader: {
type: Array,
required: true,
default: () => [],
},
},
};
</script>
<style lang="less" scoped></style>
tabList
:是传递的table数据
tabHeader
:传递的是表头数据
使用
<template>
<div>
<nq-table :tabList="filterData" :tabHeader="tabHeader">
// #controls="{ row, index }" 插槽名称和数据
<template #controls="{ row, index }">
<el-button type="text" size="small" @click="getDtInfo(row.id)"
>查看</el-button
>
<el-button type="text" size="small" @click="upDtDicList(row)"
>编辑</el-button
>
<el-button
type="text"
size="small"
style="color: #f56c6c"
@click="delDtDicList(row)"
>
删除
</el-button>
</template>
<template #seq="{ row, index }">{{ index }} </template>
<template #isEnabled="{ row, index }">
<el-checkbox
v-model="row.isEnabled"
@change="upPartDicList('isEnabled', row.isEnabled, row.id)"
size="small"
></el-checkbox>
</template>
<template #isDefault="{ row, index }">
<el-checkbox
v-model="row.isDefault"
@change="upPartDicList('isDefault', row.isDefault, row.id)"
size="small"
></el-checkbox>
</template>
</nq-table>
<div>
<template>
export default {
data() {
return {
filterData:[],
tabHeader: [
{ label: "操作", prop: "", slot: "controls" },
{ label: "序号", prop: "", slot: "seq" },
{ label: "分类名称", prop: "category.name" },
{ label: "显示名称", prop: "displayName" },
{ label: "字典值", prop: "value" },
{ label: "字典描述", prop: "description" },
{ label: "启用情况", prop: "isEnabled", slot: "isEnabled" },
{ label: "默认显示", prop: "isDefault", slot: "isDefault" },
{ label: "排序", prop: "seq" },
],
}
}
}
思路: 子组件判断是否表头数据(
tabHeader
)里是否存在插槽(slot
)这个属性,如果没用就使用原来的el-table-column
若有就 使用插槽 并且插槽的数据传递给父组件
新手学习,大神勿吐槽 ,欢迎大神指点迷津