简体   繁体   中英

Populate Kendo grid with Ajax result

I want to be able to use the result of the ajax call to make a decision which Kendo Grid I will populate. If I receive only one item in the array I want to populate a grid, otherwise I have another grid for multiple items.

My jQuery

    $.ajax({
        type: "POST",
        dataType: "json",
        url: 'Item/GetItems/',
        data: { number: number },
        success: function (data) {

            if (data.length == 1) {
                var sGrid = $("#SingleGrid").data("kendoGrid").dataSource.data(data);

                //I´ve also tried this
                //sGrid.refresh();
            }
            else {
                var mGrid = $("#MultipleGrid").data("kendoGrid").dataSource.data(data);

                //I´ve also tried this
                //mGrid .refresh();
            }
        },
        error: function () {
        }
    });

My Controller Action

    public ActionResult GetItems([DataSourceRequest] DataSourceRequest request, string number)
    {
        var items = _idg.GetItems(number);
        return Json(items.ToDataSourceResult(request, ModelState));
    }

I´ve been monitoring Firebug and it shows no errors. I´m trying to prevent the second call to the server by making the decision to populate one of the grids. Is there a way force dataSource refresh like this client-side? (without calling the read function on the dataSource which would call the server the second time)

###### EDITED ########

function TestGrid() {
    var dataSource = new kendo.data.DataSource({
        transport: {
            read: {
                type: "POST",
                dataType: "json",
                url: 'Item/GetItems/'
            }
        },
        schema: {
            data: function (response) {
                // Here the Total is always correct
                return response.Total;
            }
        }
    });

    dataSource.fetch(function () {
        var kendoGrid;
        var data = this.data();
        //Here the data does not include my Total
        alert(data);
        if (data.length == 1) {
            kendoGrid = $("#SingleGrid").data("kendoGrid");
        } else {
            kendoGrid = $("#MultipleGrid").data("kendoGrid");
        }
        kendoGrid.setDataSource(dataSource);
        kendoGrid.refresh();
    });
}

From my example above I can´t seem to reach the total number from this.data(). When I debug with Firebug I can see that the Total number is always correct. Any ideas?

You could populate a new instance of a kendo datasource, load the data, then acting on the results, set the datasource of the grid. Maybe something like this:

var dataSource = new kendo.data.DataSource({
  transport: {
    read:  {
      type: "POST",
      dataType: "json",
      url: 'Item/GetItems/',
      data: { number: number },
    }
  }
});

Then fetch the data from the server and act on the result:

dataSource.fetch(function() {
  var data = this.data();
  var kendoGrid;
  if (data.length == 1) {
    kendoGrid = $("#SingleGrid").data("kendoGrid");
  } else {
    kendoGrid = $("#MultipleGrid").data("kendoGrid");
  }
  // Replace the grids data source with our new populated data source
  kendoGrid.setDataSource(dataSource);
  kendoGrid.refresh();
});

You can also do this things.

$("#grid").kendoGrid({
            dataSource: {
                transport: {
                    read: {
                        url: "http://localhost:13073/home/KendoGriddata",
                        dataType: "json",
                        type: "Post",
                        data: function () {
                            var dt = { MultipleSearchText: 'rikin' };
                            return dt;
                        }
                    }
                },
                pageSize: 5    
            });

You can try such a variant:

var grid = $('#SingleGrid').getKendoGrid(); 
grid.dataSource.data(data);
grid.refresh(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM