要更改网格中的值,您需要更改网格存储中的值。网格数据与存储数据绑定,网格会根据需要自行更新。
因此,关键是了解 Dojo 的数据 api 以及存储在 Dojo 中如何工作。不要直接在网格中操作数据,而是在存储中操作数据。
理想情况下,存储是您在应用程序运行时操作的数组,并且您不需要将数组同步到网格。只需使用 ItemFileWriteStore 作为您的数据持有者,除非这是不可能的。
此外,如果可能的话,使用 dojo 数据身份 API 可以使在网格中查找项目变得更加简单。假设您知道应用程序中的某个项目何时被更新、删除或更改,您应该能够在操作发生时根据需要修改网格存储。这绝对是首选方法。如果你做不到这一点,你将不得不进行一般的获取并使用 onComplete 回调手动同步你的数组,这将非常慢并且无法很好地扩展,在这种情况下,你也可以创建一个新的存储所有一起并使用 grid.setStore(myNewStore) 将其分配给网格
这是一个基本的创建、更新和删除操作的小提琴:http://jsfiddle.net/BC7yT/11/
这些示例都利用了创建商店时声明身份的优势。
var store = new dojo.data.ItemFileWriteStore({
data: {
identifier : 'planet',
items: itemList
}
});
更新现有项目:
//If the store is not in your scope you can get it from the grid
var store = grid.store;
//fetchItemByIdentity would be faster here, but this uses query just to show
//it is also possible
store.fetch({query : {planet : 'Zoron'},
onItem : function (item ) {
var humans = store.getValue(item, 'humanPop');
humans += 200;
store.setValue(item, 'humanPop', humans);
}
});
插入新项目:
store.newItem({planet: 'Endron', humanPop : 40000, alienPop : 9000});
} catch (e) {
//An item with the same identity already exists
}
删除项目:
store.fetchItemByIdentity({ 'identity' : 'Gaxula', onItem : function (item ) {
if(item == null) {
//Item does not exist
} else {
store.deleteItem(item);
}
}});