vue如何將后臺返回的數(shù)字轉(zhuǎn)換成對應(yīng)的文字
更新時間:2022年10月25日 10:25:21 作者:small面包
這篇文章主要介紹了vue如何將后臺返回的數(shù)字轉(zhuǎn)換成對應(yīng)的文字,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
將后臺返回的數(shù)字轉(zhuǎn)換成對應(yīng)的文字
今天遇到一個問題就是性別一欄后臺返回我的是數(shù)字,但是前端展示的是漢字男女,而且0,1,2,對應(yīng)不同的漢字
下面跟大家分享一下我的方法
首先用map遍歷后臺返回的數(shù)據(jù)利用里面的回調(diào)對數(shù)據(jù)進(jìn)行解析即可,下面直接上代碼
?? ??? ?this.usersData.map(function (val) {
? ? ? ? ? if (val.gender == 0) {
? ? ? ? ? ? val.gender = '人妖'
? ? ? ? ? } else if (val.gender == 1) {
? ? ? ? ? ? val.gender = '男'
? ? ? ? ? } else if (val.gender == 2) {
? ? ? ? ? ? val.gender = '女'
? ? ? ? ? } else {
? ? ? ? ? ? return
? ? ? ? ? }
? ? ? ? })這樣就可以直接在頁面中顯示了~
還有一種方式
<el-table
:data="tableData"
border>
?? ?<el-table-column
? ? ? ? prop="status"
? ? ? ? :show-overflow-tooltip="true"
? ? ? ? label="狀態(tài)"
? ? ? ? width="60"
? ? ? ? :formatter="statusFormatter"
? ? ? >
? ? ?</el-table-column>
</el-table>
<script>
?? ?export default{
?? ??? ?data(){
?? ??? ??? ?return{
?? ??? ??? ??? ?tableData:[]
?? ??? ??? ?}
?? ??? ?},
?? ??? ?methods:{
?? ??? ??? ?statusFormatter(row, column){
?? ??? ??? ??? ?const status = row.status
? ? ? ?? ??? ??? ?if (status == 0) {
? ? ? ? ?? ??? ??? ?return '正常'
? ? ? ?? ??? ??? ?} else if (status == 1) {
? ? ? ? ?? ??? ??? ?return '待審批'
? ? ? ?? ??? ??? ?} else if (status == 2) {
? ? ? ? ?? ??? ??? ?return '拒絕'
? ? ? ?? ??? ??? ?} else if (status == 3) {
?? ??? ??? ? ? ? ? ?return '鎖定'
? ? ? ?? ??? ??? ?} else {
? ? ? ? ?? ??? ??? ?return '刪除'
? ? ? ?? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ?}
</script>vue數(shù)字轉(zhuǎn)漢字
Vue中將阿里伯?dāng)?shù)字轉(zhuǎn)換為中文表示,一般用作排名使用。目前支持兩位數(shù)字轉(zhuǎn)換。
let toChinese=function(val){
let chin_list=['一','二','三','四','五','六','七','八','九','十'];//所有的數(shù)值對應(yīng)的漢字
let sn = parseInt(val)+1;//這里由于我的后臺是從0開始排序
if(sn<=10){
return chin_list[sn-1];
}
else if(sn<=100){
if(sn<20)
return '十'+chin_list[sn%10-1];
if(sn%10==0)
return chin_list[Math.floor(sn/10)-1]+'十';
else
return chin_list[Math.floor(sn/10)-1]+'十'+chin_list[sn%10-1];
}
else{
//可以支持更多
}
}
效果如圖

html代碼如下
<div
v-for="(item,index) in ticketsList"
:key="index"
class="button-info">
<span class="passenger-title passenger-padding">航段{{ toChinese(index) }}</span>
</div>
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vuex新手進(jìn)階篇之改變state?mutations的使用
在vue的項(xiàng)目中不可避免的會使用到vuex用于數(shù)據(jù)的存儲,下面這篇文章主要給大家介紹了關(guān)于vuex新手進(jìn)階篇之改變state?mutations的使用,文中通過圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10
vue.js將時間戳轉(zhuǎn)化為日期格式的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue.js將時間戳轉(zhuǎn)化為日期格式的實(shí)現(xiàn)代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06

