Javascript 如何将项目添加到剑道 ui 网格的数据源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10734588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I add items to the data source of a kendo ui grid
提问by Vasco Costa
I have created a kendo.data.dataSource with success, and I am able to bind it to the KendoUI Grid on my page.
我已经成功创建了一个 kendo.data.dataSource,并且我能够将它绑定到我页面上的 KendoUI 网格。
But when I try to dataSource.insert(0, [a : "b"]);
it removes the data that was there previously.
但是当我尝试dataSource.insert(0, [a : "b"]);
删除之前存在的数据时。
My example code follows:
我的示例代码如下:
var tempSource = new kendo.data.DataSource({
data: [{"ID":1,"Name":"Cliente 1","NameID":"1 - Cliente 1"},{"ID":2,"Name":"Cliente 2","NameID":"2 - Cliente 2"}]
});
This is how I'm binding to the grid:
这就是我绑定到网格的方式:
$("#association-grid").kendoGrid({
height: 99,
columns:
[
{
field: "ID",
title: "ID"
},
{
field: "Name",
title: "Name"
},
{
field: "NameID",
title: "NameID"
}
],
dataSource: tempSource
});
This is how I add a new item:
这是我添加新项目的方式:
tempSource.insert(0, { ID: "John Smith", Name: "Product Description", NameID: "123 1st Street" });
If I perform the add before binding the data to the Grid, I lose the first two items that were originally on the dataSource object.
如果我在将数据绑定到 Grid 之前执行添加,我将丢失最初位于 dataSource 对象上的前两个项目。
In summary: I have a pre-created dataSource binded to a Grid. I want to be able to add a new item to the dataSource, and then refresh the Grid so that the new item appears.
总结:我有一个绑定到网格的预先创建的数据源。我希望能够向数据源添加一个新项目,然后刷新网格以便新项目出现。
Thanks,
谢谢,
VRC
虚拟现实控制中心
回答by Zaheer Ahmed
try this:
尝试这个:
dataSource.add({ name: "John Smith", description: "Product Description", address: "123 1st Street" });
回答by Sajjad Ali Khan
var grid = $("#itemsGrid").data("kendoGrid");
for (var i = 0; i < data.length; i++) {
grid.dataSource.insert(data[i]);
}
inserting new record to grid datasource
向网格数据源插入新记录