您好,登錄后才能下訂單哦!
通過在vue中使用element的table表格,實現數據動態渲染,并且動態渲染表頭。通過在父組件中引入子組件表格,然后向子組件傳遞表格數據和表頭數據。
子組件table中template模板
<el-table :data="this.tableData" height="400px" max-height="400px" size="small" row-class-name="row" cell-class-name="column" :row- :cell- :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>
接收父組件傳過來的數據
props: { tableData: { // 父組件傳遞過來的表格數據 type: Array, default: [] }, tableLabel: { // 父組件傳遞過來的表頭數據 type: Array, default: () => { return [] } } }
父組件
<file-table :tableData="tableData" :tableLabel="tableLabel" > </file-table> data() { return { // 子組件的表格數據 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} ], // 子組件的表頭數據 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'} ] } }
問題:這種方式只能在一個組件中動態渲染,但是當我們需要操作每一列數據的時候,沒法操作,因為element table再帶的方法是每個單元格點擊事件,而不符合我們需求,如何能實現表格動態渲染,并且每個組件都能使用,還能夠實現操作的可控的列,下節分享。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。