CraftGridPanel.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. Ext.define('erp.view.pm.mes.CraftGridPanel',{
  2. extend: 'Ext.grid.Panel',
  3. alias: 'widget.erpGridPanel2',
  4. requires: ['erp.view.core.toolbar.Toolbar', 'erp.view.core.plugin.CopyPasteMenu'],
  5. region: 'south',
  6. layout : 'fit',
  7. id: 'grid',
  8. emptyText : $I18N.common.grid.emptyText,
  9. columnLines : true,
  10. autoScroll : true,
  11. store: [],
  12. columns: [],
  13. binds:null,
  14. bodyStyle: 'background-color:#f1f1f1;',
  15. plugins: [Ext.create('Ext.grid.plugin.CellEditing', {
  16. clicksToEdit: 1,
  17. listeners:{
  18. beforeedit:function(e){
  19. var g=e.grid,r=e.record,f=e.field;
  20. if(g.binds){
  21. var bool=true;
  22. Ext.Array.each(g.binds,function(item){
  23. if(Ext.Array.contains(item.fields,f)){
  24. Ext.each(item.refFields,function(field){
  25. if(r.get(field)!=null && r.get(field)!=0 && r.get(field)!='' && r.get(field)!='0'){
  26. bool=false;
  27. }
  28. });
  29. }
  30. });
  31. return bool;
  32. }
  33. }
  34. }
  35. }), Ext.create('erp.view.core.plugin.CopyPasteMenu')],
  36. features : [Ext.create('Ext.grid.feature.GroupingSummary',{
  37. startCollapsed: true,
  38. groupHeaderTpl: '{name} (共:{rows.length}条)'
  39. }),{
  40. ftype : 'summary',
  41. showSummaryRow : false,//不显示默认合计行
  42. generateSummaryData: function(){
  43. // 避开在grid reconfigure后的计算,节约加载时间50~600ms
  44. return {};
  45. }
  46. }],
  47. GridUtil: Ext.create('erp.util.GridUtil'),
  48. BaseUtil: Ext.create('erp.util.BaseUtil'),
  49. necessaryField: '',//必填字段
  50. detno: '',//编号字段
  51. keyField: '',//主键字段
  52. mainField: '',//对应主表主键的字段
  53. dbfinds: [],
  54. caller: null,
  55. condition: null,
  56. gridCondition:null,
  57. initComponent : function(){
  58. if(!this.boxready) {
  59. if(this._buttons)
  60. this.bbar._buttons = this._buttons;// 在toolbar加入grid固定按钮
  61. var condition = this.condition;
  62. if(!condition){
  63. var urlCondition = this.BaseUtil.getUrlParam('gridCondition');
  64. urlCondition = urlCondition == null || urlCondition == "null" ? "" : urlCondition;
  65. gridCondition = (gridCondition == null || gridCondition == "null") ? "" : gridCondition;
  66. gridCondition = gridCondition + urlCondition;
  67. gridCondition = gridCondition.replace(/IS/g, "=");
  68. condition = gridCondition;
  69. }
  70. var gridParam = {caller: this.caller || caller, condition: this.gridCondition||condition, _m: 0};
  71. var master = getUrlParam('newMaster');
  72. if(master){
  73. gridParam.master = master;
  74. }
  75. this.getGridColumnsAndStore(this, 'common/singleGridPanel.action', gridParam, "");//从后台拿到gridpanel的配置及数据
  76. }
  77. this.callParent(arguments);
  78. if(!this.boxready) {
  79. if(this.allowExtraButtons)// 加载其它按钮
  80. this.on('reconfigure', this.loadExtraButton, this, {single: true, delay: 1000});
  81. this.on('summary', this.generateSummaryData, this, {single: true, delay: 1000});
  82. }
  83. },
  84. getEffectiveData: function(){
  85. var me = this;
  86. var effective = new Array();
  87. var s = this.store.data.items;
  88. for(var i=0;i<s.length;i++){
  89. var data = s[i].data;
  90. if(data[me.keyField] != null && data[me.keyField] != ""){
  91. effective.push(data);
  92. }
  93. }
  94. return effective;
  95. },
  96. setReadOnly: function(bool){
  97. this.readOnly = bool;
  98. },
  99. reconfigure: function(store, columns){
  100. var me = this,
  101. view = me.getView(),
  102. originalDeferinitialRefresh,
  103. oldStore = me.store,
  104. headerCt = me.headerCt,
  105. oldColumns = headerCt ? headerCt.items.getRange() : me.columns;
  106. if (columns) {
  107. columns = Ext.Array.slice(columns);
  108. }
  109. me.fireEvent('beforereconfigure', me, store, columns, oldStore, oldColumns);
  110. if (me.lockable) {
  111. me.reconfigureLockable(store, columns);
  112. } else {
  113. Ext.suspendLayouts();
  114. if (columns) {
  115. delete me.scrollLeftPos;
  116. headerCt.removeAll();
  117. headerCt.add(columns);
  118. }
  119. if (store && (store = Ext.StoreManager.lookup(store)) !== oldStore) {
  120. originalDeferinitialRefresh = view.deferInitialRefresh;
  121. view.deferInitialRefresh = false;
  122. try {
  123. me.bindStore(store);
  124. } catch ( e ) {
  125. }
  126. view.deferInitialRefresh = originalDeferinitialRefresh;
  127. } else {
  128. me.getView().refresh();
  129. }
  130. Ext.resumeLayouts(true);
  131. }
  132. me.fireEvent('reconfigure', me, store, columns, oldStore, oldColumns);
  133. this.fireEvent("summary", this);
  134. },
  135. generateSummaryData : function() {
  136. var store = this.store,
  137. columns = this.columns, s = this.features[this.features.length - 1],
  138. i = 0, length = columns.length, comp, bar = this.down('erpToolbar');
  139. if (!bar) return;
  140. //将feature的data打印在toolbar上面
  141. for (; i < length; i++ ) {
  142. comp = columns[i];
  143. if(comp.summaryType) {
  144. var tb = Ext.getCmp(comp.dataIndex + '_' + comp.summaryType);
  145. if(!tb){
  146. bar.add('-');
  147. tb = bar.add({
  148. id: comp.dataIndex + '_' + comp.summaryType,
  149. itemId: comp.dataIndex,
  150. xtype: 'tbtext'
  151. });
  152. }
  153. var val = s.getSummary(store, comp.summaryType, comp.dataIndex, false);
  154. if(comp.xtype == 'numbercolumn') {
  155. val = Ext.util.Format.number(val, (comp.format || '0,000.000'));
  156. }
  157. tb.setText(comp.text + ':' + val);
  158. }
  159. }
  160. },
  161. /**
  162. * Grid上一条
  163. */
  164. prev: function(grid, record){
  165. grid = grid || Ext.getCmp('grid');
  166. record = record || grid.selModel.lastSelected;
  167. if(record){
  168. //递归查找上一条,并取到数据
  169. var d = grid.store.getAt(record.index - 1);
  170. if(d){
  171. try {
  172. grid.selModel.select(d);
  173. return d;
  174. } catch (e){
  175. }
  176. } else {
  177. if(record.index - 1 > 0){
  178. return this.prev(grid, d);
  179. } else {
  180. return null;
  181. }
  182. }
  183. }
  184. },
  185. /**
  186. * Grid下一条
  187. */
  188. next: function(grid, record){
  189. grid = grid || Ext.getCmp('grid');
  190. record = record || grid.selModel.lastSelected;
  191. if(record){
  192. //递归查找下一条,并取到数据
  193. var d = grid.store.getAt(record.index + 1);
  194. if(d){
  195. try {
  196. grid.selModel.select(d);
  197. return d;
  198. } catch (e){
  199. }
  200. } else {
  201. if(record.index + 1 < grid.store.data.items.length){
  202. return this.next(grid, d);
  203. } else {
  204. return null;
  205. }
  206. }
  207. }
  208. },
  209. allowExtraButtons: false,// 加载其它按钮,从GridButton加载
  210. loadExtraButton: function() {
  211. var me = this;
  212. Ext.Ajax.request({
  213. url : basePath + "common/gridButton.action",
  214. params: {
  215. caller: caller
  216. },
  217. method : 'post',
  218. async: false,
  219. callback : function(options, success, response){
  220. var r = new Ext.decode(response.responseText);
  221. if(r.exceptionInfo){
  222. showError(r.exceptionInfo);
  223. }
  224. if(r.buttons){
  225. var buttons = Ext.decode(r.buttons), tb = me.down('#toolbar');
  226. if(tb) {
  227. Ext.each(buttons, function(b){
  228. try {
  229. tb.add({
  230. xtype: b.xtype,
  231. disabled: true,
  232. style: {
  233. marginLeft: '0'
  234. }
  235. });
  236. } catch(e) {
  237. tb.add({
  238. text: $I18N.common.button[b.xtype],
  239. id: b.xtype,
  240. cls: 'x-btn-gray',
  241. disabled: true
  242. });
  243. }
  244. });
  245. }
  246. }
  247. }
  248. });
  249. },
  250. getGridColumnsAndStore: function(grid, url, param, no){
  251. var me = this;
  252. grid.setLoading(true);
  253. Ext.Ajax.request({//拿到grid的columns
  254. url : basePath + url,
  255. params: param,
  256. async: (grid.sync ? false : true),
  257. method : 'post',
  258. callback : function(options,success,response){
  259. grid.setLoading(false);
  260. if (!response) return;
  261. var res = new Ext.decode(response.responseText);
  262. if(res.exceptionInfo){
  263. showError(res.exceptionInfo);return;
  264. }
  265. if(res.columns){
  266. var limits = res.limits, limitArr = new Array();
  267. if(limits != null && limits.length > 0) {//权限外字段
  268. limitArr = Ext.Array.pluck(limits, 'lf_field');
  269. }
  270. Ext.each(res.columns, function(column, y){
  271. // column有取别名
  272. if(column.dataIndex.indexOf(' ') > -1) {
  273. column.dataIndex = column.dataIndex.split(' ')[1];
  274. }
  275. //power
  276. if(limitArr.length > 0 && Ext.Array.contains(limitArr, column.dataIndex)) {
  277. column.hidden = true;
  278. }
  279. //renderer
  280. me.GridUtil.setRenderer(grid, column);
  281. //logictype
  282. me.GridUtil.setLogicType(grid, column, {
  283. headerColor: res.necessaryFieldColor
  284. });
  285. });
  286. //data
  287. var data = [];
  288. if(!res.data || res.data.length == 2){
  289. if (grid.buffered) {
  290. me.GridUtil.add10EmptyData(grid.detno, data);
  291. me.GridUtil.add10EmptyData(grid.detno, data);//添加20条空白数据
  292. } else {
  293. grid.on('reconfigure', function(){// 改为Grid加载后再添加空行,节约200~700ms
  294. me.GridUtil.add10EmptyItems(grid, 40, false);
  295. });
  296. }
  297. } else {
  298. data = Ext.decode(res.data.replace(/,}/g, '}').replace(/,]/g, ']'));
  299. data = me.parseData(data);
  300. }
  301. //store
  302. var store = me.GridUtil.setStore(grid, res.fields, data, grid.groupField, grid.necessaryField);
  303. //view
  304. if(grid.selModel && grid.selModel.views == null){
  305. grid.selModel.views = [];
  306. }
  307. //dbfind
  308. if(res.dbfinds && res.dbfinds.length > 0){
  309. grid.dbfinds = res.dbfinds;
  310. }
  311. //reconfigure
  312. if(grid.sync) {//同步加载的Grid
  313. grid.reconfigure(store, res.columns);
  314. grid.on('afterrender', function(){
  315. me.GridUtil.setToolbar(grid, grid.columns, grid.necessaryField, limitArr);
  316. });
  317. } else {
  318. //toolbar
  319. if (grid.generateSummaryData === undefined) {// 改为Grid加载后再添加合计,节约60ms
  320. me.GridUtil.setToolbar(grid, res.columns, grid.necessaryField, limitArr);
  321. }
  322. grid.reconfigure(store, res.columns);
  323. }
  324. if(grid.buffered) {//缓冲数据的Grid
  325. grid.verticalScroller = Ext.create('Ext.grid.PagingScroller', {
  326. activePrefetch: false,
  327. store: store
  328. });
  329. store.guaranteeRange(0, Math.min(store.pageSize, store.prefetchData.length) - 1);
  330. }
  331. var form = Ext.ComponentQuery.query('form');
  332. if(form && form.length > 0){
  333. grid.readOnly = form[0].readOnly;//grid不可编辑
  334. }
  335. } else {
  336. grid.hide();
  337. var form = Ext.ComponentQuery.query('form')[0];
  338. if(form && !form.isStatic) {
  339. if(form.items.items.length == 0) {
  340. form.on('afterload', function(){
  341. me.GridUtil.updateFormPosition(form);//字段较少时,修改form布局
  342. });
  343. } else {
  344. me.GridUtil.updateFormPosition(form);//字段较少时,修改form布局
  345. }
  346. }
  347. }
  348. }
  349. });
  350. },
  351. parseData:function(data){
  352. var arr = new Array();
  353. if (caller === 'Craft') {
  354. Ext.each(data,function(item){
  355. var o = new Object();
  356. for(var key in item){
  357. if(key == 'cd_updatedate' || key == 'cd_id' || key == 'cd_crid' || key == 'cd_stepno' ||
  358. key == 'cd_detno' || key == 'cd_stepcode' || key == 'cd_stepname'||key == 'cd_table'){
  359. o[key] = item[key];
  360. }else{
  361. o[key] = item[key]==-1?true:false;
  362. }
  363. }
  364. arr.push(o);
  365. });
  366. }else if (caller == 'STANDARDCRAFT') {
  367. Ext.each(data,function(item){
  368. var o = new Object();
  369. for(var key in item){
  370. if(key == 'sd_updatedate' || key == 'sd_id' || key == 'sd_scid' || key == 'sd_detno' ||
  371. key == 'sd_stepcode' || key == 'sd_stepname' || key == 'sd_stepno' || key == 'sd_nextstepcode'
  372. || key == 'sd_nextstepname' || key == 'sd_firststep'||key == 'sd_table'){
  373. o[key] = item[key];
  374. }else{
  375. o[key] = item[key]==-1?true:false;
  376. }
  377. }
  378. arr.push(o);
  379. });
  380. }
  381. return arr;
  382. }
  383. });