Ext.QuickTips.init(); Ext.define('erp.controller.common.RelativeSearch', { extend: 'Ext.app.Controller', requires: ['erp.util.BaseUtil'], views:[ 'common.RelativeSearch' ], init:function(){ var me = this; me.BaseUtil = Ext.create('erp.util.BaseUtil'); this.control({ 'tabpanel': { afterrender: function(tb) { this.getRelativeSearch(tb, caller); }, add: function(t, p) { p.on('activate', function(){ if(!p.down('gridpanel') && p.datas) { var _f = p.datas.form, _g = p.datas.grid; p.add({ xtype: 'grid', columnLines: true, layout: 'fit', autoScroll : true, anchor: '100% 85%', selModel: Ext.create('Ext.selection.CheckboxModel',{ headerWidth: 0, views: [] }), headerCt: Ext.create("Ext.grid.header.Container", { suspendLayout: true }), plugins: [Ext.create('Ext.ux.grid.GridHeaderFilters')], dockedItems: [new Ext.toolbar.Paging(me.pagingConfig)], tableName: _f.tablename, defaultCondition: _f.condition, fields: Ext.Array.concate(_g.gridFields, ',', 'name'), store: new Ext.data.Store({ fields: _g.gridFields, data: [] }), listeners: { scrollershow: function(scroller) { if (scroller && scroller.scrollEl) { scroller.clearManagedListeners(); scroller.mon(scroller.scrollEl, 'scroll', scroller.onElScroll, scroller); } } }, columns: new Array(), getGridData: function(page) { me.query(this.ownerCt.down('form'), this, page); } }); var cm = _g.gridColumns, fn = me.link; Ext.each(cm, function(){ if(this.logic) { this.renderer = fn; } }); p.down('grid').reconfigure(new Ext.data.Store({ fields: _g.gridFields, data: [{},{},{},{},{},{},{},{},{},{}] }), cm); } else { var g = p.down('gridpanel'); g.getGridData(g.plugins[0].page || 1); } }); } }, 'field' : { afterrender : function(f) { var name = f.name, form = f.ownerCt, c = form.down('combo'); if (!c) { var a = parent.Ext.getCmp(name); if (a) { f.setValue(a.getValue()); } } if(f.logic) { var s = parent.Ext.getCmp(name); if(s) { f.setValue(s.getValue()); } } } }, 'combo': { afterrender: function(f) { var cm = parent.Ext.ComponentQuery.query('grid[relative=true]'); var grid = cm.length > 0 ? cm[0] : parent.Ext.getCmp('grid'); if(grid) { var d = []; grid.store.each(function(item){ if(!Ext.isEmpty(item.get(f.name))) { d.push({ display: item.get(f.name), value: item.get(f.name), data: item.data }); } }); f.store = Ext.create('Ext.data.Store', { fields: ['display','value','data'], data: d }); //根据选中行直接筛选 var lastselected=grid.getSelectionModel().selected.items[0]; if(lastselected){ f.setValue(lastselected.get(f.name)); }else if(d.length > 0) f.setValue(d[0].value); } }, change: function(f) { if(!Ext.isEmpty(f.value)) { var form = f.ownerCt; if (f.lastSelection[0]) { var d = f.lastSelection[0].data.data; form.getForm().getFields().each(function(e){ typeof d[e.name] !== 'undefined' && (e.setValue(d[e.name])); }); } var grid = form.ownerCt.down('grid'); if(grid && grid.rendered) { grid.plugins[0].clearFilters(); this.query(form, grid, 1); } } } }, 'gridpanel': { reconfigure: function(grid) { grid.reconfigured = true; this.query(grid.ownerCt.down('form'), grid, 1); }, headerfiltersapply: function(grid, filters) { if(grid.reconfigured){ var condition = null; for(var fn in filters){ var value = filters[fn], f = grid.getHeaderFilterField(fn); if(!Ext.isEmpty(value)){ if(f.filtertype) { if (f.filtertype == 'numberfield') { value = fn + "=" + value + " "; } } else { if(Ext.isDate(value)){ value = Ext.Date.toString(value); value = fn + "=to_date('" + value + "','yyyy-MM-dd') "; } 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 = fn + "=to_date('" + value + "','yyyy-MM-dd') "; } else if(exp_t.test(value)){ value = fn + "=to_date('" + value + "','yyyy-MM-dd HH24:mi:ss') "; } else{ value = fn + " LIKE '%" + value + "%' "; } } } if(condition == null){ condition = value; } else { condition = condition + " AND " + value; } } } grid.filterCondition = condition; grid.getGridData(1); } return false; } }, 'button[name=export]': { click: function(btn) { var grid = btn.up('gridpanel'); if (grid) { var tb = grid.ownerCt, form = tb.down('form'), cols = grid.fields, con = this.getQueryCondition(form, grid); this.BaseUtil.customExport(caller, grid, tb.title, '/common/form/relativeSearch.xls', con, { _id: form.rs_id, _tab: grid.tableName, _fies: cols }); } } } }); }, getRelativeSearch: function(tb, cal) { var me = this; Ext.Ajax.request({ url: basePath + 'common/form/relativeSearch.action', params: { caller: cal }, callback: function(opt, s, r) { if (s) { var rs = Ext.decode(r.responseText); if(rs.data) { var s = null, _f = null; for(var i in rs.data) { s = rs.data[i]; _f = s.form; _g = s.grid; tb.add({ title: _f.title, datas: s, items: [{ xtype: 'form', layout: 'column', items: _f.items, rs_id: _f.fo_id, cls: 'custom', bodyStyle: 'background: #f1f1f1;border: none;', fieldDefaults: { margin: '6 0 0 0', labelWidth: 70 }, anchor: '100% 15%', buttonAlign: 'center', buttons: [{ text: $I18N.common.button.erpQueryButton, iconCls: 'x-button-icon-query', cls: 'x-btn-blue', handler: function(btn) { var f = btn.ownerCt.ownerCt, g = f.ownerCt.down('grid'); me.query(f, g, 1); } },{ text: $I18N.common.button.erpCloseButton, iconCls: 'x-button-icon-close', cls: 'x-btn-blue', handler: function() { var w = parent.Ext.getCmp('ext-relative-query'); if (w) { w.hide(); } } }] }] }); } if(tb.items.items.length > 0) { var p = tb.items.items[0]; p.fireEvent('activate', p); } } } } }); }, getQueryCondition: function(form, grid) { var con = grid.defaultCondition, fileter = grid.filterCondition; if(!Ext.isEmpty(con)) { con = "(" + con + ")"; } if(!Ext.isEmpty(fileter)) { if(!Ext.isEmpty(con)) { con += " AND (" + fileter + ")"; } else { con = fileter; } } form.getForm().getFields().each(function(f){ if(f.logic) { if(f.logic.indexOf('to:') > -1) { var _field = f.logic.split('to:')[1]; if(!Ext.isEmpty(con)) { if(f.xtype == 'datefield') { con += " AND (to_char(" + _field + ",'yyyymmdd')='" + Ext.Date.format(f.value, 'Ymd') + "')"; } else { con += " AND (" + _field + "='" + f.value + "')"; } } else { if(f.xtype == 'datefield') { con = " to_char(" + _field + ",'yyyymmdd')='" + Ext.Date.format(f.value, 'Ymd') + "'"; } else { con = _field + "='" + f.value + "'"; } } } } }); return con; }, pageSize: 12, query: function(form, grid, page) { var cols = grid.fields, pageSize = this.pageSize, start = (page - 1) * pageSize + 1, end = page * pageSize, con = this.getQueryCondition(form, grid); grid.setLoading(true); Ext.Ajax.request({ url: basePath + 'common/form/search.action', params: { _id: form.rs_id, _tab: grid.tableName, _fies: cols, _start: start, _end: end, _cond: con }, callback: function(opt, s, r) { grid.setLoading(false); var rs = Ext.decode(r.responseText); if(rs.data) { grid.store.loadData(rs.data); grid.down('pagingtoolbar').page = page; grid.down('pagingtoolbar').dataCount = rs.count; grid.down('pagingtoolbar').onLoad(); } } }); }, pagingConfig: { dock: 'bottom', displayInfo: true, pageSize: 12, items: ['-',{ name: 'export', tooltip: $I18N.common.button.erpExportButton, iconCls: 'x-button-icon-excel', cls: 'x-btn-tb', width: 24, handler: function(){ } }], updateInfo : function(){ var page = this.child('#inputItem').getValue() || 1; var me = this, pageSize = 12, dataCount = me.dataCount || 0; var displayItem = me.child('#displayItem'), pageData = me.getPageData(); pageData.fromRecord = (page-1)*pageSize+1; pageData.toRecord = page*pageSize > dataCount ? dataCount : page*pageSize; pageData.total = dataCount; var msg; if (displayItem) { if (me.dataCount === 0) { msg = me.emptyMsg; } else { msg = Ext.String.format( me.displayMsg, pageData.fromRecord, pageData.toRecord, pageData.total ); } displayItem.setText(msg); me.doComponentLayout(); } }, getPageData : function(){ var me = this, totalCount = me.dataCount; return { total : totalCount, currentPage : me.page, pageCount: Math.ceil(me.dataCount / me.pageSize), fromRecord: ((me.page - 1) * me.pageSize) + 1, toRecord: Math.min(me.page * me.pageSize, totalCount) }; }, doRefresh:function(){ this.moveFirst(); }, onPagingKeyDown : function(field, e){ var me = this, k = e.getKey(), pageData = me.getPageData(), increment = e.shiftKey ? 10 : 1, pageNum = 0; if (k == e.RETURN) { e.stopEvent(); pageNum = me.readPageFromInput(pageData); if (pageNum !== false) { pageNum = Math.min(Math.max(1, pageNum), pageData.pageCount); me.child('#inputItem').setValue(pageNum); if(me.fireEvent('beforechange', me, pageNum) !== false){ me.page = pageNum; me.ownerCt.getGridData(me.page); } } } else if (k == e.HOME || k == e.END) { e.stopEvent(); pageNum = k == e.HOME ? 1 : pageData.pageCount; field.setValue(pageNum); } else if (k == e.UP || k == e.PAGEUP || k == e.DOWN || k == e.PAGEDOWN) { e.stopEvent(); pageNum = me.readPageFromInput(pageData); if (pageNum) { if (k == e.DOWN || k == e.PAGEDOWN) { increment *= -1; } pageNum += increment; if (pageNum >= 1 && pageNum <= pageData.pages) { field.setValue(pageNum); } } } me.updateInfo(); me.resetTool(pageNum); }, moveFirst : function(){ var me = this; me.child('#inputItem').setValue(1); value = 1; me.page = value; me.ownerCt.getGridData(value); me.updateInfo(); me.resetTool(value); }, movePrevious : function(){ var me = this; me.child('#inputItem').setValue(me.child('#inputItem').getValue() - 1); value = me.child('#inputItem').getValue(); me.page = value; me.ownerCt.getGridData(value); me.updateInfo(); me.resetTool(value); }, moveNext : function(){ var me = this, last = me.getPageData().pageCount; total = last; me.child('#inputItem').setValue(me.child('#inputItem').getValue() + 1); value = me.child('#inputItem').getValue(); me.page = value; me.ownerCt.getGridData(value); me.updateInfo(); me.resetTool(value); }, moveLast : function(){ var me = this, last = me.getPageData().pageCount; total = last; me.child('#inputItem').setValue(last); value = me.child('#inputItem').getValue(); me.page = value; me.ownerCt.getGridData(value); me.updateInfo(); me.resetTool(value); }, onLoad : function() { var e = this, d, b, c, a; if (!e.rendered) { return } d = e.getPageData(); b = d.currentPage || 1; c = Math.ceil(e.dataCount / e.pageSize); a = Ext.String.format(e.afterPageText, isNaN(c) ? 1 : c); e.child("#afterTextItem").setText(a); e.child("#inputItem").setValue(b); e.child("#first").setDisabled(b === 1); e.child("#prev").setDisabled(b === 1); e.child("#next").setDisabled(b === c || c===1);// e.child("#last").setDisabled(b === c || c===1); e.child("#refresh").enable(); e.updateInfo(); e.fireEvent("change", e, d); }, resetTool: function(value){ var pageCount = this.getPageData().pageCount; this.child('#last').setDisabled(value == pageCount || pageCount == 1); this.child('#next').setDisabled(value == pageCount || pageCount == 1); this.child('#first').setDisabled(value <= 1); this.child('#prev').setDisabled(value <= 1); } }, link: function(val, m, record, x, y, store, view) { var grid = view.ownerCt, column = grid.columns[y], url = column.logic; if(url) { var index = 0, length = url.length, s, e; while(index < length) { if((s = url.indexOf('{', index)) != -1 && (e = url.indexOf('}', s + 1)) != -1) { url = url.substring(0, s) + record.get(url.substring(s+1, e)) + url.substring(e+1); index = e + 1; } else { break; } } return '' + val + ''; } return val; } });