knockoutjs ObservableArrays and sort function: UI is not updated

妖精的绣舞 提交于 2019-12-09 12:11:46

问题


In my viewmodel, I have a knockoutjs ObserableArray. Just after I initialized the ViewModel, it binds the data successfully. Then, what I need to do is to sort the collection.

$.each(vm.searchResults(), function (i, property) {
    console.log(property.Name());
});

vm.searchResults().sort(function (a, b) {
    return a.Name().toLowerCase() > b.Name().toLowerCase() ? 1 : -1;
});

$.each(vm.searchResults(), function (i, property) {
    console.log(property.Name());
});

As you can see, I output the Name of the element to the console to see the order before and after the sorting. The sorting works just fine. The problem is with the UI update. Somehow, the UI is not updated.

Then, try to remove a record from the array with the following code to see if the UI will respond to that or not:

vm.searchResults().shift();

The UI stays the the same and didn't update again. What would be the problem here?

Edit:

Here is a sample case as well: http://jsfiddle.net/tugberk/KLpwP/

The same problem here as well.

Edit:

I solved the problem as shown in this sample: http://jsfiddle.net/tugberk/KLpwP/16/ But I am still not sure why it worked as I tried at first.


回答1:


You're unwrapping observable array when you are going to sort it. This is causes problem, because KO can't track array was changed. ko.observableArray() has sort function with same signature and it will notify observable's subscribers that it has been changed. Solution is very simple: remove braces vm.searchResults().sort => vm.searchResults.sort. Checkout my example: http://jsfiddle.net/RbX86/

Another way to tell KO that array has changes - call valueHasMutated method after manipulations with array. Please review this sample: http://jsfiddle.net/RbX86/1/ This approach is very good when you need to make many changes in your array and you want to refresh UI only one time.



来源:https://stackoverflow.com/questions/10480118/knockoutjs-observablearrays-and-sort-function-ui-is-not-updated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!