RelativeSearch.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. Ext.QuickTips.init();
  2. Ext.define('erp.controller.common.RelativeSearch', {
  3. extend: 'Ext.app.Controller',
  4. requires: ['erp.util.BaseUtil'],
  5. views:[
  6. 'common.RelativeSearch'
  7. ],
  8. init:function(){
  9. var me = this;
  10. me.BaseUtil = Ext.create('erp.util.BaseUtil');
  11. this.control({
  12. 'tabpanel': {
  13. afterrender: function(tb) {
  14. this.getRelativeSearch(tb, caller);
  15. },
  16. add: function(t, p) {
  17. p.on('activate', function(){
  18. if(!p.down('gridpanel') && p.datas) {
  19. var _f = p.datas.form, _g = p.datas.grid;
  20. p.add({
  21. xtype: 'grid',
  22. columnLines: true,
  23. layout: 'fit',
  24. autoScroll : true,
  25. anchor: '100% 85%',
  26. selModel: Ext.create('Ext.selection.CheckboxModel',{
  27. headerWidth: 0,
  28. views: []
  29. }),
  30. headerCt: Ext.create("Ext.grid.header.Container", {
  31. suspendLayout: true
  32. }),
  33. plugins: [Ext.create('Ext.ux.grid.GridHeaderFilters')],
  34. dockedItems: [new Ext.toolbar.Paging(me.pagingConfig)],
  35. tableName: _f.tablename,
  36. defaultCondition: _f.condition,
  37. fields: Ext.Array.concate(_g.gridFields, ',', 'name'),
  38. store: new Ext.data.Store({
  39. fields: _g.gridFields,
  40. data: []
  41. }),
  42. listeners: {
  43. scrollershow: function(scroller) {
  44. if (scroller && scroller.scrollEl) {
  45. scroller.clearManagedListeners();
  46. scroller.mon(scroller.scrollEl, 'scroll', scroller.onElScroll, scroller);
  47. }
  48. }
  49. },
  50. columns: new Array(),
  51. getGridData: function(page) {
  52. me.query(this.ownerCt.down('form'), this, page);
  53. }
  54. });
  55. var cm = _g.gridColumns, fn = me.link;
  56. Ext.each(cm, function(){
  57. if(this.logic) {
  58. this.renderer = fn;
  59. }
  60. });
  61. p.down('grid').reconfigure(new Ext.data.Store({
  62. fields: _g.gridFields,
  63. data: [{},{},{},{},{},{},{},{},{},{}]
  64. }), cm);
  65. } else {
  66. var g = p.down('gridpanel');
  67. g.getGridData(g.plugins[0].page || 1);
  68. }
  69. });
  70. }
  71. },
  72. 'field' : {
  73. afterrender : function(f) {
  74. var name = f.name, form = f.ownerCt,
  75. c = form.down('combo');
  76. if (!c) {
  77. var a = parent.Ext.getCmp(name);
  78. if (a) {
  79. f.setValue(a.getValue());
  80. }
  81. }
  82. if(f.logic) {
  83. var s = parent.Ext.getCmp(name);
  84. if(s) {
  85. f.setValue(s.getValue());
  86. }
  87. }
  88. }
  89. },
  90. 'combo': {
  91. afterrender: function(f) {
  92. var cm = parent.Ext.ComponentQuery.query('grid[relative=true]');
  93. var grid = cm.length > 0 ? cm[0] : parent.Ext.getCmp('grid');
  94. if(grid) {
  95. var d = [];
  96. grid.store.each(function(item){
  97. if(!Ext.isEmpty(item.get(f.name))) {
  98. d.push({
  99. display: item.get(f.name),
  100. value: item.get(f.name),
  101. data: item.data
  102. });
  103. }
  104. });
  105. f.store = Ext.create('Ext.data.Store', {
  106. fields: ['display','value','data'],
  107. data: d
  108. });
  109. //根据选中行直接筛选
  110. var lastselected=grid.getSelectionModel().selected.items[0];
  111. if(lastselected){
  112. f.setValue(lastselected.get(f.name));
  113. }else if(d.length > 0)
  114. f.setValue(d[0].value);
  115. }
  116. },
  117. change: function(f) {
  118. if(!Ext.isEmpty(f.value)) {
  119. var form = f.ownerCt;
  120. if (f.lastSelection[0])
  121. {
  122. var d = f.lastSelection[0].data.data;
  123. form.getForm().getFields().each(function(e){
  124. typeof d[e.name] !== 'undefined' && (e.setValue(d[e.name]));
  125. });
  126. }
  127. var grid = form.ownerCt.down('grid');
  128. if(grid && grid.rendered) {
  129. grid.plugins[0].clearFilters();
  130. this.query(form, grid, 1);
  131. }
  132. }
  133. }
  134. },
  135. 'gridpanel': {
  136. reconfigure: function(grid) {
  137. grid.reconfigured = true;
  138. this.query(grid.ownerCt.down('form'), grid, 1);
  139. },
  140. headerfiltersapply: function(grid, filters) {
  141. if(grid.reconfigured){
  142. var condition = null;
  143. for(var fn in filters){
  144. var value = filters[fn], f = grid.getHeaderFilterField(fn);
  145. if(!Ext.isEmpty(value)){
  146. if(f.filtertype) {
  147. if (f.filtertype == 'numberfield') {
  148. value = fn + "=" + value + " ";
  149. }
  150. } else {
  151. if(Ext.isDate(value)){
  152. value = Ext.Date.toString(value);
  153. value = fn + "=to_date('" + value + "','yyyy-MM-dd') ";
  154. } else {
  155. var exp_t = /^(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/,
  156. exp_d = /^(\d{4})\-(\d{2})\-(\d{2})$/;
  157. if(exp_d.test(value)){
  158. value = fn + "=to_date('" + value + "','yyyy-MM-dd') ";
  159. } else if(exp_t.test(value)){
  160. value = fn + "=to_date('" + value + "','yyyy-MM-dd HH24:mi:ss') ";
  161. } else{
  162. value = fn + " LIKE '%" + value + "%' ";
  163. }
  164. }
  165. }
  166. if(condition == null){
  167. condition = value;
  168. } else {
  169. condition = condition + " AND " + value;
  170. }
  171. }
  172. }
  173. grid.filterCondition = condition;
  174. grid.getGridData(1);
  175. }
  176. return false;
  177. }
  178. },
  179. 'button[name=export]': {
  180. click: function(btn) {
  181. var grid = btn.up('gridpanel');
  182. if (grid) {
  183. var tb = grid.ownerCt, form = tb.down('form'),
  184. cols = grid.fields, con = this.getQueryCondition(form, grid);
  185. this.BaseUtil.customExport(caller, grid, tb.title, '/common/form/relativeSearch.xls', con, {
  186. _id: form.rs_id,
  187. _tab: grid.tableName,
  188. _fies: cols
  189. });
  190. }
  191. }
  192. }
  193. });
  194. },
  195. getRelativeSearch: function(tb, cal) {
  196. var me = this;
  197. Ext.Ajax.request({
  198. url: basePath + 'common/form/relativeSearch.action',
  199. params: {
  200. caller: cal
  201. },
  202. callback: function(opt, s, r) {
  203. if (s) {
  204. var rs = Ext.decode(r.responseText);
  205. if(rs.data) {
  206. var s = null, _f = null;
  207. for(var i in rs.data) {
  208. s = rs.data[i];
  209. _f = s.form;
  210. _g = s.grid;
  211. tb.add({
  212. title: _f.title,
  213. datas: s,
  214. items: [{
  215. xtype: 'form',
  216. layout: 'column',
  217. items: _f.items,
  218. rs_id: _f.fo_id,
  219. cls: 'custom',
  220. bodyStyle: 'background: #f1f1f1;border: none;',
  221. fieldDefaults: {
  222. margin: '6 0 0 0',
  223. labelWidth: 70
  224. },
  225. anchor: '100% 15%',
  226. buttonAlign: 'center',
  227. buttons: [{
  228. text: $I18N.common.button.erpQueryButton,
  229. iconCls: 'x-button-icon-query',
  230. cls: 'x-btn-blue',
  231. handler: function(btn) {
  232. var f = btn.ownerCt.ownerCt, g = f.ownerCt.down('grid');
  233. me.query(f, g, 1);
  234. }
  235. },{
  236. text: $I18N.common.button.erpCloseButton,
  237. iconCls: 'x-button-icon-close',
  238. cls: 'x-btn-blue',
  239. handler: function() {
  240. var w = parent.Ext.getCmp('ext-relative-query');
  241. if (w) {
  242. w.hide();
  243. }
  244. }
  245. }]
  246. }]
  247. });
  248. }
  249. if(tb.items.items.length > 0) {
  250. var p = tb.items.items[0];
  251. p.fireEvent('activate', p);
  252. }
  253. }
  254. }
  255. }
  256. });
  257. },
  258. getQueryCondition: function(form, grid) {
  259. var con = grid.defaultCondition, fileter = grid.filterCondition;
  260. if(!Ext.isEmpty(con)) {
  261. con = "(" + con + ")";
  262. }
  263. if(!Ext.isEmpty(fileter)) {
  264. if(!Ext.isEmpty(con)) {
  265. con += " AND (" + fileter + ")";
  266. } else {
  267. con = fileter;
  268. }
  269. }
  270. form.getForm().getFields().each(function(f){
  271. if(f.logic) {
  272. if(f.logic.indexOf('to:') > -1) {
  273. var _field = f.logic.split('to:')[1];
  274. if(!Ext.isEmpty(con)) {
  275. if(f.xtype == 'datefield') {
  276. con += " AND (to_char(" + _field + ",'yyyymmdd')='" +
  277. Ext.Date.format(f.value, 'Ymd') + "')";
  278. } else {
  279. con += " AND (" + _field + "='" + f.value + "')";
  280. }
  281. } else {
  282. if(f.xtype == 'datefield') {
  283. con = " to_char(" + _field + ",'yyyymmdd')='" +
  284. Ext.Date.format(f.value, 'Ymd') + "'";
  285. } else {
  286. con = _field + "='" + f.value + "'";
  287. }
  288. }
  289. }
  290. }
  291. });
  292. return con;
  293. },
  294. pageSize: 12,
  295. query: function(form, grid, page) {
  296. var cols = grid.fields,
  297. pageSize = this.pageSize,
  298. start = (page - 1) * pageSize + 1,
  299. end = page * pageSize, con = this.getQueryCondition(form, grid);
  300. grid.setLoading(true);
  301. Ext.Ajax.request({
  302. url: basePath + 'common/form/search.action',
  303. params: {
  304. _id: form.rs_id,
  305. _tab: grid.tableName,
  306. _fies: cols,
  307. _start: start,
  308. _end: end,
  309. _cond: con
  310. },
  311. callback: function(opt, s, r) {
  312. grid.setLoading(false);
  313. var rs = Ext.decode(r.responseText);
  314. if(rs.data) {
  315. grid.store.loadData(rs.data);
  316. grid.down('pagingtoolbar').page = page;
  317. grid.down('pagingtoolbar').dataCount = rs.count;
  318. grid.down('pagingtoolbar').onLoad();
  319. }
  320. }
  321. });
  322. },
  323. pagingConfig: {
  324. dock: 'bottom',
  325. displayInfo: true,
  326. pageSize: 12,
  327. items: ['-',{
  328. name: 'export',
  329. tooltip: $I18N.common.button.erpExportButton,
  330. iconCls: 'x-button-icon-excel',
  331. cls: 'x-btn-tb',
  332. width: 24,
  333. handler: function(){
  334. }
  335. }],
  336. updateInfo : function(){
  337. var page = this.child('#inputItem').getValue() || 1;
  338. var me = this,
  339. pageSize = 12,
  340. dataCount = me.dataCount || 0;
  341. var displayItem = me.child('#displayItem'),
  342. pageData = me.getPageData();
  343. pageData.fromRecord = (page-1)*pageSize+1;
  344. pageData.toRecord = page*pageSize > dataCount ? dataCount : page*pageSize;
  345. pageData.total = dataCount;
  346. var msg;
  347. if (displayItem) {
  348. if (me.dataCount === 0) {
  349. msg = me.emptyMsg;
  350. } else {
  351. msg = Ext.String.format(
  352. me.displayMsg,
  353. pageData.fromRecord,
  354. pageData.toRecord,
  355. pageData.total
  356. );
  357. }
  358. displayItem.setText(msg);
  359. me.doComponentLayout();
  360. }
  361. },
  362. getPageData : function(){
  363. var me = this,
  364. totalCount = me.dataCount;
  365. return {
  366. total : totalCount,
  367. currentPage : me.page,
  368. pageCount: Math.ceil(me.dataCount / me.pageSize),
  369. fromRecord: ((me.page - 1) * me.pageSize) + 1,
  370. toRecord: Math.min(me.page * me.pageSize, totalCount)
  371. };
  372. },
  373. doRefresh:function(){
  374. this.moveFirst();
  375. },
  376. onPagingKeyDown : function(field, e){
  377. var me = this,
  378. k = e.getKey(),
  379. pageData = me.getPageData(),
  380. increment = e.shiftKey ? 10 : 1,
  381. pageNum = 0;
  382. if (k == e.RETURN) {
  383. e.stopEvent();
  384. pageNum = me.readPageFromInput(pageData);
  385. if (pageNum !== false) {
  386. pageNum = Math.min(Math.max(1, pageNum), pageData.pageCount);
  387. me.child('#inputItem').setValue(pageNum);
  388. if(me.fireEvent('beforechange', me, pageNum) !== false){
  389. me.page = pageNum;
  390. me.ownerCt.getGridData(me.page);
  391. }
  392. }
  393. } else if (k == e.HOME || k == e.END) {
  394. e.stopEvent();
  395. pageNum = k == e.HOME ? 1 : pageData.pageCount;
  396. field.setValue(pageNum);
  397. } else if (k == e.UP || k == e.PAGEUP || k == e.DOWN || k == e.PAGEDOWN) {
  398. e.stopEvent();
  399. pageNum = me.readPageFromInput(pageData);
  400. if (pageNum) {
  401. if (k == e.DOWN || k == e.PAGEDOWN) {
  402. increment *= -1;
  403. }
  404. pageNum += increment;
  405. if (pageNum >= 1 && pageNum <= pageData.pages) {
  406. field.setValue(pageNum);
  407. }
  408. }
  409. }
  410. me.updateInfo();
  411. me.resetTool(pageNum);
  412. },
  413. moveFirst : function(){
  414. var me = this;
  415. me.child('#inputItem').setValue(1);
  416. value = 1;
  417. me.page = value;
  418. me.ownerCt.getGridData(value);
  419. me.updateInfo();
  420. me.resetTool(value);
  421. },
  422. movePrevious : function(){
  423. var me = this;
  424. me.child('#inputItem').setValue(me.child('#inputItem').getValue() - 1);
  425. value = me.child('#inputItem').getValue();
  426. me.page = value;
  427. me.ownerCt.getGridData(value);
  428. me.updateInfo();
  429. me.resetTool(value);
  430. },
  431. moveNext : function(){
  432. var me = this,
  433. last = me.getPageData().pageCount;
  434. total = last;
  435. me.child('#inputItem').setValue(me.child('#inputItem').getValue() + 1);
  436. value = me.child('#inputItem').getValue();
  437. me.page = value;
  438. me.ownerCt.getGridData(value);
  439. me.updateInfo();
  440. me.resetTool(value);
  441. },
  442. moveLast : function(){
  443. var me = this,
  444. last = me.getPageData().pageCount;
  445. total = last;
  446. me.child('#inputItem').setValue(last);
  447. value = me.child('#inputItem').getValue();
  448. me.page = value;
  449. me.ownerCt.getGridData(value);
  450. me.updateInfo();
  451. me.resetTool(value);
  452. },
  453. onLoad : function() {
  454. var e = this, d, b, c, a;
  455. if (!e.rendered) {
  456. return
  457. }
  458. d = e.getPageData();
  459. b = d.currentPage || 1;
  460. c = Math.ceil(e.dataCount / e.pageSize);
  461. a = Ext.String.format(e.afterPageText, isNaN(c) ? 1 : c);
  462. e.child("#afterTextItem").setText(a);
  463. e.child("#inputItem").setValue(b);
  464. e.child("#first").setDisabled(b === 1);
  465. e.child("#prev").setDisabled(b === 1);
  466. e.child("#next").setDisabled(b === c || c===1);//
  467. e.child("#last").setDisabled(b === c || c===1);
  468. e.child("#refresh").enable();
  469. e.updateInfo();
  470. e.fireEvent("change", e, d);
  471. },
  472. resetTool: function(value){
  473. var pageCount = this.getPageData().pageCount;
  474. this.child('#last').setDisabled(value == pageCount || pageCount == 1);
  475. this.child('#next').setDisabled(value == pageCount || pageCount == 1);
  476. this.child('#first').setDisabled(value <= 1);
  477. this.child('#prev').setDisabled(value <= 1);
  478. }
  479. },
  480. link: function(val, m, record, x, y, store, view) {
  481. var grid = view.ownerCt, column = grid.columns[y], url = column.logic;
  482. if(url) {
  483. var index = 0, length = url.length, s, e;
  484. while(index < length) {
  485. if((s = url.indexOf('{', index)) != -1 && (e = url.indexOf('}', s + 1)) != -1) {
  486. url = url.substring(0, s) + record.get(url.substring(s+1, e)) + url.substring(e+1);
  487. index = e + 1;
  488. } else {
  489. break;
  490. }
  491. }
  492. return '<a href="javascript:openUrl(\'' + url + '\');">' + val + '</a>';
  493. }
  494. return val;
  495. }
  496. });