第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

基于排序數(shù)組更新組件。角 (4.3)

基于排序數(shù)組更新組件。角 (4.3)

慕田峪9158850 2021-08-20 17:59:38
使用我們希望能夠排序以在組件中顯示的數(shù)據(jù)數(shù)組,并且它似乎沒有排序或更新 DOM,但是我有一個(gè)正確演示該概念的工作代碼示例,它應(yīng)該正在排序,但在 angular 應(yīng)用程序中,它根本沒有被排序。存儲(chǔ)原始數(shù)據(jù)的父組件將數(shù)據(jù)存儲(chǔ)在名為 的輸入?yún)?shù)對(duì)象上Batch,我們正在排序的數(shù)組位于 上Batch.Invoices.Results。來自子組件的事件沒問題,確認(rèn)合適的數(shù)據(jù)冒泡到父組件。應(yīng)該對(duì)數(shù)組進(jìn)行排序的函數(shù)如下所示: public OnInvoiceSortChange({orderValue, orderAscending}){        console.log(`Invoice Sorting has been called. Value: ${orderValue} . Ascending? ${orderAscending}`);        console.log(`Before:`);        console.log(this.BatchViewModel.Invoices.Results.map(x => x.VendorName));        const sortingArray = [...this.BatchViewModel.Invoices.Results];        if(orderAscending){            const sorted = sortingArray.sort((a, b) => a[orderValue] > b[orderValue] ? 1 : 0);            this.BatchViewModel.Invoices.Results = sorted;            console.log('Sorted');            console.log(sorted.map(x => x.VendorName));        } else {            const sorted = sortingArray.sort((a, b) => a[orderValue] < b[orderValue] ? 1 : 0);            this.BatchViewModel.Invoices.Results = sorted;            console.log(sorted.map(x => x.VendorName));        }        console.log(`After:`);        console.log(this.BatchViewModel.Invoices.Results.map(x => x.VendorName));    }所有控制臺(tái)日志都用于調(diào)試器可見性,輸出如下:在我的測試文件(非角度)中的哪個(gè)位置看起來像這樣:(哪里data是來自 Angular 應(yīng)用程序的數(shù)組的直接副本。const ascendingData = [...data];const descendingData = [...data];const sortedDescending = descendingData.sort((a, b) => a['VendorName'] < b['VendorName']? 0 : 1)const sortedAscending = ascendingData.sort((a, b) => a['VendorName'] > b['VendorName']? 0 : 1);const vendorListAscending = sortedAscending.map(x => x.VendorName);const vendorListDescending = sortedDescending.map(x => x.VendorName);console.log(vendorListDescending);console.log(vendorListAscending);輸出如下所示:所以我看到排序應(yīng)該起作用,但它在 Angular 中沒有發(fā)生。如何對(duì)數(shù)組進(jìn)行排序,并同時(shí)更新 DOM?
查看完整描述

1 回答

?
精慕HU

TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個(gè)贊

您傳遞給的函數(shù)sort是錯(cuò)誤的。它應(yīng)該為“更少”返回負(fù)值,為“更大”返回正值或?yàn)椤跋嗟取狈祷亓?。如?code>orderValue是數(shù)字,則最容易返回a[orderValue] - b[orderValue],如果不是,則只需將您的更改0-1.

(順便說一下,名字orderKey可能會(huì)更清楚一點(diǎn)?)

我認(rèn)為 angular 在這里沒有任何關(guān)系,但我現(xiàn)在不知道為什么你會(huì)得到不同的結(jié)果。無論如何,您的排序函數(shù)無效(它指出a等于b,但同時(shí)b大于a),我希望修復(fù)此函數(shù)會(huì)有所幫助。


查看完整回答
反對(duì) 回復(fù) 2021-08-20
  • 1 回答
  • 0 關(guān)注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)