Kendo Grid ClientTemplate conditional column

11,090

Solution 1

You can try like below code..may be this help you..

columns.Bound(p => p.Active).ClientTemplate("\\#if('#=Active#'=='Y') {\\<input type='button' value='OK' />\\}\\#");

or may be use

"#= (Active) ? ' ' : 'your code here' #"

Solution 2

You can use the following piece of code:

@(Html.Kendo().Grid(Model.Users).Name("Grid").Columns(columns =>
{
  columns.Bound(p => p.FirstName);
  columns.Bound(p => p.LastName);
  columns.Bound(p => p.UserName);
  columns.Bound(p => p.Email);   
  columns.Bound(o => o.IsActive).ClientTemplate("#if(IsActive){#<a href='javascript:void(0)' >Edit</a>#}#").Title("Action");

})
Share:
11,090
Pawan
Author by

Pawan

Updated on June 05, 2022

Comments

  • Pawan
    Pawan about 2 years

    I am working with ASP.NET MVC 4 with Kendo UI(kendo grid).Below is sample code of Kendo Grid -

    @(Html.Kendo().Grid(Model.Users).Name("Grid").Columns(columns =>
    {
       columns.Bound(p => p.FirstName);
       columns.Bound(p => p.LastName);
       columns.Bound(p => p.UserName);
       columns.Bound(p => p.Email);   
       columns.Bound(o => o.IsActive).ClientTemplate(links).Title("Action");      
    
    })
    

    In the above code my IsActive column have some links for Actions like Edit,Update,Delete.And i am adding those links into Kendo grid by links variable.And I want to use links variable on the basis of conditions.Means i want conditional ClientTemplate here.

    So anyone suggest how can make a conditional ClientTemplate in kendoGrid ?

    2) Also i want to add condition on the basis on the bool field value of my model(Model.Users).

    So i want to know how we can get that field from Model.Users model in kendo grid for each row.Like -

    .ClientTemplate(if(IsAdmin && ViewBag.IsActive){.....} else{....})
    
  • Pawan
    Pawan almost 10 years
    Thanks for reply #Parthiv.But according to my need i have to add one more condition in that template query.And that condition will be based on some other server variable.So can you suggest me how we can add that ?
  • Parthiv Pandya
    Parthiv Pandya almost 10 years
    Hey Pawan, You are absolutely right but you can maintain this IsAdmin flag in ViewBag and access in this ClientTemplate.or whatever values you can maintaing in viewbag and access it.
  • adityaa
    adityaa about 4 years
    @ParthivPandya Parthiv - can you help on this similar issue stackoverflow.com/questions/61334924/…
  • Parthiv Pandya
    Parthiv Pandya about 4 years
    .ClientTemplate("# if(data.TotalSalesAmt.sum == 0) " + "{# <div>Avg: #= kendo.toString(0,'P2') # </div>#} " + "else {# <div>Avg: #= kendo.toString(data.TotalMarginAmt.sum / data.TotalSalesAmt.sum,'P2') # </div> #}#") try this way.