最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to refresh kendo grid with new data - Stack Overflow

programmeradmin1浏览0评论

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 acplish this?

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 acplish this?

Share Improve this question asked Oct 14, 2016 at 20:16 Tom STom S 2271 gold badge6 silver badges22 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

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 pletely 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./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./kendo-ui/api/javascript/data/datasource#methods-read

发布评论

评论列表(0)

  1. 暂无评论