我有一个网格和一个按钮将在中删除http://jsfiddle.net/9zB8R/
我的领域喜欢fields: ['id', 'name', 'type']
。如果type == delete
,我将删除它。
我初始化数据,如:
var simpleData = [];
var store = new Ext.data.ArrayStore({
fields: ['id', 'name', 'type'],
data: simpleData
});
for (i = 0; i < 20; i++) {
simpleData.push({id:''+i+'', name: 'name'+i, type: 'delete'});
}
//this record will not delete
simpleData.push({id:'20', name: 'enable', type: 'enable'});
store.loadData(simpleData);
我有一个类似下面的tbar,但不能删除所有具有type == delete
的记录。我该怎么解决。谢谢
tbar:[
{
text:'Delete all type = delete',
handler:function(){
store.each(function(item, idx) {
if (item && item.get('type')=='delete') {
store.removeAt(idx);
}
});
}
}
]
问题出在"each"循环上。删除循环中的项目会改变存储大小,因此只有部分项目会被删除。
为了绕过它,你可以按相反的顺序循环浏览商店,然后按如下方式删除:
var grid = new Ext.grid.GridPanel({
width: 400,
height: 600,
store: store,
loadMask: true,
renderTo: Ext.getBody(),
tbar:[
{
text:'Delete all type = delete',
handler:function(){
var i = store.getCount()-1;
for (i; i>=0; i--) {
if (store.getAt(i).get('type')=='delete') {
store.removeAt(i);
}
}
}
}
],