【发布时间】:2011-04-02 13:57:55
【问题描述】:
努力寻找一些易于理解的代码。
如何在 Dojo 数据网格(版本 1.4.2)中添加一行并清除所有行。假设数据是包含 customerID 和地址的 2 列。
我正在使用
dojo.data.ItemFileWriteStore
将值存储在其中 - 但又不太确定应该如何使用它。
没那么难。
干杯。
【问题讨论】:
标签: javascript datagrid dojo
努力寻找一些易于理解的代码。
如何在 Dojo 数据网格(版本 1.4.2)中添加一行并清除所有行。假设数据是包含 customerID 和地址的 2 列。
我正在使用
dojo.data.ItemFileWriteStore
将值存储在其中 - 但又不太确定应该如何使用它。
没那么难。
干杯。
【问题讨论】:
标签: javascript datagrid dojo
以上答案是正确的,但您还需要在写入存储上调用save() 以“提交”更改。保存时,使用商店的小部件(例如数据网格)将自行刷新。
另外,newItem() 返回您刚刚创建的新项目,因此如果您不想将对象传递给 newItem,只需修改其返回值,然后 save() 存储。
伪代码:
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()。
您应该使用setStore(new ItemFileWriteStore()) 而不是deleteItem,但我怀疑这样做时会出现内存泄漏,请小心。这使得一个新的空白存储与网格一起使用。
【讨论】:
我已经完成了一个关于这个的例子......代码在这里
//首先我们创建添加/删除行的按钮 var addBtn = new dijit.form.Button({ id: "addBtn", 类型:“提交”, 标签:“添加行” }, "divAddBtn");//加载按钮的divvar delBtn = new dijit.form.Button({ id:“delBtn”, 类型:“提交”, 标签:“删除选定的行” }, "divDelBtn");
//连接到此按钮的onClick事件,以添加/删除行的相应操作。 //其中 grid 是要处理的网格变量的名称。 dojo.connect(addBtn,“onClick”,函数(事件){ // 设置新项目的属性: var myNewItem = { id:grid.rowCount+1, 类型:“国家”, name: "填写这个国家名称" }; // 将新项目插入商店: //(我们在这个例子中使用上面例子中的store3) 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
});
【讨论】:
您可以使用grid.store 从网格中获取数据存储引用,然后您可以使用store.newItem() 在存储中创建一个新项目。此新项目将作为新行添加到网格中。例如,store.newItem({customerID : 1, address : "Somewhere"})。
要清除所有行,您可以循环数据存储中的所有项目并使用deleteItem() 删除所有项目,或使用数据网格中的内部函数_clearData() 删除所有行,或使用setStore() 为网格设置一个新的空存储。我更喜欢使用空存储来重置网格。
【讨论】: