封装定时任务可编辑表格基类

eams.dc.taskMK.TaskItemEditGrid 相当于可编辑的定时任务的基类

eams.dc.taskMK.TaskItemEditGrid = Ext.extend(Ext.grid.EditorGridPanel, {
mystore : null,
myPStore: null,
actions : null,
searchValue : ' ',
constructor : function(_cfg) {

this.actions = [new Ext.Action({
text : '查询',
iconCls : 'db-icn-back',
handler : function() {
this.onSearch();
},
scope : this
}),
new Ext.Action({
text : '保存',
width:500,
iconCls : 'db-icn-back',
handler : function() {
this.SaveFn();
},
scope : this
})
,
new Ext.Action({
text : '添加',
width:200,
iconCls : 'db-icn-back',
handler : function() {
this.addRecord();
},
scope : this
})
];

this.myPStore = this.getMyPStore();
this.mystore = this.getStore();

Ext.apply(this,{
width : 825,
height : 455,
border : 0,
autoScroll : true,
clicksToEdit : 0,
cm : this.getCM(),
store : this.mystore,
buttonAlign:'center',
selModel : new Ext.grid.RowSelectionModel({
singleSelect : true
}),// 设置单行选中模式, 否则将无法删除数据
tbar : [{
xtype : 'label',
text : '任务项名称:'
}, {
xtype : 'textfield',
width : 300,
id : 'srchField'
}, this.actions[0]],
    bbar :['->',this.actions[1],this.actions[2]]
});


eams.dc.taskMK.TaskItemEditGrid.superclass.constructor.call(this,_cfg);
//this.getBottomToolbar().style='height:500';

this.on('contextmenu', function(e) {
e.preventDefault();
}, this);
var grid = this;

    this.on('rowcontextmenu', this.rowContextFn,this);//右键菜单代码关键部分
/*eams.dc.taskMK.TaskItemEditGrid.superclass.constructor.call(this);*/
},
rowContextFn:function(grid,rowindex,e){
    var DelMenu = new Ext.menu.Menu({
items : [{
text : "删除"
}]
});
DelMenu.on('click', function(Menu, item, e) {
var store = this.mystore;
var srchValue = this.searchValue;
var record = this.getSelectionModel().getSelected();
if (record == null) {
Ext.Msg.alert('提示', '请先选中要删除的记录!');
return
};
var name = record.data.name;
Ext.Msg.confirm('提示', '你确定要删除' + name + '吗?', function(clkBtn) {
if (clkBtn == 'yes') {
var uri = record.data.uri;
Ext.Ajax.request({
url : 'TaskItemAction?op=del',
method : 'post',
params : {
delUri : uri
},
callback : function(options, success, response) {
if (success == true) {
var remoteData = Ext.decode(response.responseText);
if(remoteData.result.success==true)
{
Ext.Msg.alert('提示', '数据删除成功');
}else
{
                                Ext.Msg.alert('提示',remoteData.result.msg);return;
}

} else {
Ext.Msg.alert('提示', '服务器端出现异常,稍候再试!');
}
store.proxy = new Ext.data.HttpProxy({
url : 'TaskItemAction?op=query&ex='
+ encodeURI(srchValue)
});
store.load();
}
})

};
});
}, this);
            e.preventDefault();
            DelMenu.showAt(e.getXY());
            grid.getSelectionModel().selectRow(rowindex);
            },
onSearch : function() {
var store = this.mystore;
var toolBar = Ext.get('srchField');
this.searchValue = toolBar.getValue();

        if(this.searchValue==null||this.searchValue=='undefined') this.searchValue=' ';
store.proxy = new Ext.data.HttpProxy({
url : 'TaskItemAction?op=query&ex=' + encodeURI(this.searchValue)
});
store.load();
},
addRecord : function() {
this.stopEditing();
var r = new Ext.data.Record({
length : 0,
width : 0,
height : 0,
qty : 0,
weight : 0,
newRecord : true
});
r.set('name', '');
r.set('termBuff', '');
r.set('ammeterBuff', '');
r.set('protocolUri', 'TP_3');
r.set('ammeterProtocolUri','AP_1')


this.mystore.insert(0, r);
this.startEditing(0, 0);
},
getCM : function() {
return new Ext.grid.ColumnModel([new Ext.grid.RowNumberer({
header : '序号',
width : 35
}), {
header : '标识',
dataIndex : 'uri',
width:150,
editable : false,
hidden:true,
editor : new Ext.form.TextField({
allowBlank : false
})
}, {
header : '任务项名称',
dataIndex : 'name',
width:150,
sortable : true,
editor : new Ext.form.TextField({
allowBlank : false
})
},
                {
header : '终端协议标识',
dataIndex : 'protocolUri',
width:200,
/**/renderer:  function(v){ 
  if(v){
  var comboxStore = Ext.getCmp('cmbtp').store;
  var index = comboxStore.find('uri',v);
  if(index<0) return;
  return comboxStore.getAt(index).get('name');
}
},
editor : new Ext.form.ComboBox({
typeAhead : true,
triggerAction : 'all',
emptyText : '请选择...',
id:'cmbtp',
store :this.myPStore,
valueField : 'uri', // 传送的值
displayField : 'name', // UI列表显示的文本
editable : false,
mode : 'local' // 数据模式,romote远程模式/local为本地模式
})
},

{
header : '终端数据单元',
dataIndex : 'termBuff',
width:150,
editor : new Ext.form.TextField({
allowBlank : false
})
},
                 {
header : '表计协议标识',
dataIndex : 'ammeterProtocolUri',
width:200,
/**/renderer:  function(v){ 
  if(v){
  var comboxStore = Ext.getCmp('cmbap').store;
  var index = comboxStore.find('uri',v);
  if(index<0) return;
  return comboxStore.getAt(index).get('name');
}
},
editor : new Ext.form.ComboBox({
typeAhead : true,
triggerAction : 'all',
emptyText : '请选择...',
id:'cmbap',
store :this.myPStore,
valueField : 'uri', // 传送的值
displayField : 'name', // UI列表显示的文本
editable : false,
mode : 'local' // 数据模式,romote远程模式/local为本地模式
})
},
{
header : '表计数据单元',
dataIndex : 'ammeterBuff',
width:150,
editor : new Ext.form.TextField({
allowBlank : false
})
}]);
},
getStore : function() {
var store = new Ext.data.JsonStore({
url : 'TaskItemAction?op=query',
method : 'POST',
root : 'datas',
fields : ['name', 'uri', 'termBuff', 'ammeterBuff','protocolUri','ammeterProtocolUri'],
totalProperty : 'totalProperty'
});

store.load({
params : {
start : 0,
limit : 1
}
});

return store;
},
getMyPStore:function()
{
        var store = new Ext.data.JsonStore({ // 填充的数据
fields : ['name','uri'],
url:'ProtocolAction',
root:'PList',
method:'POST'
});
store.load();//实例化时即加载,这样mode:可以设置成local
return store;
},
SaveFn : function() {
var count = this.mystore.getCount();
var store = this.mystore;
var record;
var array = new Array();
var modifiedId;
var modifiedJSON;
var hasModify = false;// 是否有更改的记录

for (var i = 0; i < count; i++) {
record = this.mystore.getAt(i);

if (true == record.dirty)// 如果记录被更改过
{
var uri = record.get('uri');
// modifiedJSON = Ext.util.JSON.encode(record.getChanges());
modifiedJSON = Ext.util.JSON.encode(record.data);// 这里将整个记录回传
array.push([modifiedJSON]);
hasModify = true;
}
}
if (!hasModify) {
Ext.Msg.alert('提示', '没有添加或更改的记录!');
return
};
var srchValue = encodeURI(this.searchValue);
Ext.Ajax.request({
url : 'TaskItemAction?op=update',
method : 'post',
params : {
array : array.join('@')
},
callback : function(options, success, response) {
if (success == true) {
var remoteData = Ext.decode(response.responseText);
if(remoteData.result.success)
{
Ext.Msg.alert('提示', '数据更新成功');
}else
{
                                Ext.Msg.alert('提示', remoteData.result.msg);return;
}

} else {
Ext.Msg.alert('提示', '服务器端出现异常,稍候再试!');
}
store.proxy = new Ext.data.HttpProxy({
url : 'TaskItemAction?op=query&ex='
+ srchValue
});
store.load();
}
}

);
}
});

你可能感兴趣的:(Ajax,json,UI,ext)