亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

vueJS 對嵌套表行進行排序

vueJS 對嵌套表行進行排序

慕娘9325324 2023-08-24 15:57:12
我有一個像這樣的 json 對象列表(載體):在我的 *.vue 中,我將其渲染為:<tr v-for="carrier in this.carriers">   <td>{{ carrier.id }}</td> ....可以單擊我的 thead / th's 對表進行排序,如下所示:<thead>   <tr>      <th class="pointer link" @click="sort('id')">         ID         <span v-if="'id' === currentSortCol">            {{currentSortDir ==='asc' ? '&#8593;':'&#8595;'}}         </span>      </th>      <th class="pointer link" @click="sort('region.name')">         Region         <span v-if="'region.name' === currentSortCol">            {{currentSortDir ==='asc' ? '&#8593;':'&#8595;'}}         </span>      </th>....我的排序方法是這樣的:        sort(col) {            if (this.currentSortCol === col) {                this.currentSortDir = this.currentSortDir === "asc" ? "desc" : "asc";            } else {                this.currentSortCol = col;            }            this.carriers.sort(this.sortBy(col, this.currentSortDir));        },        sortBy(property, order) {            this.currnetSortDir=order;            return function(a, b) {                const varA =                    typeof a[property] === "string"                        ? a[property].toUpperCase()                        : a[property];                const varB =                    typeof b[property] === "string"                        ? b[property].toUpperCase()                        : b[property];                let comparison = 0;                if (varA > varB) comparison = 1;                else if (varA < varB) comparison = -1;                return order === "desc" ? comparison * -1 : comparison;            };        }問題: 排序 asc, desc 與 ID (Carrier.id) 配合良好。但它不會對嵌套的 Carrier.region.name 列進行排序。如何對嵌套列進行排序,例如載體.區域.名稱?
查看完整描述

1 回答

?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

您應該使用包中的“get”方法lodash來獲取嵌套的道具:


import _get from 'lodash/get'

...

const propValueA = _get(a, property);

const propValueB = _get(b, property);

const varA = typeof propValueA === "string" ? propValueA.toUpperCase() : propValueA;

const varB = typeof propValueB === "string" ? propValueB.toUpperCase() : propValueB;


查看完整回答
反對 回復 2023-08-24
  • 1 回答
  • 0 關注
  • 205 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號