软件下载 | 资讯教程 | 最近更新 | 下载排行 | 一键转帖 | 发布投稿
您的位置:最火下载站 > Web前端 > Ajax/JavaScript > extjs通用CRUD方法

extjs通用CRUD方法

extjs通用CRUD方法,只要你传入相应的参数就能完成extjs表格的CRUD

代码:

/**
* 添加事件
* @param {} saveButton
* @param {} gridObj
* @param {} idName
* @param {} treeObj
* @param {} url
* @param {} assistFunc
* @returns {}
*/
function commGridInsertAction(saveButton,gridObj,idName,treeObj,url,assistFunc){
if(saveButton != null && saveButton != undefined){
if(assistFunc){
assistFunc();
}
saveButton.on("click",function(){
var sd = gridObj.getStore();
conn.request({
url : url,
params : {},
success: function(response, opts){//成功操作
var obj = Ext.decode(response.responseText)[0];
var Plant = gridObj.getStore().recordType;
var p = new Plant(obj);
gridObj.stopEditing();
sd.insert(0, p);
gridObj.startEditing(0, 2);
},
failure:function() {//失败操作
Ext.Msg.alert("提示", "添加数据失败...");
}
})
});
}
}
/**
* 删除事件
* @param {} deleteButton
* @param {} gridObj
* @param {} idName
* @param {} treeObj
* @param {} url
* @param {} assistFunc
* @returns {}
*/
function commGridDeleteAction(deleteButton,gridObj,idName,treeObj,url,params,assistFunc){
if(deleteButton != null && deleteButton != undefined){
if(assistFunc){
assistFunc();
}
deleteButton.on("click",function(b,e){
var sd = gridObj.getStore();
var rs = gridObj.getSelectionModel().getSelections();
var length = rs.length;
if(length > 0){
var ids_data = "";
var dis_array = new Array();
for (var i = 0; i < length; i++) {
if(i==0){
ids_data = rs[i].get(idName);
}else{
ids_data = ids_data + ","+rs[i].get(idName);
}
dis_array.push(rs[i].get(idName));
}
var para = params;
para[idName] = ids_data;
conn.request({
url: url,
params : para,
success: function(response){//成功操作
sd.reload();
//循环删除gird的垃圾数据
for (var i = 0; i < length; i++) {
sd.remove(rs[i]);
}
Ext.example.msg("提醒","您已经成功删除数据");
},
failure:function() {//失败操作
Ext.Msg.alert("提示", "删除数据失败...");
}
})
}else{
Ext.Msg.alert("提示", "您还没有选择要删除的数据...");
}
})
}
}
/**
* 批量更新
* @param {} listupdate
* @param {} gridObj
* @param {} treeObj
* @param {} url
* @param {} assistFunc
* @param {} canUpdateFields
* @returns {}
*/
function commGridListUpdateAction(listupdate,gridObj,treeObj,url,assistFunc,canUpdateFields){
if(listupdate != null && listupdate != undefined){
if(assistFunc){
assistFunc();
}
listupdate.on("click",function(){
var sd = gridObj.getStore();
var objs = sd.getModifiedRecords();
var objData = new Array();
var model = null;
if(objs.length == 0){
Ext.example.msg("提醒","没有要保存的数据");
return;
}else{
for(var i = 0 ; i<objs.length ;i++){
model = {};
$.each(canUpdateFields,function(k,v){
model[k] = objs[i].get(k);
});
objData.push(model);
}
conn.request({
url : url,
params : {strData:JSON.stringify(objData)},
success: function(response, opts){//成功操作
var i = response.responseText;
for(var j = 0 ; j<objs.length ;j++){ //去掉修改的红色标记
objs[j].commit();
j--;
}
Ext.example.msg("提醒","您已经成功修改 "+i+" 条数据");
},
failure:function() {//失败操作
Ext.Msg.alert("提示", "保存数据失败...");
}
})
}
})
}
}

代码:

/**
* 数据字典表格时间注册
* @param {} gridObj
* @param {} idName
* @param {} treeObj
* @returns {}
*/
function userGridEventMonitor(gridObj,idName,treeObj){
/**添加事件*/
var saveButton = gridObj.getTopToolbar().findById("save-user");
new commGridInsertAction(saveButton,gridObj,idName,treeObj,"/dept/userAction.action?FunctionName=insertUser&checkNodeId="+gridObj.store.baseParams["checkNodeId"]);
/**删除事件*/
var deleteButton = gridObj.getTopToolbar().findById("delete-user");
new commGridDeleteAction(deleteButton,gridObj,idName,treeObj,"/dept/userAction.action?FunctionName=deleteUser&cascadeType=CASCADE",
{userId:{}},null);
/**批量更新事件*/
var listupdate = gridObj.getTopToolbar().findById("listupdate-user");
new commGridListUpdateAction(listupdate,gridObj,treeObj,
"/dept/userAction.action?FunctionName=listupdate",
null,{"userId" : null,
"username" : null,
"chineseName" : null,
"password" : null,
"updateDate" : null,
"enabled" : null,
"orderIndex" : null,
"deptId" : null}
);
/**双击部门的事件*/
gridObj.on("celldblclick",function(g,rowIndex,columnIndex,e) {
var sd = g.getStore();
var cm = g.getColumnModel();
var id = cm.getDataIndex(columnIndex);
if("deptId" == id){
var deptID = sd.getAt(rowIndex).get("deptId");
var tree = new sms.treeDicWin.treeDicWin({width:250,height:500,
url:"/dept/deptAction.action?FunctionName=loadDept4Tree&showCheckbox=true&deptId=",
text:"海顿是公司",idName:"userId",DicidName:"deptId",g:g,rowIndex:rowIndex,columnIndex:columnIndex});
tree.show();
}else if("photosAdd" == id){
var dialog = new Ext.ux.UploadDialog.Dialog({
url: 'upload.jsp',
reset_on_hide: false,
autoCreate: true,
closable: true,
allow_close_on_upload: true,
permitted_extensions: ['JPG','jpg','jpeg','JPEG','GIF','gif'],
base_params:{className: "com.hd.department.model.user" }
upload_autostart: false
});
dialog.show();
dialog.on("uploadsuccess", function(dialog, filename, resp_data, record){

});
}

});

    相关阅读
    网友评论