vue封裝動(dòng)態(tài)表格方式詳解
前言
這里只是提供一種想法并提供一些快速實(shí)現(xiàn),實(shí)際這些技巧可以用在很多地方,如:動(dòng)態(tài)表單
實(shí)現(xiàn)方式簡(jiǎn)述
- 通過(guò)json定義要顯示的列
- 通過(guò)slot實(shí)現(xiàn)自定義列
- 通過(guò)
require.context實(shí)現(xiàn)組件的自動(dòng)注冊(cè),并通過(guò)<components is="xxx"></components>, 調(diào)用動(dòng)態(tài)注冊(cè)的組件
優(yōu)點(diǎn):
- 支持通過(guò)slot自定義擴(kuò)展
- 支持編寫(xiě)vue文件擴(kuò)展列的顯示方式
- 支持通過(guò)json來(lái)控制列順序和哪些列需要顯示哪些不需要
- 能避免封裝的table組件越來(lái)越復(fù)雜
表格實(shí)現(xiàn):
<template>
<el-table :data="tableData" style="width: 100%" height="100%">
<el-table-column
v-for="column in columnList"
:key="column.prop"
v-bind="column.columnAttrs"
>
<template slot-scope="scope">
<slot
v-if="column.type === 'slot'"
:name="column.slotName"
:row="scope.row"
></slot>
<components
v-else
:row="scope.row"
:prop="column.prop"
:config="column"
:is="`column-${column.type}`"
></components>
</template>
</el-table-column>
</el-table>
</template>
<script>
const modules = {};
// 將當(dāng)前目錄以及子目錄中的index.vue自動(dòng)注冊(cè)為組件(require.context是webpack提供的,如果是vite要用vite的方式)
const files = require.context("./", true, /index.vue$/);
files.keys().forEach((item) => {
const name = item.split("/")[1];
modules[`column-${name}`] = files(item).default;
});
console.log(modules, "modules");
export default {
name: "CustomTable",
components: {
// 組件動(dòng)態(tài)注冊(cè)
...modules,
},
props: {
tableData: {
type: Array,
default: () => [],
},
columnList: {
type: Array,
default: () => [],
},
},
};
</script>
<style scoped></style>
func組件
<template>
<span>{{ config.call(row, config) }}</span>
</template>
<script>
export default {
name: "ColumnFunc",
props: {
config: {
type: Object,
},
row: {
type: Object,
},
},
};
</script>
<style scoped></style>
text組件:
<template>
<span>{{ row[config.prop] }}</span>
</template>
<script>
export default {
name: "ColumnText",
props: {
config: {
type: Object,
},
row: {
type: Object,
},
},
};
</script>
<style scoped></style>
調(diào)用示例
<template>
<CustomTable :column-list="columnList" :table-data="tableData">
<template #operate="{ row }">
<el-button size="small">刪除</el-button>
<el-button size="small" type="primary" @click="onEdit(row)">
編輯
</el-button>
</template>
</CustomTable>
</template>
<script>
import CustomTable from "@/components/my-table/CustomTable";
export default {
name: "CustomTableDemo",
components: {
CustomTable,
},
data() {
return {
columnList: [
{
type: "text",
prop: "name",
columnAttrs: {
label: "姓名",
width: "180",
},
},
{
type: "text",
prop: "date",
columnAttrs: {
label: "日期",
width: "180",
},
},
{
type: "func",
prop: "sex",
columnAttrs: {
label: "性別",
},
call: (row) => {
switch (row.sex) {
case 1: {
return "男";
}
case 2: {
return "女";
}
default: {
return "未知";
}
}
},
},
{
type: "slot",
slotName: "operate",
prop: "operate",
columnAttrs: {
label: "操作",
},
},
],
tableData: [
{
date: "2016-05-02",
name: "王小虎",
address: "上海市普陀區(qū)金沙江路 1518 弄",
sex: 1,
},
{
date: "2016-05-04",
name: "王小虎",
address: "上海市普陀區(qū)金沙江路 1517 弄",
sex: 2,
},
{
date: "2016-05-01",
name: "王小虎",
address: "上海市普陀區(qū)金沙江路 1519 弄",
sex: 3,
},
],
};
},
methods: {
onEdit(row) {
console.log(row);
},
},
};
</script>
<style scoped></style>
效果

參考文章
以上就是vue封裝動(dòng)態(tài)表格方式詳解的詳細(xì)內(nèi)容,更多關(guān)于vue封裝動(dòng)態(tài)表格的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue項(xiàng)目build打包后部分樣式錯(cuò)亂的解決
這篇文章主要介紹了vue項(xiàng)目build打包后部分樣式錯(cuò)亂的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
vue編寫(xiě)的功能強(qiáng)大的swagger-ui頁(yè)面及使用方式
swagger是一種標(biāo)準(zhǔn)的數(shù)據(jù)格式的定義,對(duì)于不同語(yǔ)言進(jìn)行實(shí)現(xiàn)一些注解API式的東西,能快速生成這種描述restful格式的api信息的json串,本文給大家詳細(xì)介紹vue編寫(xiě)的功能強(qiáng)大的swagger-ui頁(yè)面,感興趣的朋友跟隨小編一起看看吧2022-02-02
vue實(shí)現(xiàn)四級(jí)導(dǎo)航及驗(yàn)證碼的方法實(shí)例
我們?cè)谧鲰?xiàng)目經(jīng)常會(huì)遇到多級(jí)導(dǎo)航這個(gè)需求,所以下面這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)四級(jí)導(dǎo)航及驗(yàn)證碼的相關(guān)資料,文章通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-07-07
詳解Vue-axios 設(shè)置請(qǐng)求頭問(wèn)題
這篇文章主要介紹了Vue-axios 設(shè)置請(qǐng)求頭問(wèn)題,文中給大家提到了axios設(shè)置請(qǐng)求頭內(nèi)容的方法,需要的朋友可以參考下2018-12-12
手把手搭建安裝基于windows的Vue.js運(yùn)行環(huán)境
手把手教大家搭建安裝基于windows的Vue.js的運(yùn)行環(huán)境,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
用Vue-cli搭建的項(xiàng)目中引入css報(bào)錯(cuò)的原因分析
本篇文章主要介紹了用Vue-cli搭建的項(xiàng)目中引入css報(bào)錯(cuò)的原因分析,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
vue-router2.0 組件之間傳參及獲取動(dòng)態(tài)參數(shù)的方法
下面小編就為大家?guī)?lái)一篇vue-router2.0 組件之間傳參及獲取動(dòng)態(tài)參數(shù)的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
使用兩種方式調(diào)用本地json文件(基于Vue-cli3腳手架)
這篇文章主要介紹了使用兩種方式調(diào)用本地json文件(基于Vue-cli3腳手架),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,2023-10-10

