Knockout JS - Modifying properties of items in observableArray

PercivalMcGullicuddy

I am trying to update the umber shown in red to reflect the positions of each item in an observableArray when one item has been repositioned. You can see my current code here: http://jsfiddle.net/BV87N/

It's not quite behaving the way I would expect it to. I have a feeling it's because the items and their properties inside of the array are not observable themselves.

But I'm not quite sure how to get this to work.

   ko.bindingHandlers.sortable.afterMove = function () {
    self.adjustOrder();
};

self.adjustOrder = function () {
    for (var i = 0, j = self.items().length; i < j; i++) {
        self.items()[i].sortOrder = i;
    };
};
nemesv

This is very explicitly stated in the documentation of observable Arrays

Key point: An observableArray tracks which objects are in the array, not the state of those objects

Simply putting an object into an observableArray doesn’t make all of that object’s properties themselves observable. Of course, you can make those properties observable if you wish, but that’s an independent choice.

So you need to make your sortOrder properties ko.observable() (there is a plugin called Knockout Mapping which could help in that) and then change your adjustOrder to

self.adjustOrder = function () {
    for (var i = 0, j = self.items().length; i < j; i++) {
        self.items()[i].sortOrder(i);
    };
};

Demo JSFiddle.

SideNote: But in your case you don't really need any on your sortOrder properties because the order of the items in the items is the sort order. So in your bindings you can use just $index (binding context property) instead of sortOrder.

Demo JSFiddle.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Knockout Modifying ObservableArray inside Computed

From Dev

Knockout Modifying ObservableArray inside Computed

From Dev

knockout.js validation on observableArray to require all items (no empty fields)

From Dev

knockout.js: No concat on observableArray

From Dev

Custom filtering knockout js observableArray

From Dev

Knockout.js contenteditable observablearray

From Dev

knockout.js: No concat on observableArray

From Dev

Update ObservableArray - Knockout.js

From Dev

Knockout JS failed to update observableArray

From Dev

Knockout js observableArray is not getting updated

From Dev

Knockout observableArray not updating all properties on checked value

From Dev

Knockout.js - Registering to change in observableArray

From Dev

Knockout.js - Registering to change in observableArray

From Dev

knockout.js observableArray computed value

From Dev

knockout.js observableArray changes not reflecting in UI

From Dev

Sorting observableArray with added item Knockout JS

From Dev

Can not find index form observablearray knockout.js

From Dev

Knockout: Update item in an observableArray

From Dev

Knockout not create and observableArray

From Dev

Filtering an observableArray in Knockout

From Dev

Knockout observableArray is not binding correctly

From Dev

Knockout observableArray push not working

From Dev

Load Knockout observableArray not working

From Dev

Knockout observableArray not updating UI

From Dev

Modifying value of observable values in observableArray

From Dev

knockout js observable array removing all items

From Dev

Knockout.js foreach on list of items not displaying

From Dev

Reorder items in a list using Knockout JS

From Dev

Implement knockout.js localization for json items

Related Related

HotTag

Archive