我有一個(gè)像這樣的 json 對(duì)象列表(載體):在我的 *.vue 中,我將其渲染為:<tr v-for="carrier in this.carriers"> <td>{{ carrier.id }}</td> ....可以單擊我的 thead / th's 對(duì)表進(jìn)行排序,如下所示:<thead> <tr> <th class="pointer link" @click="sort('id')"> ID <span v-if="'id' === currentSortCol"> {{currentSortDir ==='asc' ? '↑':'↓'}} </span> </th> <th class="pointer link" @click="sort('region.name')"> Region <span v-if="'region.name' === currentSortCol"> {{currentSortDir ==='asc' ? '↑':'↓'}} </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) 配合良好。但它不會(huì)對(duì)嵌套的 Carrier.region.name 列進(jìn)行排序。如何對(duì)嵌套列進(jìn)行排序,例如載體.區(qū)域.名稱?
vueJS 對(duì)嵌套表行進(jìn)行排序
慕娘9325324
2023-08-24 15:57:12