You said you did not want to use templates, but I think you were talking about column templates.
You can change the markup of td itself using the line pattern:
<script id="template" type="text/x-kendo-template"> <tr data-uid="#= uid #"> # this.columns.forEach(function(col) { var val = data[col.field], css, style = '' cClasses = ''; if (typeof col.attributes === 'function') { css = col.attributes(data); cClasses = css["class"]; style = css.style } # <td class='#= cClasses #' style='#= style #'> #= data[col.field] # </td> # }) # </tr> </script>
For the loop to work, you need to bind your template to the grid, though:
var grid = $("#grid").kendoGrid({ columns: [{ field: "name", title: "Name", attributes: function (e) { return { "class": "table-cell", style: e.name == "Jane Doe" ? "background-color: red" : "background-color: green" }; } }, { field: "title", title: "Title" }], dataSource: [{name: "Jane Doe", title: "Dr. Dr."}, {name: "John Doe", title: "Senior Citizen"}] }).data("kendoGrid"); var template = kendo.template($("#template").html()).bind(grid); grid.setOptions({ rowTemplate: template });
( demo )
Alternatively, you can also create the following attributes:
{ field: "name", title: "Name", attributes: { "class": "# if(data.name === 'Jane Doe') { # red # } else { # green # } #" } },
This would have the advantage of not using a string pattern, but you would need to use the pattern syntax for the logic.
( demo )
source share