努力寻找一些易于理解的代码。
如何在Dojo数据网格(版本1.4.2)中添加行并清除所有行。假设数据是带有customerID和地址的2列。
我正在使用
dojo.data.ItemFileWriteStore
将值存储在 - 但又不太确定应该如何使用它。
这不可能那么难。
干杯。
答案 0 :(得分:4)
您可以使用grid.store
从网格中获取数据存储引用,然后您可以使用store.newItem()
在商店中创建新项目。此新项目将添加为网格中的新行。例如,store.newItem({customerID : 1, address : "Somewhere"})
。
要清除所有行,您可以循环数据存储中的所有项目并使用deleteItem()
删除所有项目,或使用数据网格中的内部函数_clearData()
删除所有项目行,或使用setStore()
将新的空商店设置为网格。我更喜欢使用空商店来重置网格。
答案 1 :(得分:4)
上述答案是正确的,但您还需要在写入存储上调用save()
来“提交”更改。保存时,使用商店的小部件(例如datagrid)将自行刷新。
此外,newItem()
会返回您刚创建的新项目,因此如果您不想将对象传递给newItem
,只需修改其返回值,然后save()
商店。< / p>
伪代码:
var i = store.newItem({});
store.setValue(i,"newattribute1","new value");
store.setValue(i,"newattribute2","new value 2");
store.save();
Here is the relevant docs for ItemFileWriteStore说明如何使用newItem()
,setValue()
和save()
。
而不是deleteItem,你应该使用setStore(new ItemFileWriteStore())
,但我怀疑当你这样做时有内存泄漏,要小心。这使得一个新的空白商店与网格一起使用。
答案 2 :(得分:1)
我已经完成了一个关于此的示例...代码在这里
//First we create the buttons to add/del rows var addBtn = new dijit.form.Button({ id: "addBtn", type: "submit", label: "Add Row" }, "divAddBtn");//div where the button will loadvar delBtn = new dijit.form.Button({ id: "delBtn", type: "submit", label: "Delete Selected Rows" }, "divDelBtn");
//Connect to onClick event of this buttons the respective actions to add/remove rows. //where grid is the name of the grid var to handle. dojo.connect(addBtn, "onClick", function(event) { // set the properties for the new item: var myNewItem = { id: grid.rowCount+1, type: "country", name: "Fill this country name" }; // Insert the new item into the store: // (we use store3 from the example above in this example) store.newItem(myNewItem); });
dojo.connect(delBtn, "onClick", function(event) { // Get all selected items from the Grid: var items = grid.selection.getSelected(); if (items.length) { // Iterate through the list of selected items. // The current item is available in the variable // "selectedItem" within the following function: dojo.forEach(items, function(selectedItem) { if (selectedItem !== null) { // Delete the item from the data store: store.deleteItem(selectedItem); } // end if }); // end forEach } // end if });