Vue中如何動態(tài)顯示表格內(nèi)容
在項目中,我們可能會用到表格來顯示不同的數(shù)據(jù),表格顯示數(shù)據(jù),無非是列的內(nèi)容不同,所以,我們可以考慮封裝個公共的表格組件,動態(tài)得顯示不同的數(shù)據(jù)
實現(xiàn)截圖
如下:

在項目中創(chuàng)建一個公共表格組件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>
在建一個父組件
引入子組件table.vue,并把動態(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: '序號',
width: '50',
prop: '',
type:"index"
},
{
label: '節(jié)點id',
width: '',
prop: 'id'
},
{
label: '農(nó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é)點',
grade: 'ERWFD'
},
{
id: '3241',
name: '節(jié)點B',
grade: 'FDD'
},
{
id: '8238',
name: '節(jié)點C',
grade: 'FVDFA'
},
{
id: '3424',
name: '節(jié)點',
grade: 'ERWFD'
},
{
id: '32ree',
name: '節(jié)點B',
grade: 'FDD'
},
{
id: '821221',
name: '節(jié)點C',
grade: 'FVDFA'
},
{
id: '89238',
name: '節(jié)點',
grade: 'ERWFD'
},
{
id: '323432',
name: '節(jié)點B',
grade: 'FDD'
},
];
// 最后增加一列為操作
this.tableColumns.push({
prop: '',
label: '操作',
width: 280,
align: 'center',
operate: [
{
type: 'add',
name: '新增',
},
{
type: 'del',
name: '刪除',
},
]
});
},
/**
* 接收table 組件操作時傳入的參數(shù)
* @param row {object} 所選行
* @param type {String} 操作類型(add,del)
*/
tableOperateHandle(row, type) {
console.log(row, type)
}
},
components: {
RefactorTable,
Pagination
},
//接收子組件值
handleCurrentChange(cpage) {
this.currentPage = cpage;
},
handleSizeChang(psize) {
this.limit = psize;
}
}
</script>
然后就可以實現(xiàn)表格內(nèi)容動態(tài)顯示啦~
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3+ElementUI 多選框中復(fù)選框和名字點擊方法效果分離方法
這篇文章主要介紹了Vue3+ElementUI 多選框中復(fù)選框和名字點擊方法效果分離方法,文中補充介紹了VUE-Element組件 CheckBox多選框使用方法,需要的朋友可以參考下2024-01-01
Vue實現(xiàn)組件通信的8種實戰(zhàn)方案詳解
組件與組件之間的數(shù)據(jù)傳遞組件的數(shù)據(jù)是獨立的,無法直接訪問其他組件的數(shù)據(jù),通過組件通信,可以訪問其他組件的數(shù)據(jù),本文給大家介紹了Vue實現(xiàn)組件通信的8種實戰(zhàn)方案,需要的朋友可以參考下2025-08-08
Vue 2閱讀理解之initRender與callHook組件詳解
這篇文章主要為大家介紹了Vue 2閱讀理解之initRender與callHook組件詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08
vuex(vue狀態(tài)管理)的特殊應(yīng)用案例分享
Vuex 是一個專為 Vue.js 應(yīng)用程序開發(fā)的狀態(tài)管理模式。它采用集中式存儲管理應(yīng)用的所有組件的狀態(tài),并以相應(yīng)的規(guī)則保證狀態(tài)以一種可預(yù)測的方式發(fā)生變化。2020-03-03
vue?watch監(jiān)聽觸發(fā)優(yōu)化搜索框的性能防抖節(jié)流的比較
這篇文章主要為大家介紹了vue?watch監(jiān)聽觸發(fā)優(yōu)化搜索框的性能防抖節(jié)流的比較,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
Vue2?與?Vue3?的數(shù)據(jù)綁定原理及實現(xiàn)
這篇文章主要介紹了Vue2與Vue3的數(shù)據(jù)綁定原理及實現(xiàn),數(shù)據(jù)綁定是一種把用戶界面元素的屬性綁定到特定對象上面并使其同步的機制,使開發(fā)人員免于編寫同步視圖模型和視圖的邏輯2022-09-09

