vue+Element-ui的el-table的多級內(nèi)容渲染問題
vue+Element-ui的el-table的多級內(nèi)容渲染
將多層內(nèi)容渲染到表格里面
平常的表格是一個對象里面的數(shù)據(jù)渲染成一行,對象里面的key就是表格的label,如果對象里面還有對象數(shù)組那該怎么去渲染,如渲染下面的數(shù)據(jù),將一個對象渲染成一行數(shù)據(jù),和平常的table表格的數(shù)據(jù)渲染不一樣,這個不能直接渲染出來,需要加第三方的label來渲染數(shù)據(jù)
tableData: [
{
name: 'Huawei P40',
properties: [
{
description: '顏色',
value: '紅色'
},
{
description: '內(nèi)存',
value: '128G'
}
]
},
{
name: '小米10',
properties: [
{
description: '顏色',
value: '黑色'
},
{
description: '內(nèi)存',
value: '128G'
}
]
}
]
代碼如下,另外加一個columnIndex數(shù)組,用來存儲數(shù)據(jù)里面的屬性的key
<template>
<el-table :data="tableData" border>
<el-table-column label="手機">
<template slot-scope="scope">
<span>{{scope.row.name}}</span>
</template>
</el-table-column>
<el-table-column v-for="(item, index) in columnIndex" :key="index" :label="item">
<template slot-scope="scope">
<span>{{scope.row.properties[index].value}}</span>
</template>
</el-table-column>
</el-table>
</template>
<script>
export default {
data() {
return {
// 有兩層數(shù)據(jù),渲染表格
columnIndex: ['顏色','內(nèi)存'],
tableData: [
{
name: 'Huawei P40',
properties: [
{
description: '顏色',
value: '紅色'
},
{
description: '內(nèi)存',
value: '128G'
}
]
},
{
name: '小米10',
properties: [
{
description: '顏色',
value: '黑色'
},
{
description: '內(nèi)存',
value: '128G'
}
]
}
]
}
}
}
</script>
<style>
</style>
效果圖:
可以看到,properties里面的數(shù)據(jù)也渲染到表格里面了

Element UI 多級表格渲染
// TableColumn.js
function renderColumn(h, column){
return h{
'el-table-column',
{
props:{
align:'center',
...column
}
},
column&&column.children&&column.children.map(c=> renderColumn(h, c))
}
}
export default {
functional: true,
props: {
columns: {
type:Array,
default: ()=>[]
}
},
render(h, ctx) {
return ctx.props.columns.map(column => renderColumn(h, column ))
}
}// Table.vue
<template>
<el-table
ref="elTable"
:data="list"
stripe
border
height="100%"
>
</el-table>
</template>
<script>
import TableColumn from './TableColumn.js'
const columns = [
{
label:'一級',
children:[
{
label:'二級一',
align:'center',
prop:'name1'
},
{
label:'二級二',
children:[
{
label:'三級一',
align:'center',
prop:'name2'
},
{
label:'三級二',
align:'center',
prop:'name3'
}
]
}
]
}
]
export default {
name: "Table",
data() {
return {
list:[]
}
}
}
</script>總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Composition API思想封裝NProgress示例詳解
這篇文章主要為大家介紹了Composition API思想封裝NProgress示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
element-vue實現(xiàn)網(wǎng)頁鎖屏功能(示例代碼)
這篇文章主要介紹了element-vue實現(xiàn)網(wǎng)頁鎖屏功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11
使用Vue Router進(jìn)行路由組件傳參的實現(xiàn)方式
Vue Router 為 Vue.js 應(yīng)用提供了完整的路由解決方案,其中包括了組件間的數(shù)據(jù)傳遞功能,通過路由組件傳參,我們可以輕松地在導(dǎo)航到新頁面時傳遞必要的數(shù)據(jù),本文將深入探討如何使用 Vue Router 進(jìn)行路由組件間的傳參,并通過多個示例來展示其實現(xiàn)方式2024-09-09
vue中如何使用embed標(biāo)簽PDF預(yù)覽
這篇文章主要介紹了vue中如何使用embed標(biāo)簽PDF預(yù)覽,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05
Vue創(chuàng)建淺層響應(yīng)式數(shù)據(jù)的實例詳解
這篇文章主要介紹了Vue創(chuàng)建淺層響應(yīng)式數(shù)據(jù)的實例,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11
Vue安裝sass-loader和node-sass版本匹配的報錯問題
這篇文章主要介紹了Vue安裝sass-loader和node-sass版本匹配的報錯問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04

