vue如何將后臺(tái)返回的數(shù)字轉(zhuǎn)換成對(duì)應(yīng)的文字
將后臺(tái)返回的數(shù)字轉(zhuǎn)換成對(duì)應(yīng)的文字
今天遇到一個(gè)問(wèn)題就是性別一欄后臺(tái)返回我的是數(shù)字,但是前端展示的是漢字男女,而且0,1,2,對(duì)應(yīng)不同的漢字
下面跟大家分享一下我的方法
首先用map遍歷后臺(tái)返回的數(shù)據(jù)利用里面的回調(diào)對(duì)數(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
? ? ? ? ? }
? ? ? ? })這樣就可以直接在頁(yè)面中顯示了~
還有一種方式
<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ù)值對(duì)應(yīng)的漢字
let sn = parseInt(val)+1;//這里由于我的后臺(tái)是從0開(kāi)始排序
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>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vuex新手進(jìn)階篇之改變state?mutations的使用
在vue的項(xiàng)目中不可避免的會(huì)使用到vuex用于數(shù)據(jù)的存儲(chǔ),下面這篇文章主要給大家介紹了關(guān)于vuex新手進(jìn)階篇之改變state?mutations的使用,文中通過(guò)圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10
Vue3手動(dòng)清理keep-alive組件緩存的方法詳解
這篇文章主要為大家詳細(xì)介紹了Vue3中手動(dòng)清理keep-alive組件緩存的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04
vue.js將時(shí)間戳轉(zhuǎn)化為日期格式的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue.js將時(shí)間戳轉(zhuǎn)化為日期格式的實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
vue使用element-ui按需引入時(shí)踩過(guò)的那些坑
Element-UI是基于vue實(shí)現(xiàn)的一套不依賴業(yè)務(wù)的UI組件庫(kù),提供了豐富的PC端組件,減少用戶對(duì)常用組件的封裝,降低了開(kāi)發(fā)的難易程度,下面這篇文章主要給大家介紹了關(guān)于vue使用element-ui按需引入時(shí)踩過(guò)的那些坑,需要的朋友可以參考下2022-05-05

