簡體   English   中英

在索引之后刪除Knockout observable數組

[英]Knockout observable array remove after index

我正在尋找一種方法來淘汰去除一個可觀察數組中的所有元素,這些元素來自一個給定的索引。

執行此操作的for循環效率低下,因為一次刪除一個元素會觸發每次刪除時的更改通知。 有沒有開箱即用的東西?

試試這個(注意HowMany是可選的,如果你沒有指定它,將刪除StartIndex之后的所有項目):

myObservableArray.splice(StartIndex, HowMany)

如果需要刪除具有某些屬性的項,可以將一個返回布爾值的函數傳遞給knockout的remove函數,例如:

myObservableArray.remove(function(item) { return item.property > YourValue })

+來自淘汰文檔的引用: LINK

Normally, an observableArray notifies its subscribers immediately, as soon as it’s 
changed. But if an observableArray is changed repeatedly or triggers expensive updates,
you may get better performance by limiting or delaying change notifications. This is
accomplished using the rateLimit extender like this:

// Ensure it notifies about changes no more than once per 50-millisecond period
myViewModel.myObservableArray.extend({ rateLimit: 50 });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM