Javascript – How to refresh kendo grid with new data

javascriptkendo-asp.net-mvckendo-gridkendo-ui

After I load a kendo grid, I would like to refresh/reload the grid with new data using a button click.

Controller:

    using Kendo.Mvc;
    using Kendo.Mvc.Extensions;
    using Kendo.Mvc.Infrastructure;
    using Kendo.Mvc.UI;

    public ActionResult FollowUpGrid_Read([DataSourceRequest]DataSourceRequest request, string name, string id)
{
List<vmFollowUpGrid> FUPList = new List<vmFollowUpGrid>();
FUPList = (from u in db.usrUserBldgLists 
                join e in db.entEntities on u.EntID equals e.EntID
                join d in db.entDistricts on e.FANo equals d.DistNo
                join ed in db.entDistricts on e.OANo equals ed.DistNo
                join b in db.entBuildings on e.OBNo equals b.BuildNo
                where u.UserID == "A1036719" && u.FANO == id
                select new vmFollowUpGrid { FANo = u.FANO, FAName = d.DistrictName, OANo = u.OANO, District = ed.DistrictName, OBNo = u.OBNo, Building = b.BuildName}).Take(50).ToList();
var FUList = FUPList.ToDataSourceResult(request);
return Json(FUList, JsonRequestBehavior.AllowGet);                         
} 

The cshtml page:

        @(Html.Kendo().Grid<MDEFollowUp.Models.vmFollowUpGrid>()
.Name("FollowUpGrid")
.Columns(columns =>
{
    columns.Bound(p => p.FANo);
    columns.Bound(p => p.FAName);
    columns.Bound(p => p.OANo);
    columns.Bound(p => p.District);
    columns.Bound(p => p.OBNo);
    columns.Bound(p => p.Building);
})
   .Pageable(pageable => pageable
            .Refresh(true)
            .PageSizes(true)
            .ButtonCount(5))
    .Sortable()
    .Scrollable()
    .Filterable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
                .Read(read => read.Action("FollowUpGrid_Read", "FollowUp").Data("additionalAgencyInfo")))
        ) 

Then a button click event to refresh the grid with new data but this doesn't seem to recognize the grid assignment.

            $("#btnclick").click(function () {

        var grid = $("#FollowUpGrid").data("tGrid");
        var params = {
            name: "Agency",
            id: "63190"
        };
        var dataSource = new kendo.data.DataSource({ data: params });
        //grid.rebind(params);
        grid.setDataSource(dataSource);
    })

How should I assign the grid in the button to accomplish this?

Best Answer

There are two main tasks to handle in the described scenario:

  1. dataSource.data should point to an array, not an object
  2. if the new data will feature completely different fields, you will also need to replace the Grid columns

A possible way to set a new DataSource and new columns is via setOptions.

Here is an example:

http://dojo.telerik.com/oWahO

UPDATE

If the columns will remain the same after the data is reloaded, then you can use the following approach:

$("#btnclick").click(function () {
    var grid = $("#FollowUpGrid").data("tGrid");
    var params = {
        name: "Agency",
        id: "63190"
    };
    grid.dataSource.read(params);
})

This will pass the name and id parameters to the action method, and preserve the existing DataSource instance and its settings.

http://docs.telerik.com/kendo-ui/api/javascript/data/datasource#methods-read