Reputation: 4896
I have a KendoUI grid bound to ko vm
Due to specific requirements (icons in some columns, links , etc) I need to define the rowTemplate, which I define as ko template.
But I want also to have normal and alternate rows in different bg colors.
Because of this I defined two identical templates as below
<script id="rowTmpl" type="text/html">
<tr role="row" >
<td align="center">
<a data-bind="attr: { href: 'scrccc_checkEdit.aspx?id=' + CheckID }" >
<img src="images/icon-edit.gif" border="0" alt="Edit/View Check" />
</a>
</td>
<td data-bind="text: CheckNumber"></td>
....
</tr>
</script>
<script id="altTmpl" type="text/html">
<tr class="k-alt" role="row">
<td align="center">
<a data-bind="attr: { href: 'scrccc_checkEdit.aspx?id=' + CheckID }" >
<img src="images/icon-edit.gif" border="0" alt="Edit/View Check" />
</a>
</td>
<td data-bind="text: CheckNumber"></td>
....
</tr>
</script>
Basically the two templates are identical, except the alt template have class class="k-alt" applied to table row.
But this approach is very bad, because it duplicates the whole markup for row template.
What is a better way to accomplish what I need?
Thank you
Upvotes: 1
Views: 1685
Reputation: 844
One can also create an AltRow property and IsAltRow() method on the VM and use the css data-bind feature.
var vm = function () {
var self = this;
this.AltRow = true;
this.IsAltRow = function () {
self.AltRow = !self.AltRow;
return self.AltRow;
}
}
<script id="rowTmpl" type="text/html">
<tr data-bind="css: { 'k-alt': $root.IsAltRow() === true }">
<td>
...
</td>
</tr>
</script>
I did also try to use the Computed Property feature of KO but it was throwing errors, not sure why. But this solution worked out fine.
Upvotes: 0
Reputation: 530
I was able to achieve this by using the "JQuery even" selector. I added the code to the databound property of my grid definition. Here's kinda what it looks like.
JAVASCRIPT
var myGridDefinition = {
columns: {...}
dataBound: {
$("#myGridId .k-grid-content tr:even").addClass("k-alt");
}
}
HTML
<div id="myGridId" data-bind="kendoGrid: myGridDefinition"></div>
Upvotes: 1
Reputation: 813
if u only want to change the style of alternative row, you can see sample here: http://jsfiddle.net/P5EQt/
HTML
<div data-bind="kendoGrid: { data: items, rowTemplate: 'rowTmpl', useKOTemplates: true }"> </div>
<button data-bind="click: addItem">Add Item</button>
<script id="rowTmpl" type="text/html">
<tr data-bind="css:{strong:id%2===0}">
<td data-bind="text: id"></td>
<td>
<input data-bind="value: name" />
</td>
<td>
<a href="#" data-bind="click: $root.removeItem">x</a>
</td>
</tr>
</script>
Javascript
var ViewModel = function() {
this.items = ko.observableArray([
{ id: "1", name: ko.observable("apple")},
{ id: "2", name: ko.observable("orange")},
{ id: "3", name: ko.observable("banana")}
]);
this.addItem = function() {
var num = this.items().length + 1;
this.items.push({ id: num, name: "new" + num});
};
this.removeItem = function(item) {
this.items.remove(item);
}.bind(this);
};
ko.applyBindings(new ViewModel());
CSS
.strong { background-color:red; }
Using the knockout css binding with the condition of id (in my sample) that differentiate the alternative row
Upvotes: 2