Skip to content Skip to sidebar Skip to footer

Computed Values In Knockout KoGrid

I need to use a grid in which one column has a computed value based on values from other columns in the grid ... As a sample requirement I have one column which shows the age this

Solution 1:

You can achieve this by only two steps:

(1) Create instantiation function for every item in your datalist with computedAge computed property within it.

function Item(data) {
    this.name = ko.observable(data.name);
    this.age = ko.observable(data.age);
    this.computedAge = ko.computed(function(){
        return 100 - this.age();
    }, this);
}

(2) Map source array to create instances instead of simple observableArray creation.

self.browsers = ko.observableArray(
    ko.utils.arrayMap(
        datastore.initialData,
        function(data){ return new Item(data); }
    )
);

Working example: http://jsfiddle.net/xp6xa/

Update: To get self-updating cell do not forget to define your self.calculatedCellTemplate like this:

    self.calculatedCellTemplate = '<span data-bind="text: $parent.entity.computedAge"></span>';

http://jsfiddle.net/xp6xa/3/


Post a Comment for "Computed Values In Knockout KoGrid"