实现Grid删除功能

今天毕业设计做到了删除功能,
但是找了好多的例子却不知道怎么获取被选中的行,
终于在网上找到了这个例子:

Ext.onReady(function(){

/* var MyRecord = Ext.data.Record.create([
{ name:’attend_id’ },
{ name:’attend_banci’ },
{ name:’attend_otime’ },
{ name:’attend_onime’ }
]);
var dataProxy = new Ext.HttpProxy({ url:”attend_smesage.php” });
var theReader = new Ext.data.JsonReader({
totalProperty:”totalCount”,
root:”rows”,
id:”id”
},MyRecord);
var store = new Ext.data.JsonStore({
proxy:dataProxy,
reader:theReader
});*/

var store = new Ext.data.JsonStore({
url:”attend_smesage.php”,
totalProperty:”totalCount”,
root:”json_string”,
fields:[‘attend_id’,’attend_banci’,’attend_outime’,’attend_ontime’]
});
store.load();
var sm = new Ext.grid.CheckboxSelectionModel();
/***
增加了选项框
*/
var colM = new Ext.grid.ColumnModel([
sm,
{ header:’编 号’, dataIndex:’attend_id’,sortable:true },
{ header:’班次设置’, dataIndex:’attend_banci’},
{ header:’上班时间’, dataIndex:’attend_outime’},
{ header:’下班时间’, dataIndex:’attend_ontime’}
]);

var grid = new Ext.grid.EditorGridPanel({
title:”考勤时间信息”,
//renderTo: ‘server_message’,
cm:colM,
store:store,
sm:sm,
iconCls:’attend_mes’,
//frame:true,
//bodyStyle:’padding:5px;’,
stripeRows :true,//让表格具有间隔的斑纹
trackMouseOver:true,//鼠标移动到某一行时,该行会变成高亮显示
autoExpandColumn:3,
tbar:[{pressed:true,text:’重新设置’,iconCls:’attend_add’,handler:add},
{xtype:”tbseparator”},
{pressed:true,text:’删 除’,iconCls:’attend_del’,handler : deleteMenu}
]
});

function add()
{
/**
@重新设置功能函数
*/
var countrys =[[“白 班”,’1′],[“早 班”,’2′],[“中 班”,’3′],[“晚 班”,’4′]];
var store = new Ext.data.SimpleStore({
fields:[“name”,’code’],
data:countrys
});

var attend = new Ext.form.FormPanel({
baseCls: ‘x-plain’,
labelWidth: 55,
defaultType: ‘textfield’,
items: [{
fieldLabel: ‘选择班次’,
xtype:”combo”,
name: ‘attend_banci’,
displayField:”name”,
valueField:’code’,
emptyText:’选择班次’,
anchor:’100%’,
store:store,
mode:’local’,
typeAhead:true,
triggerAction:’all’,
allowBlank:false,
selectOnFocus:true
},
{
fieldLabel: ‘上班时间’,
name: ‘attend_outime’,
xtype:’timefield’,
allowBlank:false,
anchor:’100%’
},{
fieldLabel: ‘下班时间’,
name: ‘attend_ontime’,
xtype:’timefield’,
allowBlank:false,
//format:”G:i”,
anchor: ‘100%’
}, {
xtype: ‘textarea’,
hideLabel: true,
name: ‘attend_remark’,
anchor: ‘100% -78’
}]
});

var windows = new Ext.Window({
title: ‘考勤时间设置’,
width: 500,
height:300,
minWidth: 300,
minHeight: 200,
iconCls:’attend’,
layout: ‘fit’,
plain:true,
modal:true,
closeAction:’close’,
closable:true,
collapsible:true,
maximizable:true,
bodyStyle:’padding:5px;’,
buttonAlign:’center’,
items: attend,
buttons: [{
text: ‘确 定’,
type: ‘submit’,
//定义表单提交事件
handler:function(){
if(attend.form.isValid()){//验证合法后使用加载进度条
Ext.MessageBox.show({
title: ‘请稍等’,
msg: ‘正在加载…’,
progressText: ”,
width:300,
progress:true,
closable:false,
animEl: ‘loding’
});
//控制进度速度
var f = function(v){
return function(){
var i = v/11;
Ext.MessageBox.updateProgress(i, ”);
};
};

for(var i = 1; i < 13; i++){
setTimeout(f(i), i*150);
}

//提交到服务器操
attend.form.doAction(‘submit’,{
url:’attend_sett.php’,//文件路径
method:’post’,//提交方法post或get
params:”,
//提交成功的回调函数
success:function(form,action){
if (action.result.msg==’ok’) {
document.location=’ok.html’;
} else {
Ext.Msg.alert(‘提交失败’,action.result.msg);
}
},
//提交失败的回调函数
failure:function(){
Ext.Msg.alert(‘错误’,’服务器出现错误请稍后再试!’);
}
});
}
}
},{
text: ‘重 置’,
handler:function(){attend.form.reset();}
}]
});

windows.show();
}
//——–@结束——–

function deleteMenu() {
/*
@实现删除功能
*/
var selectedKeys = grid.selModel.selections.keys;

if (selectedKeys.length > 0) {
Ext.MessageBox.confirm(‘请注意’,’是否真的要删除指定的内容’, deleteRecord);
} else {
Ext.MessageBox.alert(‘请注意’, ‘请您选择要删除的记录!’);
}
}

function deleteRecord(btn) {
if (btn == ‘yes’) {
var selectedRows = grid.selModel.selections.items;
var selectedKeys = grid.selModel.selections.keys;
//var selectedKeyse = grid.selModel.selections.getSelected().get(“attend_id”);
var encoded_keys = Ext.encode(selectedKeys);//
//var selectedKeys = grid.getSelectionModel().getSelected().attend_id;

Ext.MessageBox.show({
msg : ‘正在请求数据,请稍后’,
processText : ‘正在请求数据’,
width : 300,
wait : true,
waitConfig : {
interval : 200
}

});
function successFn(response,options){

Ext.MessageBox.alert(‘ 提示’,’你删除的数据ID为’+selectedKeys);
}
function failureFn(response,options){
Ext.MessageBox.alert(‘提示’,’数据删除失败!’);
}
Ext.Ajax.request({
url : ’11.php’,
method : ‘post’,
params :{ id:encoded_keys},
success :successFn,
failure :failureFn
/* callback : function(options, success, response) {
var responseArray = Ext.util.JSON.decode(response.responseText);
if (responseArray.success == true) {
Ext.MessageBox.hide();
Ext.MessageBox.alert(“success”, “delete option success”);
dataStore.reload();
} else {
Ext.MessageBox.hide();
Ext.MessageBox.alert(“失败,请重试”, responseArray.message);
}
},
failure : function(response, options) {
Ext.MessageBox.hide();
ReturnValue = Ext.MessageBox.alert(“警告”, “出现错误,请与管理员联系”);
}*/
})
}
}
//——–结束——–
win = new Ext.Window({
id:’win’,
title:’考勤信息’,
layout:’fit’,
width:700,
height:400,
plain:true,
iconCls:’attend’,
bodyStyle:’padding:5px;’,
maximizable:true,
closeAction:’close’,
closable:true,
collapsible:true,
buttonAlign:’center’,
items:grid
});
win.show();

});


但是我在实践的时候发现得到的id和原来的不同,
本来我的id是6,用这个方法返回来的是1006.

后来改用了下面的方法解决了。

if(app.grid.selModel.hasSelection()) {
var records = app.grid.selModel.getSelections();//得到被选择的行当数组
var recordsLen = records.length;

//var selectedKeys = app.grid.selModel.selections.keys;
//var encoded_keys = Ext.encode(selectedKeys);
//alert(selectedKeys);
//alert(encoded_keys);
var jsonStr = ‘{‘;
for(var i=0; i //id为app.store中的field中的id
var id = records.get(“id”);
if(i!=0) {
jsonStr += ‘,”id”:’+id;
}
else {
jsonStr += ‘”id”:’+id;
}
}
jsonStr += ‘}’;
//alert(jsonStr);
else {
Ext.Msg.alert(“提示”,”请选择要删除的作业”);
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注