vue element 中的table動態(tài)渲染實現(xiàn)(動態(tài)表頭)
通過在vue中使用element的table表格,實現(xiàn)數(shù)據(jù)動態(tài)渲染,并且動態(tài)渲染表頭。通過在父組件中引入子組件表格,然后向子組件傳遞表格數(shù)據(jù)和表頭數(shù)據(jù)。
子組件table中template模板
<el-table
:data="this.tableData"
height="400px"
max-height="400px"
size="small"
row-class-name="row"
cell-class-name="column"
:row-style="setRowStyle"
:cell-style="setColumnStyle"
:highlight-current-row="true"
@cell-click="cellClick"
fit
>
<el-table-column
v-for="(item, index) in tableLabel"
:key="index"
:prop="item.prop"
:width="item.width"
:label="item.label">
</el-table-column>
</el-table>
接收父組件傳過來的數(shù)據(jù)
props: {
tableData: { // 父組件傳遞過來的表格數(shù)據(jù)
type: Array,
default: []
},
tableLabel: { // 父組件傳遞過來的表頭數(shù)據(jù)
type: Array,
default: () => {
return []
}
}
}
父組件
<file-table
:tableData="tableData"
:tableLabel="tableLabel"
>
</file-table>
data() {
return {
// 子組件的表格數(shù)據(jù)
tableData: [
{id: 1, date: '2018-07-24', sales: 23.34, sale: 137597.76, const: 102203.71, profit: 35394.05},
{id: 2, date: '2018-07-24', sales: 23.34, sale: 137597.76, const: 102203.71, profit: 35394.05},
{id: 3, date: '2018-07-24', sales: 23.34, sale: 137597.76, const: 102203.71, profit: 35394.05},
{id: 4, date: '2018-07-24', sales: 23.34, sale: 137597.76, const: 102203.71, profit: 35394.05},
{id: 5, date: '2018-07-24', sales: 23.34, sale: 137597.76, const: 102203.71, profit: 35394.05},
{id: 6, date: '2018-07-24', sales: 23.34, sale: 137597.76, const: 102203.71, profit: 35394.05},
{id: 7, date: '2018-07-24', sales: 23.34, sale: 137597.76, const: 102203.71, profit: 35394.05},
{id: 8, date: '2018-07-24', sales: 23.34, sale: 137597.76, const: 102203.71, profit: 35394.05},
{id: 9, date: '2018-07-24', sales: 23.34, sale: 137597.76, const: 102203.71, profit: 35394.05},
{id: 10, date: '2018-07-24', sales: 23.34, sale: 137597.76, const: 102203.71, profit: 35394.05},
{id: 11, date: '2018-07-24', sales: 23.34, sale: 137597.76, const: 102203.71, profit: 35394.05},
{id: 12, date: '2018-07-24', sales: 23.34, sale: 137597.76, const: 102203.71, profit: 35394.05}
],
// 子組件的表頭數(shù)據(jù)
tableLabel: [
{label: '', width: '40', prop: 'id'},
{label: '日期', width: '', prop: 'date'},
{label: '銷售量', width: '', prop: 'sales'},
{label: '銷售額', width: '', prop: 'sale'},
{label: '成本', width: '', prop: 'const'},
{label: '利潤', width: '', prop: 'profit'}
]
}
}
問題:這種方式只能在一個組件中動態(tài)渲染,但是當我們需要操作每一列數(shù)據(jù)的時候,沒法操作,因為element table再帶的方法是每個單元格點擊事件,而不符合我們需求,如何能實現(xiàn)表格動態(tài)渲染,并且每個組件都能使用,還能夠實現(xiàn)操作的可控的列,下節(jié)分享。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
element table跨分頁多選及回顯的實現(xiàn)示例
本文主要介紹了element table跨分頁多選及回顯的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
Vue-router不允許導航到當前位置(/path)錯誤原因以及修復方式
本文主要介紹了Vue-router不允許導航到當前位置(/path)錯誤原因以及修復方式,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09

