How to display Syncfusion controls in grid columns?
Solution:
You can display the other Syncfusion Essential JavaScript controls using template property of Grid columns.
The following code example shows how to render Syncfusion Essential JavaScript ejRating control as one of the Grid column and set the value based on the column “EmployeeID”.
We have used JSRender conditional tag “if” to set value for Rating control based on condition.
1. Render the Grid control.
JS
<script type="text/x-jsrender" id="columnTemplate"> {{if EmployeeID<3}} <input type="text" class="rating" value="3" /> {{else EmployeeID>2 && EmployeeID<5}} <input type="text" class="rating" value="3" /> {{else EmployeeID>4}} <input type="text" class="rating" value="5" /> {{/if}}
|
JS
<div id="Grid"></div> <script type="text/javascript"> $(function () {// Document is ready. $("#Grid").ejGrid({ dataSource: window.EmployeeView, allowPaging: true, columns: [ { headerText: "Employee Rating", template:"#columnTemplate", width: 150 }, //binding template column to grid { field: "EmployeeID", headerText: "Employee ID", width: 90 }, { field: "FirstName", headerText: "First Name", width: 90 }, { field: "LastName", headerText: "Last Name", width: 90 }, { field: "Country", headerText: "Country", width: 80 }, ], templateRefresh: "template", }); }); </script>
MVC
@(Html.EJ().Grid<EmployeeView>("Grid") .Datasource((IEnumerable<object>)ViewBag.data)) .AllowPaging() .Columns(col => { col.HeaderText("Employee Rating").Template("#columnTemplate"). Width(100).Add(); col.Field("EmployeeID").HeaderText("Employee ID").Width(90).Add(); col.Field("FirstName"). HeaderText("First Name"). Width(90).Add(); col.Field("LastName").HeaderText("Last Name").Width(90).Add(); col.Field("Country").HeaderText("Country").Width(80).Add(); }) .ClientSideEvents(eve=>{ eve.TemplateRefresh ("template") });
[Controller] namespace EJGrid.Controllers { public class HomeController : Controller { public ActionResult Index() { var DataSource = EmployeeViewRepository.GetAllRecords(); ViewBag.data = DataSource; return View(); } } }
ASP.NET
[aspx] <ej:Grid ID=”Grid runat="server" AllowPaging="True" > <ClientSideEvents templateRefresh="template" /> <Columns> <ej:Column HeaderText="Employee Rating" Template="#columnTemplate" Width="150" /> <ej:Column Field="EmployeeID" HeaderText="Employee ID" Width="90"/> <ej:Column Field="FirstName" HeaderText="First Name" Width="90"/> <ej:Column Field="LastName" HeaderText="Last Name" Width="90"/> <ej:Column Field="Country" HeaderText="Country" Width="80"/> </Columns> </ej:Grid>
[CS] public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { this.Grid.DataSource = new NorthwindDataContext().EmployeeView.ToList(); this.Grid.DataBind(); } }
2. You can render the ejRating control in Grid column using the templateRefresh event. If ej controls are rendered in templateRefresh event then it will re-render automatically, when any grid action is performed.
JS
function template(args){ $(args.cell).find(".rating").ejRating({ allowReset: false }); }
Note: Similarly, we can bind all other Syncfusion JavaScript controls such as ejDatePicker, ejTimePicker, ejCheckBox etc as one of the column in grid.
The following output is displayed as a result of the above code example.
Figure: Grid with template column