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 is a editable column ... if the age is changed the next column should compute the value of 100 - the updated age ...
I have created a jsfiddle to demo my requirement here JSFiddle
{field: 'computedAge', displayName: '100 -Age',cellTemplate:self.calculatedCellTemplate}
is the computed column that i wish to populate when the age column is updated
Can anyone suggest how i could achieve this ?
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/