问题描述:在可编辑表格中,我选择一行已输入的数据,点击删除按钮,该行数据将被删除,然后当我点击表单提交按钮时,已经被删除的那一行数据仍然被插入数据库中。
1:我的可编辑表格:
2:选中一行后,点击删除一行按钮:
3:删除成功后:
就是当我点击“提交”按钮时,仍然会插入数据库中两条记录(包括我删除的那一行数据)。
我的删除一行的代码:
/*
* 1:grid是当前的可编辑表格 我的是:var grid = newExt.grid.EditorGridPanel({});
* 2: store是可编辑表格中定义的数据源,我的是:var store = newExt.data.JsonStore({});
*/
var sm = grid.getSelectionModel();
var cell =sm.getSelectedCell();
var record =store.getAt(cell[0]);
store.remove(record);
解决方法:在你的store中添加属性配置选项:
pruneModifiedRecords: true
API中解释:此值为true将在 store被加载或者一条记录被删除时,清除所有记录的修改信息(默认为false。也就是说当我点击删除一行操作时,其实被删除的那一行数据还是存在的,只是不显示罢了,如果配置该选项为true,当我们执行删除一行操作时,就会把要删除的那一行数据给清除掉。
如下:
var store = new Ext.data.JsonStore({
data : data,
pruneModifiedRecords : true,
fields : ['detailCosts','detailType','detailPaid','detailDescription',{name : 'detailTime',type : 'date', dateFormat : 'Y-m-d'}]
});
添加后就执行删除行操作后,再点击提交按钮就不会把原来已经删除的那些数据提交到后台了!
补充:
新增一行:
Var countnum=10;//你的可编辑表格的初始最大行数
var p = new Record({
detailCosts : '',
detailType : '',
detailPaid : '2',
detailTime : '',
detailDescription : ''
});
grid.stopEditing();
store.insert(countnum,p);
countnum++;
上移:
var sm =grid.getSelectionModel();
var cell = sm.getSelectedCell();
var record = store.getAt(cell[0]);
var index = cell[0];
if(index > 0){
store.remove(record);
store.insert(index-1,record)
grid.getView().refresh();
}
下移:
var sm =grid.getSelectionModel();
var cell = sm.getSelectedCell();
var record = store.getAt(cell[0]);
var index = cell[0];
if(index < store.getCount()-1){
store.remove(record);
store.insert(index+1,record)
grid.getView().refresh();
}