Articles in this section
Category / Section

How to display Syncfusion JavaScript controls in grid columns?

4 mins read

Solution:

You can display the other Syncfusion Essential JavaScript controls using template property of JavaScript 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}}</script>

 


 

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.

Grid with template column

 

Figure: Grid with template column

 

Conclusion

I hope you enjoyed learning on how to display Syncfusion JavaScript controls in grid columns.

You can refer to our JavaScript Grid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our JavaScript Grid example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!
Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied