DbfindTrigger.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /**
  2. * dbfind trigger
  3. * 支持带条件dbfind
  4. */
  5. Ext.define('erp.view.core.trigger.DbfindTrigger', {
  6. extend: 'Ext.form.field.Trigger',
  7. alias: 'widget.dbfindtrigger',
  8. triggerCls: 'x-form-search-trigger',
  9. autoDbfind: true,
  10. initComponent: function() {
  11. var me = this;
  12. me.addEvents({
  13. aftertrigger: true,
  14. beforetrigger: true
  15. });
  16. me.callParent(arguments);
  17. if(!me.ownerCt) {
  18. Ext.defer(function(){
  19. me.getOwner();
  20. }, 50);
  21. }
  22. if(me.clearable) {
  23. me.trigger2Cls = 'x-form-clear-trigger';
  24. if(!me.onTrigger2Click) {
  25. me.onTrigger2Click = function(){
  26. this.setValue(null);
  27. };
  28. }
  29. }
  30. },
  31. getOwner: function() {
  32. var me = this;
  33. if (me.el) {
  34. var gridEl = me.el.up('.x-grid');
  35. if (gridEl) {
  36. var grid = Ext.getCmp(gridEl.id);
  37. if (grid) {
  38. me.owner = grid;
  39. me.column = grid.down('gridcolumn[dataIndex=' + me.name + ']');
  40. }
  41. }
  42. }
  43. },
  44. listeners: { //功能:自动取数据(当前dbfind结果只有一条数据时,直接赋值,无需弹出window)
  45. focus: function(f) {
  46. if (!f.readOnly) {
  47. var trigger = this;
  48. trigger.lastTriggerId = trigger.id;
  49. if (!trigger.ownerCt) {
  50. if (!trigger.owner) {
  51. trigger.getOwner();
  52. }
  53. if (trigger.owner.editingPlugin.activeEditor.field.id == trigger.id) {
  54. trigger.record = trigger.owner.editingPlugin.activeRecord;
  55. } else {
  56. trigger.record = trigger.owner.selModel.lastSelected;
  57. }
  58. var index = trigger.owner.store.indexOf(trigger.record);
  59. if (index != null) {
  60. trigger.lastTriggerId = trigger.id + '---' + index;
  61. } else {
  62. trigger.lastTriggerId = null;
  63. }
  64. }
  65. }
  66. },
  67. blur: function(f) {
  68. if (this.lastTriggerId && !f.readOnly) {
  69. var which = 'form';
  70. var cal = this.dbCaller||caller;
  71. var key = this.name;
  72. var con = key + " like '%" + this.value + "%'";
  73. var currrecord = null;
  74. if (contains(this.lastTriggerId, '---', true) && !this.ownerCt) {
  75. which = 'grid';
  76. dbfind = this.column.dbfind;
  77. cal = dbfind.split('|')[0];
  78. con = dbfind.split('|')[1] + " like '%" + this.value + "%'";
  79. currrecord = this.owner.selModel.lastSelected;
  80. if (this.value != null && this.value != '') {
  81. var record = this.owner.store.getAt(this.lastTriggerId.split('---')[1]);
  82. this.owner.selModel.select(record);
  83. }
  84. }
  85. if (this.value != null && this.value != '' && this.lastTriggerId && !this.readOnly && this.autoDbfind) {
  86. this.autoDbfind(which, cal, key, con); //光标移开后自动dbfind
  87. if (currrecord) {
  88. this.owner.selModel.select(currrecord);
  89. }
  90. }
  91. }
  92. }
  93. },
  94. onTriggerClick: function() {
  95. var trigger = this,
  96. bool = true; // 放大镜所在
  97. bool = trigger.fireEvent('beforetrigger', trigger);
  98. dbCaller=this.dbCaller|| (typeof caller === 'undefined' ? '' : caller);
  99. if (bool == false) {
  100. return;
  101. }
  102. this.setFieldStyle('background:#C6E2FF;');
  103. var key = this.name,
  104. // name属性
  105. dbfind = '',
  106. // 需要dbfind的表和字段
  107. dbBaseCondition = '',
  108. dbCondition = '',
  109. dbGridCondition = '',
  110. findConfig = this.findConfig,
  111. dbKey = this.dbKey,
  112. mappingKey = this.mappingKey,
  113. gridKey = this.gridKey,
  114. mappinggirdKey = this.mappinggirdKey;
  115. window.onTriggerClick = this.id;
  116. // 存在查询条件的字段
  117. if (findConfig) {
  118. dbCondition = findConfig;
  119. }
  120. if (dbKey) {
  121. var dbKeyValue = Ext.getCmp(dbKey).value;
  122. if (dbKeyValue) {
  123. dbCondition = mappingKey + " IS '" + dbKeyValue + "'";
  124. } else {
  125. showError(this.dbMessage);
  126. return
  127. }
  128. }
  129. if (gridKey) {
  130. var gridKeys = gridKey.split('|');
  131. var mappinggirdKeys = mappinggirdKey.split('|');
  132. var gridErrorMessages = this.gridErrorMessage.split('|');
  133. for (var i = 0; i < gridKeys.length; i++) {
  134. var gridkeyvalue = Ext.getCmp(gridKeys[i]).value;
  135. if (i == 0) {
  136. if (gridkeyvalue) {
  137. dbGridCondition = mappinggirdKeys[i] + " IS '" + gridkeyvalue + "' ";
  138. } else {
  139. showError(gridErrorMessages[i]);
  140. return
  141. }
  142. } else {
  143. if (gridkeyvalue) {
  144. dbGridCondition = dbGridCondition + " AND " + mappinggirdKeys[i] + " IS '" + gridkeyvalue + "' ";
  145. } else {
  146. showError(gridErrorMessages[i]);
  147. return
  148. }
  149. }
  150. }
  151. }
  152. if (this.dbBaseCondition) {
  153. dbBaseCondition = this.dbBaseCondition;
  154. }
  155. if (!trigger.ownerCt) { // 如果是grid的dbfind
  156. var grid = Ext.ComponentQuery.query('gridpanel');
  157. if(!trigger.dbfind){
  158. Ext.Array.each(grid,
  159. function(g, index) {
  160. Ext.Array.each(g.columns,
  161. function(column) {
  162. if (column.dataIndex == key ) {
  163. dbfind = column.dbfind;
  164. trigger.owner = g;
  165. }
  166. });
  167. });
  168. }else dbfind=trigger.dbfind;
  169. }
  170. var keyValue = this.value, ob = this.dbOrderby || ''; // 当前值
  171. keyValue = keyValue == null ? '': keyValue;
  172. var width = Ext.isIE ? screen.width * 0.7 * 0.9 : '80%',
  173. height = Ext.isIE ? screen.height * 0.75 : '95%';
  174. //针对有些特殊窗口显示较小
  175. width =this.winWidth ? this.winWidth:width;
  176. height=this.winHeight ? this.winHeight:height;
  177. var dbwin = new Ext.window.Window({
  178. id: 'dbwin',
  179. title: '查找',
  180. height: height,
  181. width: width,
  182. maximizable: true,
  183. buttonAlign: 'center',
  184. layout: 'anchor',
  185. items: [{
  186. tag: 'iframe',
  187. frame: true,
  188. anchor: '100% 100%',
  189. layout: 'fit',
  190. 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>'
  191. }],
  192. buttons: [{
  193. text: '关 闭',
  194. iconCls: 'x-button-icon-close',
  195. cls: 'x-btn-gray',
  196. handler: function() {
  197. Ext.getCmp('dbwin').close();
  198. }
  199. },
  200. {
  201. text: '重置条件',
  202. id: 'reset',
  203. cls: 'x-btn-gray',
  204. hidden: true,
  205. handler: function() {
  206. var grid = Ext.getCmp('dbwin').el.dom.getElementsByTagName('iframe')[0].contentWindow.document.defaultView.Ext.getCmp('dbfindGridPanel');
  207. grid.resetCondition();
  208. grid.getCount();
  209. }
  210. }]
  211. });
  212. dbwin.show();
  213. trigger.lastTriggerId = null;
  214. },
  215. autoDbfind: function(which, caller, field, condition) {
  216. var me = this;
  217. Ext.Ajax.request({
  218. url: basePath + 'common/autoDbfind.action',
  219. params: {
  220. which: which,
  221. caller: caller,
  222. field: field,
  223. condition: condition
  224. },
  225. async: false,
  226. method: 'post',
  227. callback: function(options, success, response) {
  228. var res = new Ext.decode(response.responseText);
  229. if (res.exceptionInfo) {
  230. showError(res.exceptionInfo);
  231. return;
  232. }
  233. if (res.data) {
  234. var data = Ext.decode(res.data.replace(/,}/g, '}').replace(/,]/g, ']'));
  235. me.autoSetValue(data[0], res.dbfinds || me.owner.dbfinds);
  236. } else {
  237. me.onTriggerClick();
  238. }
  239. }
  240. });
  241. },
  242. autoSetValue: function(data, dbfinds) {
  243. var trigger = this;
  244. var triggerV = null;
  245. if (!trigger.ownerCt) { //如果是grid的dbfind
  246. var grid = trigger.owner;
  247. var record = grid.lastSelectedRecord || trigger.record || grid.getSelectionModel().selected.items[0] || grid.selModel.lastSelected; //detailgrid里面selected
  248. Ext.Array.each(Ext.Object.getKeys(data),
  249. function(k) {
  250. Ext.Array.each(dbfinds,
  251. function(ds) {
  252. if (ds.trigger == trigger.name || Ext.isEmpty(ds.trigger)) {
  253. if (Ext.Array.contains(ds.dbGridField.split(';'), k)) { //k == ds.dbGridField//支持多dbgrid的字段对应grid同一字段
  254. if (ds.field == trigger.name) {
  255. triggerV = data[k]; //trigger所在位置赋值
  256. }
  257. record.set(ds.field, data[k]);
  258. }
  259. }
  260. });
  261. });
  262. } else {
  263. var ff;
  264. Ext.Array.each(Ext.Object.getKeys(data),
  265. function(k) {
  266. Ext.Array.each(dbfinds,
  267. function(ds) {
  268. if (k == ds.dbGridField) {
  269. if (Ext.getCmp(ds.field)) {
  270. if (trigger.name == ds.field) {
  271. triggerV = data[k];
  272. } else {
  273. ff = Ext.getCmp(ds.field);
  274. if (ff && ff.setValue) ff.setValue(data[k]);
  275. }
  276. }
  277. }
  278. });
  279. });
  280. }
  281. trigger.setValue(triggerV);
  282. data.data = data;
  283. trigger.fireEvent('aftertrigger', trigger, data);
  284. trigger.lastTriggerId = null;
  285. }
  286. });