vue中el-table格式化el-table-column內(nèi)容的三種方法
el-table格式化el-table-column內(nèi)容
遇到一個(gè)需求,一個(gè)循環(huán)展示的table中的某項(xiàng),或者某幾項(xiàng)需要格式化。對(duì)于格式化的方法,主要有template scope、formatter;
一、template scope 、v-if判斷
<el-table-column prop="cyxb" label="性別">
<template slot-scope="scope">
<span v-if="scope.row.cyxb == 0">男</span>
<span v-if="scope.row.cyxb == 1">女</span>
</template>
</el-table-column>

二、利用formatter、slot屬性
查看幫助文檔

<el-table-column prop="xb1" label="成員性別1" width="120" :formatter="Formatter">
Formatter(row, column){
if(row.xb == 0){
return "男"
}else if(row.xb == 1){
return "女"
}
}

三、但這些對(duì)我當(dāng)前的情況,并不適用。所以,后來發(fā)現(xiàn)一個(gè)好方法。將兩種方法結(jié)合起來,使用slot,自定義 formatter.(自定義)靈活應(yīng)用就好啦??
<el-table-column
v-for="column in cbdksTableColumns"
:prop="column.field"
:label="column.label"
sortable="custom"
:key="column.field"
min-width="200"
>
<template slot-scope="scope">
<div v-if="column.field == 'cyxb'">
<span v-html="xbFormatter(scope.row.cyxb, scope.column.property)"></span>
//將表格數(shù)據(jù)格式化后,再用 template + v-html 展示出來
</div>
//<div v-else-if="column.field == 'qqfs'">中間還可以加好多判斷,從此針對(duì)某列的值進(jìn)行格式化。
<div v-else>
{{ scope.row[scope.column.property] }}//千萬不要忘啦?。?!
</div>
</template>
</el-table-column>
//之前的代碼取數(shù)據(jù)比較復(fù)雜,簡(jiǎn)化代碼,便于理解。
xbFormatter(value, row) {
//性別
let cyxbvalue = value;
if (cyxbvalue == null || cyxbvalue == "" || cyxbvalue == undefined) {
return cyxbvalue;
} else {
let dycyxb = this.xbOptions.filter((item) => item.value === cyxbvalue);//filter過濾方法(看自己的情況、需求)
return dycyxb[0].label;//rerun的內(nèi)容即為要在表格中顯示的內(nèi)容
}
},
此處xbOptions是調(diào)用后臺(tái)接口返回的數(shù)據(jù),組織結(jié)構(gòu)為
this.xbOptions.push({ label: mj.mjmc, value: mj.mjz });
返回結(jié)果

當(dāng)然xbOptions也可直接在data中靜態(tài)定義。也可不定義,直接在return返回想要顯示的內(nèi)容也可。
當(dāng)然這個(gè)方法中,不僅僅if語句,自行判斷的語句都在這,判斷完返回結(jié)果就歐克了。
文章就寫到這了,多多運(yùn)用就明明白白啦??。
博文參考:
http://www.dhdzp.com/article/259218.htm
https://blog.csdn.net/chenmi8205/article/details/100626570
到此這篇關(guān)于vue中el-table格式化el-table-column內(nèi)容的三種方法的文章就介紹到這了,更多相關(guān)el-table格式化el-table-column內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue2實(shí)現(xiàn)搜索結(jié)果中的搜索關(guān)鍵字高亮的代碼
這篇文章主要介紹了vue2實(shí)現(xiàn)搜索結(jié)果中的搜索關(guān)鍵字高亮的代碼,需要的朋友可以參考下2018-08-08
Vue-drag-resize 拖拽縮放插件的使用(簡(jiǎn)單示例)
本文通過代碼給大家介紹了Vue-drag-resize 拖拽縮放插件使用簡(jiǎn)單示例,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
如何使用Vuex+Vue.js構(gòu)建單頁應(yīng)用
這篇文章主要教大家如何使用Vuex+Vue.js構(gòu)建單頁應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
vue使用eventBus遇到數(shù)據(jù)不更新的問題及解決
這篇文章主要介紹了vue使用eventBus遇到數(shù)據(jù)不更新的問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08

