Ext.define('erp.view.common.dbfind.GridPanel',{ extend: 'Ext.grid.Panel', alias: 'widget.erpDbfindGridPanel', layout : 'fit', id: 'dbfindGridPanel', emptyText : $I18N.common.grid.emptyText, columnLines : true, autoScroll : true, store: [], columns: [], selModel: Ext.create('Ext.selection.CheckboxModel',{ headerWidth: 0 }), BaseUtil: Ext.create('erp.util.BaseUtil'), bodyStyle:'background-color:#f1f1f1;', dockedItems: [{ id : 'pagingtoolbar', xtype: 'erpDbfindToolbar', dock: 'bottom', displayInfo: true }], plugins: [Ext.create('Ext.ux.grid.GridHeaderFilters')], initComponent : function(){ this.callParent(arguments); this.getCount(); }, getColumnsAndStore: function(){ var me = this; var c = condition; if(typeof trigger.getExtraCondition=='function'){ var con = trigger.getExtraCondition(); if(con!=''){ c += ' AND ' + con; } } if(me.filterCondition){ c += ' AND ' + me.filterCondition; } me.setLoading(true); Ext.Ajax.request({//拿到grid的columns url : basePath + 'common/dbfind.action', method : 'post', params : { which : which, caller : caller, field: key, condition: c, ob: dbOrderby,// dynamic order by page: page, pageSize: pageSize }, callback : function(options,success,response){ me.setLoading(false); var res = new Ext.decode(response.responseText); if(res.exceptionInfo){ showError(res.exceptionInfo);return; } if(res.autoHeight) { me.addCls('custom-grid-autoheight');//自适应样式 } var data = Ext.decode(res.data.replace(/,}/g, '}').replace(/,]/g, ']')); if(me.columns && me.columns.length > 2){ me.store.loadData(data); if(me.store.data.items.length != data.length){ me.store.add(data); } //修改pagingtoolbar信息 Ext.getCmp('pagingtoolbar').afterOnLoad(); } else { me.reconfigure(Ext.create('Ext.data.Store', { storeId: 'dbfindGridPanelStore', fields: res.fields, data: data }), res.columns); //修改pagingtoolbar信息 Ext.getCmp('pagingtoolbar').afterOnLoad(); dbfinds = res.dbfinds; //通用变更单dbfind 采用变更原有单据的放大镜 if(trigger.isCommonChange){ Ext.Array.each(dbfinds,function(d){ d.field=d.field+'-new'; }); } me.resetable = res.reset;//允许重置条件 trigger.dbfinds = dbfinds; if(me.resetable){ var win = parent.Ext.getCmp('dbwin'); win && win.down('#reset').show(); } } } }); }, getCount: function(){ var me = this; if(condition)condition=condition.replace(/\%'%/g,"%''%"); var c = condition; if(typeof trigger.getExtraCondition=='function'){ var con = trigger.getExtraCondition(); if(con!=''){ c += ' AND ' + con; } } if(me.filterCondition){ me.filterCondition=me.filterCondition.replace(/\%'%/g,"%''%"); c += ' AND ' + me.filterCondition; } Ext.Ajax.request({//拿到grid的数据总数count url : basePath + 'common/dbfindCount.action', params : { which : which, caller : caller, field: key, condition: c }, method : 'post', callback : function(options,success,response){ var res = new Ext.decode(response.responseText); dataCount = res.count; me.getColumnsAndStore(); } }); }, listeners: { 'headerfiltersapply': function(grid, filters) { if(this.allowFilter){ var condition = null; for(var fn in filters){ var value = filters[fn], f = grid.getHeaderFilterField(fn); if(f.xtype == 'datefield') value = f.getRawValue(); if(!Ext.isEmpty(value)) { if("null"!=value){ if(f.filtertype) { if (f.filtertype == 'numberfield') { value = fn + "=" + value + " "; } } else { if(Ext.isDate(value)){ value = Ext.Date.toString(value); value = "to_char(" + fn + ",'yyyy-MM-dd')='" + value + "' "; } else { var exp_t = /^(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/, exp_d = /^(\d{4})\-(\d{2})\-(\d{2})$/; if(exp_d.test(value)){ value = "to_char(" + fn + ",'yyyy-MM-dd')='" + value + "' "; } else if(exp_t.test(value)){ value = "to_char(" + fn + ",'yyyy-MM-dd')='" + value.substr(0, 10) + "' "; } else{ if (f.xtype == 'combo' || f.xtype == 'combofield') { if (value == '-所有-') { continue; } else if (value == '-无-') { value = 'nvl(' + fn + ',\' \')=\' \''; } else { value = fn + " LIKE '" + value + "%' "; } } else if(f.xtype == 'datefield') { value = "to_char(" + fn + ",'yyyy-MM-dd') like '%" + value + "%' "; } else if(f.column && f.column.xtype == 'numbercolumn'){ if(f.column.format) { var precision = f.column.format.substr(f.column.format.indexOf('.') + 1).length; value = "to_char(round(" + fn + "," + precision + ")) like '%" + value + "%' "; } else value = "to_char(" + fn + ") like '%" + value + "%' "; } else { /**字符串转换下简体*/ var SimplizedValue=this.BaseUtil.Simplized(value); //可能就是按繁体筛选 if(f.ignoreCase) {// 忽略大小写 fn = 'upper(' + fn + ')'; value = value.toUpperCase(); } if(!f.autoDim) { if(SimplizedValue!=value){ value = "("+fn + " LIKE '" + value + "%' or "+fn+" LIKE '"+SimplizedValue+"%')"; }else value = fn + " LIKE '" + value + "%' "; } else if(f.exactSearch){ value=fn+"='"+value+"'"; } else { if(SimplizedValue!=value){ value = "("+fn + " LIKE '%" + value + "%' or "+fn+" LIKE '%"+SimplizedValue+"%')"; }else value = fn + " LIKE '%" + value + "%' "; } } } } } }else value ="nvl("+fn+",' ')=' '"; if(condition == null){ condition = value; } else { condition = condition + " AND " + value; } } } this.filterCondition = condition; page = 1; this.getCount(); } else { this.allowFilter = true; } return false; } }, reconfigure: function(store, columns){ //改写reconfigure方法 var d = this.headerCt; if (this.columns.length <= 1 && columns) {//this.columns.length > 1表示grid的columns已存在,没必要remove再add if(!Ext.isChrome){//ie,firefox下,format出现NaN-NaN-NaN,暂时作string处理 Ext.each(columns, function(c){ if(c.xtype == 'datecolumn'){ c.xtype = ""; c.format = ""; } }); } d.suspendLayout = true; d.removeAll(); d.add(columns); } if (store) { if(!Ext.isChrome){//ie,firefox下,format出现NaN-NaN-NaN Ext.each(store.fields, function(f){ if(f.type == 'date'){ f.type = "string"; f.format = ""; } }); } this.bindStore(store); } else { this.getView().refresh(); } if (columns) { d.suspendLayout = false; this.forceComponentLayout(); } this.fireEvent("reconfigure", this); }, /** * 重置条件 * dbfindSet.ds_allowreset(1-允许) * dbfindSetUI.ds_allowreset(1-允许) */ resetCondition: function(){ condition = this.getCondition(); this.filterCondition = ''; var fields = this.plugins[0].fields; Ext.each(Ext.Object.getKeys(fields), function(key){ fields[key].reset(); }); }, getCondition: function() { condition = 'upper(' + key + ") like '%%'"; if(!trigger.ownerCt || trigger.column){//如果是grid的dbfind condition = 'upper(' + keyField + ") like '%%'"; if(dbGridCondition && dbGridCondition != null){ condition += " AND " + decodeURIComponent(dbGridCondition).replace(/\s{1}IS\s{1}/g, '='); } } if(dbCondition && dbCondition != null){ condition += " AND " + decodeURIComponent(dbCondition).replace(/\s{1}IS\s{1}/g, '='); } if(dbBaseCondition && dbBaseCondition != null){ condition += " AND " + decodeURIComponent(dbBaseCondition).replace(/\s{1}IS\s{1}/g, '=').replace(/@/g,'%'); } return condition; } });