| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- /**
- * dbfind trigger
- * 支持带条件dbfind
- */
- Ext.define('erp.view.core.trigger.DbfindTrigger', {
- extend: 'Ext.form.field.Trigger',
- alias: 'widget.dbfindtrigger',
- triggerCls: 'x-form-search-trigger',
- autoDbfind: true,
- initComponent: function() {
- var me = this;
- me.addEvents({
- aftertrigger: true,
- beforetrigger: true
- });
- me.callParent(arguments);
- if(!me.ownerCt) {
- Ext.defer(function(){
- me.getOwner();
- }, 50);
- }
- if(me.clearable) {
- me.trigger2Cls = 'x-form-clear-trigger';
- if(!me.onTrigger2Click) {
- me.onTrigger2Click = function(){
- this.setValue(null);
- };
- }
- }
- },
- getOwner: function() {
- var me = this;
- if (me.el) {
- var gridEl = me.el.up('.x-grid');
- if (gridEl) {
- var grid = Ext.getCmp(gridEl.id);
- if (grid) {
- me.owner = grid;
- me.column = grid.down('gridcolumn[dataIndex=' + me.name + ']');
- }
- }
- }
- },
- listeners: { //功能:自动取数据(当前dbfind结果只有一条数据时,直接赋值,无需弹出window)
- focus: function(f) {
- if (!f.readOnly) {
- var trigger = this;
- trigger.lastTriggerId = trigger.id;
- if (!trigger.ownerCt) {
- if (!trigger.owner) {
- trigger.getOwner();
- }
- if (trigger.owner.editingPlugin.activeEditor.field.id == trigger.id) {
- trigger.record = trigger.owner.editingPlugin.activeRecord;
- } else {
- trigger.record = trigger.owner.selModel.lastSelected;
- }
- var index = trigger.owner.store.indexOf(trigger.record);
- if (index != null) {
- trigger.lastTriggerId = trigger.id + '---' + index;
- } else {
- trigger.lastTriggerId = null;
- }
- }
- }
- },
- blur: function(f) {
- if (this.lastTriggerId && !f.readOnly) {
- var which = 'form';
- var cal = this.dbCaller||caller;
- var key = this.name;
- var con = key + " like '%" + this.value + "%'";
- var currrecord = null;
- if (contains(this.lastTriggerId, '---', true) && !this.ownerCt) {
- which = 'grid';
- dbfind = this.column.dbfind;
- cal = dbfind.split('|')[0];
- con = dbfind.split('|')[1] + " like '%" + this.value + "%'";
- currrecord = this.owner.selModel.lastSelected;
- if (this.value != null && this.value != '') {
- var record = this.owner.store.getAt(this.lastTriggerId.split('---')[1]);
- this.owner.selModel.select(record);
- }
- }
- if (this.value != null && this.value != '' && this.lastTriggerId && !this.readOnly && this.autoDbfind) {
- this.autoDbfind(which, cal, key, con); //光标移开后自动dbfind
- if (currrecord) {
- this.owner.selModel.select(currrecord);
- }
- }
- }
- }
- },
- onTriggerClick: function() {
- var trigger = this,
- bool = true; // 放大镜所在
- bool = trigger.fireEvent('beforetrigger', trigger);
- dbCaller=this.dbCaller|| (typeof caller === 'undefined' ? '' : caller);
- if (bool == false) {
- return;
- }
- this.setFieldStyle('background:#C6E2FF;');
- var key = this.name,
- // name属性
- dbfind = '',
- // 需要dbfind的表和字段
- dbBaseCondition = '',
- dbCondition = '',
- dbGridCondition = '',
- findConfig = this.findConfig,
- dbKey = this.dbKey,
- mappingKey = this.mappingKey,
- gridKey = this.gridKey,
- mappinggirdKey = this.mappinggirdKey;
- window.onTriggerClick = this.id;
- // 存在查询条件的字段
- if (findConfig) {
- dbCondition = findConfig;
- }
- if (dbKey) {
- var dbKeyValue = Ext.getCmp(dbKey).value;
- if (dbKeyValue) {
- dbCondition = mappingKey + " IS '" + dbKeyValue + "'";
- } else {
- showError(this.dbMessage);
- return
- }
- }
- if (gridKey) {
- var gridKeys = gridKey.split('|');
- var mappinggirdKeys = mappinggirdKey.split('|');
- var gridErrorMessages = this.gridErrorMessage.split('|');
- for (var i = 0; i < gridKeys.length; i++) {
- var gridkeyvalue = Ext.getCmp(gridKeys[i]).value;
- if (i == 0) {
- if (gridkeyvalue) {
- dbGridCondition = mappinggirdKeys[i] + " IS '" + gridkeyvalue + "' ";
- } else {
- showError(gridErrorMessages[i]);
- return
- }
- } else {
- if (gridkeyvalue) {
- dbGridCondition = dbGridCondition + " AND " + mappinggirdKeys[i] + " IS '" + gridkeyvalue + "' ";
- } else {
- showError(gridErrorMessages[i]);
- return
- }
- }
- }
- }
- if (this.dbBaseCondition) {
- dbBaseCondition = this.dbBaseCondition;
- }
- if (!trigger.ownerCt) { // 如果是grid的dbfind
- var grid = Ext.ComponentQuery.query('gridpanel');
- if(!trigger.dbfind){
- Ext.Array.each(grid,
- function(g, index) {
- Ext.Array.each(g.columns,
- function(column) {
- if (column.dataIndex == key ) {
- dbfind = column.dbfind;
- trigger.owner = g;
- }
- });
- });
- }else dbfind=trigger.dbfind;
- }
- var keyValue = this.value, ob = this.dbOrderby || ''; // 当前值
- keyValue = keyValue == null ? '': keyValue;
- var width = Ext.isIE ? screen.width * 0.7 * 0.9 : '80%',
- height = Ext.isIE ? screen.height * 0.75 : '95%';
- //针对有些特殊窗口显示较小
- width =this.winWidth ? this.winWidth:width;
- height=this.winHeight ? this.winHeight:height;
- var dbwin = new Ext.window.Window({
- id: 'dbwin',
- title: '查找',
- height: height,
- width: width,
- maximizable: true,
- buttonAlign: 'center',
- layout: 'anchor',
- items: [{
- tag: 'iframe',
- frame: true,
- anchor: '100% 100%',
- layout: 'fit',
- html: '<iframe id="iframe_dbfind" src="' + basePath + 'jsps/common/dbfind.jsp?key=' + key + "&dbfind=" + dbfind + "&dbGridCondition=" + dbGridCondition + "&dbCondition=" + dbCondition + "&dbBaseCondition=" + dbBaseCondition + "&keyValue=" + encodeURIComponent(keyValue) + "&trigger=" + trigger.id + "&caller=" + dbCaller + "&ob=" + ob + '" height="100%" width="100%" frameborder="0" scrolling="no"></iframe>'
- }],
- buttons: [{
- text: '关 闭',
- iconCls: 'x-button-icon-close',
- cls: 'x-btn-gray',
- handler: function() {
- Ext.getCmp('dbwin').close();
- }
- },
- {
- text: '重置条件',
- id: 'reset',
- cls: 'x-btn-gray',
- hidden: true,
- handler: function() {
- var grid = Ext.getCmp('dbwin').el.dom.getElementsByTagName('iframe')[0].contentWindow.document.defaultView.Ext.getCmp('dbfindGridPanel');
- grid.resetCondition();
- grid.getCount();
- }
- }]
- });
- dbwin.show();
- trigger.lastTriggerId = null;
- },
- autoDbfind: function(which, caller, field, condition) {
- var me = this;
- Ext.Ajax.request({
- url: basePath + 'common/autoDbfind.action',
- params: {
- which: which,
- caller: caller,
- field: field,
- condition: condition
- },
- async: false,
- method: 'post',
- callback: function(options, success, response) {
- var res = new Ext.decode(response.responseText);
- if (res.exceptionInfo) {
- showError(res.exceptionInfo);
- return;
- }
- if (res.data) {
- var data = Ext.decode(res.data.replace(/,}/g, '}').replace(/,]/g, ']'));
- me.autoSetValue(data[0], res.dbfinds || me.owner.dbfinds);
- } else {
- me.onTriggerClick();
- }
- }
- });
- },
- autoSetValue: function(data, dbfinds) {
- var trigger = this;
- var triggerV = null;
- if (!trigger.ownerCt) { //如果是grid的dbfind
- var grid = trigger.owner;
- var record = grid.lastSelectedRecord || trigger.record || grid.getSelectionModel().selected.items[0] || grid.selModel.lastSelected; //detailgrid里面selected
- Ext.Array.each(Ext.Object.getKeys(data),
- function(k) {
- Ext.Array.each(dbfinds,
- function(ds) {
- if (ds.trigger == trigger.name || Ext.isEmpty(ds.trigger)) {
- if (Ext.Array.contains(ds.dbGridField.split(';'), k)) { //k == ds.dbGridField//支持多dbgrid的字段对应grid同一字段
- if (ds.field == trigger.name) {
- triggerV = data[k]; //trigger所在位置赋值
- }
- record.set(ds.field, data[k]);
- }
- }
- });
- });
- } else {
- var ff;
- Ext.Array.each(Ext.Object.getKeys(data),
- function(k) {
- Ext.Array.each(dbfinds,
- function(ds) {
- if (k == ds.dbGridField) {
- if (Ext.getCmp(ds.field)) {
- if (trigger.name == ds.field) {
- triggerV = data[k];
- } else {
- ff = Ext.getCmp(ds.field);
- if (ff && ff.setValue) ff.setValue(data[k]);
- }
- }
- }
- });
- });
- }
- trigger.setValue(triggerV);
- data.data = data;
- trigger.fireEvent('aftertrigger', trigger, data);
- trigger.lastTriggerId = null;
- }
- });
|