如何解決vueelementUI中table里數(shù)字、字母、中文混合排序問題

小編給大家分享一下如何解決vue elementUI中table里數(shù)字、字母、中文混合排序問題,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務領(lǐng)域包括:成都網(wǎng)站設計、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的中衛(wèi)網(wǎng)站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設合作伙伴!

1.使用場景

使用elementUI中的table時,給包含數(shù)字字母中文的名稱等字段排序

例如:數(shù)字(0->9)->大寫字母(A->Z)->小寫字母(a->z)->中文拼音(a->z)

2.代碼解析

<el-table
   ref="multipleTable"
   border
   tooltip-effect="dark"
   class="xg-table"
   
   max-height="600">
   <el-table-column
    type="selection"
    width="60" />
   <el-table-column
    :default-sort = "{prop: 'DevName'}"
    :sort-method="sortDevName"
    prop="DevName"
    label="名稱"
    sortable
    show-overflow-tooltip />
</el-table>

設置屬性sortable,會按照自帶的機制排序,不符合我們的預期;

所以增加屬性 sort-method,在方法中自定義排序方式

<script>
  export default {
    methods: {
      sortDevName(str1, str2) {
       let res = 0
       for (let i = 0; ;i++) {
  if (!str1[i] || !str2[i]) {
   res = str1.length - str2.length
   break
  }
  const char1 = str1[i]
  const char1Type = this.getChartType(char1)
  const char2 = str2[i]
  const char2Type = this.getChartType(char2)
  // 類型相同的逐個比較字符
  if (char1Type[0] === char2Type[0]) {
   if (char1 === char2) {
   continue
   } else {
   if (char1Type[0] === 'zh') {
    res = char1.localeCompare(char2)
   } else if (char1Type[0] === 'en') {
    res = char1.charCodeAt(0) - char2.charCodeAt(0)
   } else {
    res = char1 - char2
   }
   break
   }
  } else {
  // 類型不同的,直接用返回的數(shù)字相減
   res = char1Type[1] - char2Type[1]
   break
  }
   }
   return res
  },
  getChartType(char) {
  // 數(shù)字可按照排序的要求進行自定義,我這邊產(chǎn)品的要求是
  // 數(shù)字(0->9)->大寫字母(A->Z)->小寫字母(a->z)->中文拼音(a->z)
   if (/^[\u4e00-\u9fa5]$/.test(char)) {
  return ['zh', 300]
   }
   if (/^[a-zA-Z]$/.test(char)) {
  return ['en', 200]
   }
   if (/^[0-9]$/.test(char)) {
  return ['number', 100]
   }
   return ['others', 999]
  }
    }
  }
</script>

3.頁面效果

 原列表                   ==》》            正序                 ==》》         倒序

如何解決vue elementUI中table里數(shù)字、字母、中文混合排序問題如何解決vue elementUI中table里數(shù)字、字母、中文混合排序問題如何解決vue elementUI中table里數(shù)字、字母、中文混合排序問題

以上是“如何解決vue elementUI中table里數(shù)字、字母、中文混合排序問題”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

本文名稱:如何解決vueelementUI中table里數(shù)字、字母、中文混合排序問題
本文路徑:http://www.muchs.cn/article44/ijchhe.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供微信公眾號、Google、ChatGPT營銷型網(wǎng)站建設、動態(tài)網(wǎng)站、小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設