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.dbjr.com.cn/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)文章
Vue項(xiàng)目之安裝引入使用vconsole方式(生產(chǎn)環(huán)境不顯示)
在Vue2開發(fā)中,引入vConsole可以為移動(dòng)端提供類似瀏覽器F12的調(diào)試工具,支持查看日志、網(wǎng)絡(luò)請(qǐng)求等功能,vConsole是一個(gè)輕量、可拓展的前端調(diào)試面板,與框架無關(guān),適用于多種前端框架,安裝方法包括npm和CDN兩種,可根據(jù)項(xiàng)目環(huán)境配置是否顯示調(diào)試面板2024-10-10
Vuex持久化插件(vuex-persistedstate)解決刷新數(shù)據(jù)消失的問題
這篇文章主要介紹了Vuex持久化插件(vuex-persistedstate)-解決刷新數(shù)據(jù)消失的問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04
vue3.0 CLI - 2.6 - 組件的復(fù)用入門教程
這篇文章主要介紹了 vue3.0 CLI - 2.6 - 組件的復(fù)用,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2018-09-09
Vue組件全局注冊(cè)實(shí)現(xiàn)警告框的實(shí)例詳解
這篇文章主要介紹了Vue組件全局注冊(cè)實(shí)現(xiàn)警告框的實(shí)例,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06

