[英]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.我希望能够使用 ajax 调用的结果来决定我将填充哪个 Kendo Grid。 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我的 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.我一直在监视 Firebug,它没有显示任何错误。 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)
(不调用 dataSource 上的 read 函数,这将第二次调用服务器)
###### 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().从上面的示例中,我似乎无法从 this.data() 中得出总数。 When I debug with Firebug I can see that the Total number is always correct.
当我使用 Firebug 进行调试时,我可以看到总数始终是正确的。 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.您可以填充 kendo 数据源的新实例,加载数据,然后对结果进行操作,设置网格的数据源。 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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.