I'm looking at using KoGrid and it took me a while to figure out how to use observable properties. I found the wiki page about custom templates helpful and that I have to use $parent.entity['editableField']
. However, I can't get the css binding to work.
Please see my plunker or a copy below which is modified from their example. I can bet the name and age to change as an observable, but the first row, column 2 should be green (age > 30).
function stringCellTemplateVM() {
var self = this;
this.selectedItems = ko.observableArray([]);
this.myData = ko.observableArray([{ name: ko.observable("Moroni"), age: ko.observable(50) },
{ name: "Tiancum", age: 43 },
{ name: "Jacob", age: 27 },
{ name: "Nephi", age: 29 },
{ name: "Enos", age: 34 }]);
this.gridOptions = {
data: self.myData,
selectedItems: self.selectedItems,
multiSelect: false,
columnDefs: [{field: 'name', displayName: 'Name'},
{field: 'age', displayName: 'Age', cellTemplate:'<div data-bind="attr: { \'class\': \'kgCellText colt\' + $index()},css: { green: $parent.entity[\'age\'] > 30 }, html: $parent.entity[\'age\']"></div>'}]
};
this.increaseAge = function(){
this.myData()[0].age(this.myData()[0].age()+1);
//alert(this.myData()[0].age());
//this.myData()[0].name(this.myData()[0].name() + this.myData()[0].age());
};
}
var vm = new stringCellTemplateVM();
ko.applyBindings(vm);
- Is this the right way to work with observable properties?
- How can I get the css binding to work to change that to green when that requirement is met?