elementUI?Table?自定義表頭動態(tài)數(shù)據(jù)及插槽的操作
需求
項目需求是高度自定義列表界面,表格的表頭由后端返回,并且用戶可以自定義。而且需要根據(jù)用戶自定義的表頭,數(shù)據(jù)顯示不同的樣式。比如有些字段是標(biāo)簽,有些字段是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ù)進(jìn)行處理 簡單處理即可 ... } } }
后端返回數(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)文章
基于JavaScript實現(xiàn)網(wǎng)紅太空人表盤的完整代碼
這篇文章主要介紹了基于JavaScript實現(xiàn)網(wǎng)紅太空人表盤的完整代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03Javascript attachEvent傳遞參數(shù)的辦法
找了半天找到的解決辦法,看介紹說是javascript的閉包問題,導(dǎo)致得不能直接讀取外部的那個函數(shù),不然就所有傳遞的參數(shù)都變?yōu)樽詈笠粋€了。2009-12-12JavaScript中的索引數(shù)組、關(guān)聯(lián)數(shù)組和靜態(tài)數(shù)組、動態(tài)數(shù)組講解
這篇文章主要介紹了JavaScript中的索引數(shù)組、關(guān)聯(lián)數(shù)組和靜態(tài)數(shù)組、動態(tài)數(shù)組講解,本文介紹了從數(shù)組的下標(biāo)分為索引數(shù)組、關(guān)聯(lián)數(shù)組、從對數(shù)據(jù)的存儲分為靜態(tài)數(shù)組、動態(tài)數(shù)組,并給出了示例,需要的朋友可以參考下2014-11-11JavaScript實現(xiàn)動態(tài)生成表格案例詳解
本文主要介紹了通過JavaScript實現(xiàn)一個動態(tài)添加表格的案例,當(dāng)點擊添加按鈕時,可以彈出一個表單,然后將輸入的內(nèi)容添加到表格中,也可以將表格中的整行內(nèi)容清除。感興趣的可以學(xué)習(xí)一下2021-12-12