DbfindTrigger.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. Ext.define('uas.view.trigger.DbfindTrigger', {
  2. extend: 'Ext.form.ComboBox',
  3. xtype: 'dbfindtrigger',
  4. triggerCls: Ext.baseCSSPrefix + 'form-search-trigger',
  5. queryMode: 'local',
  6. displayField: 'dispaly',
  7. valueField: 'value',
  8. minChars: 1, // 设置用户输入字符多少时触发查询
  9. tpl: '',
  10. enableKeyEvents: true,
  11. initComponent: function () {
  12. var me = this;
  13. Ext.apply(me, me.applyConfig());
  14. me.callParent();
  15. },
  16. applyConfig: function() {
  17. var me = this,
  18. dbtpls = me.dbtpls || [],
  19. fields = [],
  20. minWidth = 0,
  21. cols = '';
  22. for(let x = 0; x < dbtpls.length; x++) {
  23. let dbtpl = dbtpls[x],
  24. width = dbtpl.width || 100,
  25. field = dbtpl.field;
  26. fields.push(field);
  27. minWidth += width;
  28. if(dbtpls.length==(x+1)){
  29. cols += '<span style="padding:0 10px 0 10px;width:' + width + 'px;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">{' + field + '}</span>';
  30. }else{
  31. cols += '<span style="padding:0 10px 0 10px;width:' + width + 'px;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;float:left;">{' + field + '}</span>';
  32. }
  33. }
  34. minWidth += 15;
  35. cfg = {
  36. store: Ext.create('Ext.data.Store', {
  37. fields: fields,
  38. data: []
  39. }),
  40. listConfig: {
  41. minWidth: minWidth,
  42. width: minWidth,
  43. maxHeight: 210,
  44. autoScroll: true
  45. },
  46. tpl: Ext.create('Ext.XTemplate',
  47. '<ul style="padding-left: 0px;"><tpl for=".">',
  48. '<li role="option" class="x-boundlist-item" style="list-style:none; padding: 0;">',
  49. '<div style="height:30px;">',
  50. '' + cols + '',
  51. '</li>',
  52. '</tpl></ul>'
  53. )
  54. };
  55. return cfg;
  56. },
  57. //输入值之后进行模糊查询
  58. doQuery: function (queryString, forceAll, rawQuery) {
  59. if (!this.fireEvent('beforequery', this)) {
  60. return;
  61. };
  62. queryString = queryString || '';
  63. var me = this;
  64. if (me.lastQueryValue != queryString) {
  65. me.judge(me);
  66. me.lastQueryValue = queryString;
  67. if (queryString.trim() == '') {
  68. me.collapse();
  69. } else {
  70. //加载数据
  71. var data, dbfinds = me.dbfinds, dbCondition = [];
  72. if (dbfinds) {
  73. var dbtplcondition = "";
  74. for (let index = 0; index < dbfinds.length; index++) {
  75. var item = dbfinds[index].from;
  76. if (!dbfinds[index].ignore) {
  77. dbtplcondition += "upper(" + item + ") like '%" + queryString.toUpperCase() + "%' or ";
  78. }
  79. }
  80. dbtplcondition = "(" + dbtplcondition.substring(0, dbtplcondition.length - 4) + ")";
  81. if (dbtplcondition.length > 0) {
  82. dbCondition = [{
  83. type: 'condition',
  84. value: dbtplcondition
  85. }];
  86. }
  87. }
  88. //添加默认条件
  89. if (me.defaultCondition) {
  90. dbCondition.push({
  91. type: 'condition',
  92. value: me.defaultCondition
  93. });
  94. }
  95. Ext.Ajax.request({
  96. url: me.dataUrl,
  97. params: {
  98. number: 1,
  99. size: 10,
  100. condition: JSON.stringify(dbCondition),
  101. page: 1,
  102. start: 0,
  103. limit: 10
  104. },
  105. method: 'GET',
  106. success: function (response, opts) {
  107. data = Ext.decode(response.responseText);
  108. data = data.data ? data.data.list : [];
  109. if (data != null && data.length > 0 && me.store) {
  110. me.store.loadData(data, false);
  111. me.expand();
  112. } else {
  113. me.store.removeAll();
  114. me.collapse();
  115. }
  116. },
  117. failure: function (response, opts) {}
  118. });
  119. }
  120. return true;
  121. } else {
  122. return false;
  123. }
  124. },
  125. onTriggerClick: function (f) {
  126. var me = this;
  127. if (!this.fireEvent('beforetriggerclick', this)) {
  128. return;
  129. };
  130. f.blur(f,null,null,true);
  131. //判断dbfindtrigger归属
  132. f.judge(f); //form
  133. var panel = f.up('content-panel') || Ext.getCmp('content-panel'),
  134. panelEl;
  135. if (!f.column && f.ownerCt.ownerCt.id.indexOf('window-') > -1 && f.ownerCt.ownerCt.id.indexOf('document-') <0) {
  136. panelEl = f.ownerCt.ownerCt.getEl()
  137. } else {
  138. panelEl = panel.getEl()
  139. }
  140. var box = panelEl.getBox();
  141. var height = box.height;
  142. var width = box.width;
  143. var dbItem = {
  144. xtype: 'dbfindtriggerpanel',
  145. columns: f.dbColumns,
  146. dataUrl: f.dataUrl,
  147. searchUrl: f.searchUrl,
  148. dbfinds: f.dbfinds,
  149. belong: f.belong,
  150. dbSearchFields: f.dbSearchFields ? f.dbSearchFields : [],
  151. dbfindtrigger: f,
  152. };
  153. if (me.onAddClick) {
  154. dbItem.onAddClick = me.onAddClick;
  155. }
  156. var win = panel.add(Ext.create('Ext.window.Window', {
  157. cls: 'x-window-dbfind',
  158. trigger: f,
  159. belong: f.ownerCt,
  160. modal: true,
  161. height: height * 0.9,
  162. width: width * 0.9,
  163. title: '查找' + f.addTitle,
  164. scrollable: true,
  165. bodyPadding: 10,
  166. constrain: true,
  167. closable: true,
  168. layout: 'fit',
  169. renderTo: panel.getEl(),
  170. items: [dbItem]
  171. }));
  172. win.show();
  173. },
  174. judge: function (f) {
  175. if (f.ownerCt.xtype.trim().toUpperCase().indexOf('QUERYFORMPANEL') > -1 ||
  176. (f.ownerCt.ownerCt && (f.ownerCt.ownerCt.xtype.trim().toUpperCase().indexOf('BASEPANEL') > -1 ||
  177. f.ownerCt.ownerCt.xtype.trim().toUpperCase().indexOf('EDITDATALIST') > -1))) {
  178. f.belong = 'form';
  179. return f.ownerCt.ownerCt
  180. } else if (f.ownerCt.xtype.trim().toUpperCase().indexOf('FORM') > -1) {
  181. f.belong = 'form';
  182. return f.ownerCt
  183. } else if (f.column) {
  184. f.belong = 'grid';
  185. return f.column.ownerCt.ownerCt.ownerCt
  186. }
  187. },
  188. listeners: {
  189. blur: function (f, e, op, isTrigger) {
  190. return true;//先不使用
  191. var me = f;
  192. var dbfinds = me.dbfinds,
  193. data;
  194. if (f.value && f.value != '') {
  195. //添加默认条件
  196. var searchField = null;
  197. var dbCondition = [];
  198. if (me.defaultCondition) {
  199. dbCondition.push({
  200. type: 'condition',
  201. value: me.defaultCondition
  202. });
  203. }
  204. for (let index = 0; index < dbfinds.length; index++) {
  205. var item = dbfinds[index].to;
  206. if (item == me.name) {
  207. searchField = dbfinds[index].from;
  208. }
  209. }
  210. dbCondition.push({
  211. type: 'condition',
  212. value: searchField + "='" + me.value + "'"
  213. });
  214. Ext.Ajax.request({
  215. url: me.dataUrl,
  216. async: false,
  217. params: {
  218. number: 1,
  219. size: 1,
  220. condition: JSON.stringify(dbCondition),
  221. page: 1,
  222. start: 0,
  223. limit: 10
  224. },
  225. method: 'GET',
  226. success: function (response, opts) {
  227. data = Ext.decode(response.responseText);
  228. data = data.data ? data.data.list : [];
  229. },
  230. failure: function (response, opts) {}
  231. });
  232. }
  233. if (!f.value || f.value == '' || data.length > 1 || data.length == 0) {
  234. me.dbValues = {};
  235. if (dbfinds && dbfinds.length > 0) {
  236. if (me.belong == 'grid') {
  237. for (let index = 0; index < dbfinds.length; index++) {
  238. var item = dbfinds[index];
  239. var rec = me.column.ownerCt.ownerCt.selModel.getLastSelected();
  240. var nowRec = me.column.ownerCt.ownerCt.store.getData().getByKey(rec.id);
  241. if (nowRec.get(item.to) && nowRec.get(item.to) != "") {
  242. nowRec.set(item.to, null);
  243. delete nowRec.modified[item.to];
  244. }
  245. if (me.name == item.to) {
  246. me.column.getEditor().setValue('');
  247. }
  248. }
  249. } else if (me.belong == 'form') {
  250. for (let index = 0; index < dbfinds.length; index++) {
  251. var item = dbfinds[index];
  252. var field = me.ownerCt.down('[name=' + item.to + ']');
  253. if (field) {
  254. let xtypes = field.getXTypes().split('/');
  255. if (xtypes.indexOf('dbfindtrigger') != -1) {
  256. field.setValue(null);
  257. field.lastTriggerValue = null;
  258. } else {
  259. field.setValue(null);
  260. field.publishState('value', null);
  261. }
  262. }
  263. }
  264. }else {
  265. me.setValue(null);
  266. }
  267. }
  268. if(!isTrigger){
  269. let rec = {
  270. data:{},
  271. get:function(name){
  272. return this.data[name]
  273. }
  274. }
  275. f.aftertrigger(f,rec);
  276. }
  277. } else if (data.length == 1) {
  278. me.dbValues = data[0];
  279. var dbfinds = me.dbfinds;
  280. if (dbfinds && dbfinds.length > 0) {
  281. if (me.belong == 'grid') {
  282. for (let index = 0; index < dbfinds.length; index++) {
  283. var item = dbfinds[index];
  284. var rec = me.column.ownerCt.ownerCt.selModel.getLastSelected();
  285. var nowRec = me.column.ownerCt.ownerCt.store.getData().getByKey(rec.id);
  286. nowRec.set(item.to, data[0][item.from]);
  287. if (me.name == item.to) {
  288. me.column.getEditor().setValue(data[0][item.from]);
  289. }
  290. }
  291. } else if (me.belong == 'form') {
  292. for (let index = 0; index < dbfinds.length; index++) {
  293. var item = dbfinds[index];
  294. var field = me.ownerCt.down('[name=' + item.to + ']');
  295. if (field) {
  296. var val = data[0][item.from];
  297. if (field.xtype == 'dbfindtrigger') {
  298. field.setRawValue(val);
  299. field.setValue(val);
  300. field.value = val;
  301. field.lastTriggerValue = val;
  302. } else {
  303. field.setValue(val);
  304. }
  305. }
  306. }
  307. }
  308. }
  309. if(!isTrigger){
  310. let rec = {
  311. data:data[0],
  312. get:function(name){
  313. return this.data[name]
  314. }
  315. }
  316. f.aftertrigger(f,rec);
  317. }
  318. }
  319. },
  320. select: function (combo, record, eOpts) {
  321. var me = combo;
  322. var dbfinds = me.dbfinds;
  323. me.dbValues = record.data;
  324. if (dbfinds && dbfinds.length > 0) {
  325. if (me.belong == 'grid') {
  326. for (let index = 0; index < dbfinds.length; index++) {
  327. var item = dbfinds[index];
  328. var rec = me.column.ownerCt.ownerCt.selModel.getLastSelected();
  329. var nowRec = me.column.ownerCt.ownerCt.store.getData().getByKey(rec.id);
  330. nowRec.set(item.to, record.get(item.from));
  331. if (me.name == item.to) {
  332. me.column.getEditor().setValue(record.get(item.from));
  333. }
  334. }
  335. } else if (me.belong == 'form') {
  336. for (let index = 0; index < dbfinds.length; index++) {
  337. var item = dbfinds[index];
  338. var field = me.ownerCt.down('[name=' + item.to + ']');
  339. if (field) {
  340. var val = record.get(item.from);
  341. if (field.xtype == 'dbfindtrigger') {
  342. field.setRawValue(val);
  343. field.setValue(val);
  344. field.value = val;
  345. field.lastTriggerValue = val;
  346. } else {
  347. field.setValue(val);
  348. }
  349. }
  350. }
  351. }else {
  352. var dbfind = Ext.Array.findBy(dbfinds, function(d) {
  353. return d.to = me.name;
  354. });
  355. if(dbfind) {
  356. me.setValue(record.get(dbfind.from));
  357. }
  358. }
  359. me.aftertrigger(me, record);
  360. }
  361. }
  362. },
  363. aftertrigger: function (f) {
  364. return true;
  365. },
  366. setValue: function (v) {
  367. this.callParent(arguments);
  368. this.publishState('value', v);
  369. },
  370. getValue: function (f) {
  371. var me = this,
  372. val = me.rawToValue(me.processRawValue(me.getRawValue()));
  373. me.value = val;
  374. return val;
  375. },
  376. autoSetValue: function (combo, record) {
  377. var me = combo;
  378. var dbfinds = me.dbfinds;
  379. if (dbfinds && dbfinds.length > 0) {
  380. if (me.belong == 'grid') {
  381. for (let index = 0; index < dbfinds.length; index++) {
  382. var item = dbfinds[index];
  383. var rec = me.column.ownerCt.ownerCt.selModel.getLastSelected();
  384. var nowRec = me.column.ownerCt.ownerCt.store.getData().getByKey(rec.id);
  385. nowRec.set(item.to, record.get(item.from));
  386. if (me.name == item.to) {
  387. me.column.getEditor().setValue(record.get(item.from));
  388. }
  389. }
  390. } else if (me.belong == 'form') {
  391. for (let index = 0; index < dbfinds.length; index++) {
  392. var item = dbfinds[index];
  393. var field = me.ownerCt.down('[name=' + item.to + ']');
  394. if (field) {
  395. var val = record.get(item.from);
  396. if (field.xtype == 'dbfindtrigger') {
  397. field.setRawValue(val);
  398. field.setValue(val);
  399. field.value = val;
  400. field.lastTriggerValue = val;
  401. } else {
  402. field.setValue(val);
  403. }
  404. }
  405. }
  406. }
  407. me.aftertrigger(me, record);
  408. }
  409. }
  410. });