I'm new to using KendoUI, so I'm guessing there's an easy way to do this that I'm just not seeing. I have templates for rows to alternate colors, but I would like certain cells to change their styling depending upon whether or not they have particular content in them. What would be the best way to make that happen?
Thanks!
3 Answers, 1 is accepted
Such functionality is not supported out of the box, but you could achieve it using custom code - in the Grid's dataBound event apply your custom CSS class to the given cell(s) via standard DOM operations. For example:
dataBound:
function
() {
$(
'td'
).each(
function
(){
if
($(
this
).text()==
'Jane'
){$(
this
).addClass(
'customClass'
)}})
}
Greetings,
Iliana Nikolova
the Telerik team
columns: [ { title: "Name", field: "FirstName" }]
template: '#=Getvalue(FirstName<
br
>)#' .
And in Getvalue function you can return HTML or text based on the column value [FirstName in this case]
Example :
$("#grid").kendoGrid({
dataSource: ReportDataJson,
columns: [{ title: "Email", field: "Email", template: '<a href="mailto:#=Email#">#=Email#</a>' },
{ title: "First Name", field: "FName", template: '#=Getvalue(FName)#' }
]
});
}
function Getvalue(value) {
// console.log(value);
if (value && value != null && value.indexOf("A") == 0)
return "<b style='color:red'>"+ value+"</b>";
else
return "";
}
In this case, if first name has starting letter A , then it will be displayed in red color.
{
field: "FirstName",
title: "First Name", template: '#=Getvalue(FName)#'
},
{
field: "LastName",
title: "Last Name"
}
],
function Getvalue(value) {
// console.log(value);
if (value && value != null && value.indexOf("A") == 0)
return "<b style='color:red'>"+ value+"</b>";
else
return "";
}
This doesn't work.!!
But for your case it is FirstName.. So you need to this in template section. template: '#=Getvalue(FirstName)#' ... So this must work.
<div id="example" class="k-content">
<div id="grid"></div>
<script>
$(document).ready(function() {
var people = [
{
FirstName: "Joe",
LastName: "Jane"
},
{
FirstName: "Jane",
LastName: "Jane"
},
{
FirstName: "Nancy",
LastName: "Leverling"
},
{
FirstName: "Andrew",
LastName: "Peacock"
},
{
FirstName: "Janet",
LastName: "Smith"
},
{
FirstName: "Jane",
LastName: "Buchanan"
},
{
FirstName: "Margaret",
LastName: "Suyama"
},
{
FirstName: "Jane",
LastName: "King"
},
{
FirstName: "Steven",
LastName: "Callahan"
},
{
FirstName: "Michael",
LastName: "Jane"
}];
var localDataSource = new kendo.data.DataSource({data: people});
$("#grid").kendoGrid({
dataSource: localDataSource,
columns: [
{
field: "FirstName",
title: "First Name" ,template: '#=Getvalue(FirstName)#'
},
{
field: "LastName",
title: "Last Name"
}
],
});
function Getvalue(value) {
// console.log(value);
if (value && value != null && value.indexOf("A") == 0)
return "<b style='color:red'>"+ value+"</b>";
else
return "";
}
});
</script>
</div>
Whe I use Getvalue('#=FirstName#') display grid as:
|
Suyama | ||||||||||
I need an example where FirstName='Jane' color=red, Only FirstName | King | ||||||||||
function () { $('td').each(function(){if($(this).text()=='Jane'){$(this).addClass('customClass')}}) } gets all words "Jane", i need only FirstName='Jane' How can do it? |
Callahan | ||||||||||
Jane |
<div id="grid"></div>
<script>
function Getvalue(value) {
// console.log(value);
if (value && value != null && value.indexOf("A") == 0)
return "<b style='color:red'>"+ value+"</b>";
else
return "";
}
$(document).ready(function() {
var people = [
{
FirstName: "Joe",
LastName: "Jane"
},
{
FirstName: "Jane",
LastName: "Jane"
},
{
FirstName: "Nancy",
LastName: "Leverling"
},
{
FirstName: "Andrew",
LastName: "Peacock"
},
{
FirstName: "Janet",
LastName: "Smith"
},
{
FirstName: "Jane",
LastName: "Buchanan"
},
{
FirstName: "Margaret",
LastName: "Suyama"
},
{
FirstName: "Jane",
LastName: "King"
},
{
FirstName: "Steven",
LastName: "Callahan"
},
{
FirstName: "Michael",
LastName: "Jane"
}];
var localDataSource = new kendo.data.DataSource({data: people});
$("#grid").kendoGrid({
dataSource: localDataSource,
columns: [
{
field: "FirstName",
title: "First Name" ,template: '#=Getvalue(FirstName)#'
},
{
field: "LastName",
title: "Last Name"
}
],
});
});
</script>
</div>
2) simple steps:
1) define a COLUMN template:
<script id="dataTemplate1" type="text/x-kendo-template">
<div style="width:100%;text-align:center">
# if (CATCCA_PROG > 0 ) { #
<span class="progression ok">#: CATCCA_PROG #</span>
# } else { #
<span class="progression error">#: CATCCA_PROG #</span>
# } #
</div>
<div style="width:100%;text-align:center">#: CATCCA_CURR #</div>
</script>
OR
optionally, you have to define a function (can dynamically call templates based on data):
function kendoTemplate(options) {
if (options && "name" in options) {
var template = kendo.template($(options["name"]).html());
function prototype(data) {
/*if (data.CATCCA_PROG < 0) {
template = kendo.template($("#= kendo.toString(CATCCA_PROG,'n2')#").html());
return template(data);
}*/
if (template) {
return template(data);
}
}
return prototype
}
}
2) define using a COLUMN template inside GRID
$("#grid").kendoGrid({
autobind: false,
theme: "Nova",
dataSource: gridDataSource,
columns: [{
field: "NOMA_NAME",
title: "Номенклатура",
attributes: {
"class": "table-cell",
style: "text-aligh: right"
}
}, {
field: "CATCCA_CURR",
title: "ТО с НДС",
template: dataTemplate1
}, {
field: "CATCCA_LAST",
title: "ТО с НДС (пред.)"
}, {
field: "QTUNCA_CURR",
title: "Артикулы",
template: dataTemplate2 /* OR, dynamically: kendoTemplate({ name: "dataTemplate" })*/
}, {
field: "QTUNCA_LAST",
title: "Артикулы (пред.)",
}]
});
}