This is a migrated thread and some comments may be shown as answers.

Custom Formatting Grid Columns

2 Answers 87 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jeremy Coenen
Top achievements
Rank 1
Jeremy Coenen asked on 02 Nov 2010, 05:10 PM
I am trying to change the ClientTemplate for a column based on the data in the row, but am unable to figure out how to do this.  Here is the sample code that I am trying to use for an AJAX bound grid.

<%: Html.Telerik().Grid<TelerikBugs.Models.Employee>()
           .Name("Employees")
          .Columns(columns =>
            {
              columns.Bound(o => o.EmployeeName).ClientTemplate("<ul><li>EmployeeId: <#= EmployeeId #></li><li>EmployeeName: <#=EmployeeName#></li></ul>") ;
              columns.Bound(o => o.ManagerEmployeeId);
              columns.Bound(o => o.EmployeeId)
                 .ClientTemplate("I want to change this value")
                                 .Width(500)
                                 .HeaderHtmlAttributes("visible='false'")
                                 .Title("Employee");
            })
            .CellAction(cell =>
               {
                 if (cell.Column.Member == "EmployeeId")
                 {
                   var employee = cell.DataItem;
                   if (employee.EmployeeId == 1)
                   {
                     cell.Column.ClientTemplate = "EmployeeId is #1 do #1 formatting here";
                   }
                   else
                   {
                     cell.Column.ClientTemplate = "EmployeeId is NOT #1 other formatting here";
                   }
 
                 }
               })
          .DataBinding(dataBinding => dataBinding.Ajax().Select("GetEmployees", "Home"))                   
%>

2 Answers, 1 is accepted

Sort by
0
Accepted
Atanas Korchev
Telerik team
answered on 02 Nov 2010, 05:23 PM
Hello Jeremy Coenen,

 You can embed conditional logic in the client template like this:

.ClientTemplate("<# if (EmployeeId == 1) { #> EmployeeId is 1 <# } else { #> EmployeeId is not 1 <# } #>");

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Dan
Top achievements
Rank 1
answered on 02 Mar 2011, 08:30 PM
I am having trouble with this Client Temple column, is there a syntax error? It returns an empty cell, which is ok for if(<#= hold #> == 'N'

.ClientTemplate(

 

"<# if(<#= hold #> == 'Y') {#><img alt='OnHold'src='../../Content/Images/StopHoldY.gif'></img><# } #>")

Thanks Dan

 

Tags
Grid
Asked by
Jeremy Coenen
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Dan
Top achievements
Rank 1
Share this question
or