Vue中如何動(dòng)態(tài)顯示表格內(nèi)容
在項(xiàng)目中,我們可能會(huì)用到表格來(lái)顯示不同的數(shù)據(jù),表格顯示數(shù)據(jù),無(wú)非是列的內(nèi)容不同,所以,我們可以考慮封裝個(gè)公共的表格組件,動(dòng)態(tài)得顯示不同的數(shù)據(jù)
實(shí)現(xiàn)截圖
如下:
在項(xiàng)目中創(chuàng)建一個(gè)公共表格組件table.vue
代碼如下
<template> <!--這里放其他內(nèi)容,表格等--> <el-table :data="tableData" border style="width: 100%;" v-loading="tableLoading" > <el-table-column align="center" v-for="(th, key) in tableColumnsConfig" :key="key" :prop="th.prop" :label="th.label" :width="th.width" :show-overflow-tooltip="true" > <template slot-scope="scope"> <div v-if="th.prop==''&&th.type=='index'"> {{scope.$index+(cpage - 1) * psize + 1}} </div> <div v-else-if="th.prop==''"> <el-button :type="btn.type=='del'?'danger':'primary'" v-for="(btn,index) in th.operate" size="mini" :key="index" @click="btnHandle(scope.row,btn.type)"> {{btn.name}} </el-button> </div> <div v-else> <span>{{ scope.row[th.prop] }}</span> </div> </template> </el-table-column> </el-table> </template>
<script> export default { name: 'refactor_table', props: { tableData: { type: Array, default: function() { return [] } }, /** * 設(shè)置table 加載icon */ tableLoading: { type: Boolean, default: false }, tableColumnsConfig: { type: Array, default: function() { return [] } } }, data(){ return{ cpage:1, psize:10, } }, mounted(){ /* if(this.tableData && this.tableData.length>0){ this.tableLoading=false; } */ /* console.log(this.tableColumnsConfig); */ }, methods: { btnHandle(row, type) { this.$emit("operateHandle", row, type) } } } </script> <style> </style>
在建一個(gè)父組件
引入子組件table.vue,并把動(dòng)態(tài)獲取的表格數(shù)據(jù)傳給table.vue
<template> <div> <refactor-table :table-data="tableData" :table-columns-config="tableColumns" :table-loading="loading" @operateHandle="tableOperateHandle"> </refactor-table> <pagination :currentPage="currentPage" :limit="limit" :total="total" :small="small"></pagination> </div> </template>
<script> import RefactorTable from './sub/table.vue'; import Pagination from '@/components/Pagination/index.vue' export default { data() { return { loading: false, tableHeight: 300, tableData: [], tableColumns: [ { label: '序號(hào)', width: '50', prop: '', type:"index" }, { label: '節(jié)點(diǎn)id', width: '', prop: 'id' }, { label: '農(nóng)戶名稱(chēng)', width: '', prop: 'name', }, { label: '所屬中心店', width: '', prop: 'grade', }, ], currentPage: 1, limit: 10, total: 0, small: true } }, created() { this.loadingTable(); }, methods: { loadingTable() { // 初始化table 數(shù)據(jù) this.tableData = [{ id: '1938238', name: '節(jié)點(diǎn)', grade: 'ERWFD' }, { id: '3241', name: '節(jié)點(diǎn)B', grade: 'FDD' }, { id: '8238', name: '節(jié)點(diǎn)C', grade: 'FVDFA' }, { id: '3424', name: '節(jié)點(diǎn)', grade: 'ERWFD' }, { id: '32ree', name: '節(jié)點(diǎn)B', grade: 'FDD' }, { id: '821221', name: '節(jié)點(diǎn)C', grade: 'FVDFA' }, { id: '89238', name: '節(jié)點(diǎn)', grade: 'ERWFD' }, { id: '323432', name: '節(jié)點(diǎn)B', grade: 'FDD' }, ]; // 最后增加一列為操作 this.tableColumns.push({ prop: '', label: '操作', width: 280, align: 'center', operate: [ { type: 'add', name: '新增', }, { type: 'del', name: '刪除', }, ] }); }, /** * 接收table 組件操作時(shí)傳入的參數(shù) * @param row {object} 所選行 * @param type {String} 操作類(lèi)型(add,del) */ tableOperateHandle(row, type) { console.log(row, type) } }, components: { RefactorTable, Pagination }, //接收子組件值 handleCurrentChange(cpage) { this.currentPage = cpage; }, handleSizeChang(psize) { this.limit = psize; } } </script>
然后就可以實(shí)現(xiàn)表格內(nèi)容動(dòng)態(tài)顯示啦~
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
axios向后臺(tái)傳遞數(shù)組作為參數(shù)的方法
今天小編就為大家分享一篇axios向后臺(tái)傳遞數(shù)組作為參數(shù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08vant之關(guān)于van-list的使用以及一些坑的解決方案
這篇文章主要介紹了vant之關(guān)于van-list的使用以及一些坑的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06vue?模板循環(huán)繪制多行上傳文件功能實(shí)現(xiàn)
這篇文章主要為大家介紹了vue?模板循環(huán)繪制多行上傳文件功能實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07VueJS 組件參數(shù)名命名與組件屬性轉(zhuǎn)化問(wèn)題
這篇文章主要介紹了VueJS 組件參數(shù)名命名與組件屬性轉(zhuǎn)化問(wèn)題,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12React組件通信之路由傳參(react-router-dom)
本文主要介紹了React組件通信之路由傳參(react-router-dom),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10element?ui富文本編輯器的使用效果與步驟(quill-editor)
富文本編輯器在任何項(xiàng)目中都會(huì)用到,在Element中我們推薦vue-quill-editor組件,下面這篇文章主要給大家介紹了關(guān)于element?ui富文本編輯器的使用效果與步驟(quill-editor)的相關(guān)資料,需要的朋友可以參考下2022-10-10vuejs 切換導(dǎo)航條高亮(路由菜單高亮)的方法示例
這篇文章主要介紹了vuejs 切換導(dǎo)航條高亮路由高亮的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05