javascript 在 Dojo 数据网格中添加一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3546719/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:28:05  来源:igfitidea点击:

Add a row in Dojo datagrid

javascriptdatagriddojo

提问by Vidar

Struggling to find a bit of code to easily understand.

努力寻找一些易于理解的代码。

How do you add a row and clear all rows in a Dojo datagrid (version 1.4.2). Lets say the data is 2 columns with customerID and address.

如何在 Dojo 数据网格(版本 1.4.2)中添加一行并清除所有行。假设数据是包含 customerID 和地址的 2 列。

I am using

我在用

dojo.data.ItemFileWriteStore

to store values in - but again not quite sure how this should be used.

将值存储在 - 但再次不太确定应该如何使用它。

It can't be that hard.

不可能那么难。

Cheers.

干杯。

采纳答案by Alex Cheng

You can get the data store reference from the grid using grid.store, then you can use store.newItem()to create a new item in the store. This new item is added as a new row in the grid. For example, store.newItem({customerID : 1, address : "Somewhere"}).

您可以使用 获取网格中的数据存储引用grid.store,然后您可以使用store.newItem()它在存储中创建一个新项目。这个新项目被添加为网格中的新行。例如,store.newItem({customerID : 1, address : "Somewhere"})

To clear all the rows, you can either loop all the items in the data store and use deleteItem()to remove all the items, or use the internal function _clearData()in data grid to remove all the rows, or use setStore()to set a new empty store to the grid. I prefer to use a empty store to reset the grid.

要清除所有行,您可以循环数据存储deleteItem()中的所有项目并使用删除所有项目,或使用_clearData()数据网格中的内部函数删除所有行,或使用setStore()设置一个新的空存储到网格. 我更喜欢使用空商店来重置网格。

回答by srock

The above answers are correct, but you also need to call save()on the write store to "commit" the change. When you save, a widget using the store (datagrid for example) will refresh itself.

上面的答案是正确的,但您还需要调用save()写入存储来“提交”更改。保存时,使用商店的小部件(例如数据网格)将自行刷新。

Also, newItem()returns the new item you just created so if you don't want to pass an object to newItemjust modify its return value, then save()the store.

此外,newItem()返回您刚刚创建的新项目,因此如果您不想传递一个对象来newItem修改其返回值,那么save()store.

Pseudo code:

伪代码:

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 ItemFileWriteStorewhich tell how to use newItem(), setValue(), and save().

下面是ItemFileWriteStore相关的文档,告诉如何使用newItem()setValue()以及save()

Instead of deleteItem, you should use setStore(new ItemFileWriteStore()), but I suspect there is a memory leak when you do this, be careful. This makes a new blank store to be used with the grid.

您应该使用 代替 deleteItem,setStore(new ItemFileWriteStore())但我怀疑这样做时会出现内存泄漏,请小心。这使得一个新的空白存储与网格一起使用。

回答by Edward

I have finish one example about this... the code is here

我已经完成了一个关于这个的例子......代码在这里

//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 load

var 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 });