elementUI?Table?自定義表頭動態(tài)數(shù)據(jù)及插槽的操作
需求
項目需求是高度自定義列表界面,表格的表頭由后端返回,并且用戶可以自定義。而且需要根據(jù)用戶自定義的表頭,數(shù)據(jù)顯示不同的樣式。比如有些字段是標簽,有些字段是id需要根據(jù)數(shù)據(jù)字典查詢對應(yīng)的name(從數(shù)據(jù)字典獲取值不做講解)。
效果
一、動態(tài)生成表頭并填入數(shù)據(jù)

二、動態(tài)生成表頭并使用插槽

代碼
一、動態(tài)生成表頭并且數(shù)據(jù)處理
html
<el-table ref="table" :data="tableData" border stripe> <el-table-column type="selection" width="55" fixed="left"></el-table-column> <el-table-column v-for="item in tableTitleList" :key="item.key" :prop="item.key" :label="item.title" show-overflow-tooltip min-width="200"></el-table-column> <el-table-column label="操作" fixed="right" min-width="230"> <template slot-scope="scope"> <el-button class="icon-style" icon="el-icon-view" size="mini" @click="onDetails(scope.row)"></el-button> <el-button type="primary" class="icon-style" icon="el-icon-success" size="mini" @click="onDetails(scope.row)"></el-button> <el-button type="warning" class="icon-style" icon="el-icon-edit" size="mini" @click="onEdit(scope.row)"></el-button> <el-button type="danger" class="icon-style" icon="el-icon-delete" size="mini" @click="onDetails(scope.row)"></el-button> </template> </el-table-column> </el-table>
js
import api from './api'
export default {
data() {
return {
loading: false,
tableData: [],
tableTitleList: []
}
},
created() {
this.init()
},
methods: {
// 初始化
init() {
// 獲取表格中顯示字段 解決加載中界面抖動問題
const individual = JSON.parse(localStorage.getItem('list'))
this.tableTitleList= individual
this.loading = true
this.dictInit().then(async () => {
await api.init().then(res => {
if (res.code === 2000) {
this.tableTitleList = []
this.tableData = []
// res.title_list // 后端返回的表頭數(shù)據(jù)
// 獲取所有啟用字段
res.title_list .map(item => {
if (item.display === 1) {
this.tableTitleList.push(item)
}
})
localStorage.setItem('list', JSON.stringify(this.tableTitleList))
// 獲取所有數(shù)據(jù)
this.dataProcessing(res.data) // 數(shù)據(jù)處理
// 其他操作
...
this.$nextTick(() => {
this.loading = false
})
}
}).catch(() => {
this.loading = false
})
})
},
// 數(shù)據(jù)處理
dataProcessing(data) {
// 對數(shù)據(jù)進行處理 簡單處理即可
...
}
}
}后端返回數(shù)據(jù)
{
"code": 200,
"msg": "成功",
"title_list ": [
{
"title": "名稱",
"key": "name",
},
{
"title": "號碼",
"key": "number",
},
// 其他字段類似
...
],
"data": [
{
"name": "123",
"number": "134****2222",
"createId": "12",
"fenpeiId": "13",
"flag": "37,38",
"createTime": "2023-10-24 10:28:30"
},
// 其他字段類似
...
],
"page": 1,
"total": 1000,
"limit": 10
}二、處理后的數(shù)據(jù)使用插槽
每個單元格中的prop的值:scope.column.property 每個單元格中的值:scope.row[scope.column.property]
html
<el-table ref="table" :data="tableData" border stripe>
<el-table-column type="selection" width="55" fixed="left"></el-table-column>
<el-table-column v-for="item in tableTitleList" :key="item.key" :prop="item.key" :label="item.title" show-overflow-tooltip min-width="200">
<template slot-scope="scope">
<span v-if="scope.column.property === 'flag'">
<el-tag type="success" v-for="every in scope.row[scope.column.property]" :key="every" size="mini" style="margin: 0 2px;">{{ every }}</el-tag>
</span>
<span v-else>{{ scope.row[scope.column.property] }}</span>
</template>
</el-table-column>
<el-table-column label="操作" fixed="right" min-width="230">
<template slot-scope="scope">
<el-button class="icon-style" icon="el-icon-view" size="mini" @click="onDetails(scope.row)"></el-button>
<el-button type="primary" class="icon-style" icon="el-icon-success" size="mini" @click="onDetails(scope.row)"></el-button>
<el-button type="warning" class="icon-style" icon="el-icon-edit" size="mini" @click="onEdit(scope.row)"></el-button>
<el-button type="danger" class="icon-style" icon="el-icon-delete" size="mini" @click="onDetails(scope.row)"></el-button>
</template>
</el-table-column>
</el-table>到此這篇關(guān)于elementUI Table 自定義表頭動態(tài)數(shù)據(jù)及插槽的操作的文章就介紹到這了,更多相關(guān)elementUI Table 自定義表頭內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用js通過url傳參把數(shù)據(jù)從一個頁面?zhèn)鞯搅硪粋€頁面
如果是傳到新頁面的話,你網(wǎng)站基于什么語言開發(fā)直接用get或者post獲取,然后輸出到這個層2014-09-09
javascript實現(xiàn)二級級聯(lián)菜單的簡單制作
這篇文章主要介紹了javascript實現(xiàn)二級級聯(lián)菜單的簡單制作,感興趣的小伙伴們可以參考一下2015-11-11
一文詳解為何在Vue3中uni.createCanvasContext畫不出canvas
這篇文章主要介紹了為何在Vue3中uni.createCanvasContext畫不出canvas的相關(guān)資料,包括Canvas畫布空白、組件作用域不同導(dǎo)致的渲染失敗以及如何實現(xiàn)連線效果,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2026-01-01
Apply an AutoFormat to an Excel Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet...2007-06-06
利用函數(shù)的惰性載入提高javascript代碼執(zhí)行效率
在 addEvent 函數(shù)每次調(diào)用的時候都要走一遍,如果瀏覽器支持其中的一種方法,那么他就會一直支持了,就沒有必要再進行其他分支的檢測了2014-05-05
JS圖片左右無縫隙滾動的實現(xiàn)(兼容IE,Firefox 遵循W3C標準)
下面小編就為大家?guī)硪黄狫S圖片左右無縫隙滾動的實現(xiàn)(兼容IE,Firefox 遵循W3C標準)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09
將函數(shù)的實際參數(shù)轉(zhuǎn)換成數(shù)組的方法
實際參數(shù)在函數(shù)中我們可以使用 arguments 對象獲得 (注:形參可通過 arguments.callee 獲得),雖然 arguments 對象與數(shù)組形似,但仍不是真正意義上的數(shù)組。2010-01-01

