WCPlanMaterialLackGrid.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. Ext.define('erp.view.pm.make.WCPlanMaterialLackGrid',{
  2. extend: 'Ext.grid.Panel',
  3. alias: 'widget.erpBatchDealGridPanel',
  4. id: 'batchDealGridPanel',
  5. emptyText : $I18N.common.grid.emptyText,
  6. columnLines : true,
  7. autoScroll : true,
  8. store: [],
  9. region: 'south',
  10. layout : 'fit',
  11. columns: new Array(),
  12. height:height,
  13. GridUtil: Ext.create('erp.util.GridUtil'),
  14. bodyStyle:'background-color:#f1f1f1;',
  15. selModel: Ext.create('Ext.selection.CheckboxModel',{
  16. headerWidth: 0
  17. }),
  18. /*enableLocking:true,*/
  19. /*lockable: true,*/
  20. GridUtil: Ext.create('erp.util.GridUtil'),
  21. BaseUtil: Ext.create('erp.util.BaseUtil'),
  22. plugins: [Ext.create('Ext.grid.plugin.CellEditing', {
  23. clicksToEdit: 1,
  24. listeners:{
  25. 'edit':function(editor,e,Opts){
  26. var record=e.record,data=null,qty=0;
  27. if(record){
  28. data=record.data;
  29. for(var property in data){
  30. if(!/^[a-zA-Z]/.test(property)){
  31. qty+=data[property];
  32. }
  33. }
  34. if(qty>(data.ma_qty-data.ma_madeqty)){
  35. showError('排产数不能超过未交数!请重新输入');
  36. e.record.reject();
  37. }
  38. }
  39. }
  40. }
  41. })],
  42. /* plugins: [Ext.create('Ext.ux.grid.GridHeaderFilters')],*/
  43. initComponent : function(){
  44. var condition = this.condition;
  45. if(!condition){
  46. var urlCondition = this.BaseUtil.getUrlParam('gridCondition');
  47. urlCondition = urlCondition == null || urlCondition == "null" ? "" : urlCondition;
  48. gridCondition = (gridCondition == null || gridCondition == "null") ? "" : gridCondition;
  49. gridCondition = gridCondition + urlCondition;
  50. gridCondition = gridCondition.replace(/IS/g, "=");
  51. if(gridCondition.search(/!/) != -1){
  52. gridCondition = gridCondition.substring(0, gridCondition.length - 4);
  53. }
  54. condition = gridCondition;
  55. }
  56. var gridParam = {caller: this.caller || caller, condition: condition};
  57. this.getGridColumnsAndStore(this, 'pm/make/singleGridPanel.action', gridParam, "");//从后台拿到gridpanel的配置及数据
  58. this.callParent(arguments);
  59. },
  60. loadNewStore:function(grid, url, param, no){
  61. Ext.Ajax.request({//拿到grid的columns
  62. url : basePath + url,
  63. params: param,
  64. method : 'post',
  65. callback : function(options,success,response){
  66. var res = new Ext.decode(response.responseText);
  67. if(res.exceptionInfo){
  68. showError(res.exceptionInfo);return;
  69. }
  70. var data = Ext.decode(res.data.replace(/,}/g, '}').replace(/,]/g, ']'));
  71. //对data 进行处理 必须要先取到主记录的开始时间和结束时间
  72. var Map={},arr=new Array();
  73. var len = data.length;
  74. for(var i=0;i<len;i++){
  75. if(Map[data[i].wd_makecode] == undefined){
  76. var smalldata=data[i];
  77. var startfield=Ext.Date.format(new Date(startdate),'Y-m-d');
  78. var endfield=Ext.Date.format(new Date(enddate),'Y-m-d');
  79. if(new Date(smalldata.ml_begindate)<new Date(startdate)){
  80. smalldata[startfield]=smalldata.ml_mrponhandlack;
  81. }else if(new Date(smalldata.ml_begindate)>new Date(enddate)) smalldata[endfield]=smalldata.ml_mrponhandlack;
  82. else smalldata[smalldata.ml_begindate.substring(0,10)]=smalldata.ml_mrponhandlack;
  83. Map[data[i].ml_makecode] =smalldata;
  84. }else{
  85. var smalldata=Map[data[i].ml_makecode];
  86. smalldata[data[i].ml_begindate.substring(0,10)]=data[i].ml_mrponhandlack;
  87. Map[data[i].ml_makecode]=smalldata;
  88. }
  89. }
  90. for(var property in Map){
  91. arr.push(Map[property]);
  92. }
  93. data=arr;
  94. console.log(data);
  95. grid.store.loadData(data);
  96. }
  97. });
  98. },
  99. getGridColumnsAndStore: function(grid, url, param, no){
  100. var me = this;
  101. grid.setLoading(true);
  102. Ext.Ajax.request({//拿到grid的columns
  103. url : basePath + url,
  104. params: param,
  105. async:false,
  106. method : 'post',
  107. callback : function(options,success,response){
  108. grid.setLoading(false);
  109. var res = new Ext.decode(response.responseText);
  110. if(res.exceptionInfo){
  111. showError(res.exceptionInfo);return;
  112. }
  113. if(res.columns){
  114. var limits = res.limits, limitArr = new Array();
  115. if(limits != null && limits.length > 0) {//权限外字段
  116. limitArr = Ext.Array.pluck(limits, 'lf_field');
  117. }
  118. Ext.each(res.columns, function(column, y){
  119. //power
  120. if(limitArr.length > 0 && Ext.Array.contains(limitArr, column.dataIndex)) {
  121. column.hidden = true;
  122. }
  123. //renderer
  124. me.setRenderer(grid, column);
  125. //logictype
  126. me.setLogicType(grid, column);
  127. });
  128. //data
  129. var data = [];
  130. var gridfields=res.fields;
  131. if(!res.data || res.data.length == 2){
  132. me.add10EmptyData(grid.detno, data);
  133. me.add10EmptyData(grid.detno, data);//添加20条空白数据
  134. } else {
  135. data = Ext.decode(res.data.replace(/,}/g, '}').replace(/,]/g, ']'));
  136. //对data 进行处理 必须要先取到主记录的开始时间和结束时间
  137. var Map={},arr=new Array();
  138. var len = data.length;
  139. for(var i=0;i<len;i++){
  140. if(Map[data[i].ml_makecode] == undefined){
  141. var smalldata=data[i];
  142. smalldata[smalldata.ml_begindate.substring(0,10)]=smalldata.ml_mrponhandlack;
  143. Map[data[i].ml_makecode] =smalldata;
  144. }else{
  145. var smalldata=Map[data[i].ml_makecode];
  146. smalldata[data[i].ml_begindate.substring(0,10)]=data[i].ml_mrponhandlack;
  147. smalldata.ml_mrponhandlack+=data[i].ml_mrponhandlack;
  148. Map[data[i].ml_makecode]=smalldata;
  149. }
  150. }
  151. for(var property in Map){
  152. arr.push(Map[property]);
  153. }
  154. data=arr;
  155. }
  156. //store
  157. //view
  158. if(grid.selModel.views == null){
  159. grid.selModel.views = [];
  160. }
  161. //dbfind
  162. if(res.dbfinds&&res.dbfinds.length > 0){
  163. grid.dbfinds = res.dbfinds;
  164. }
  165. //toolbar
  166. me.setToolbar(grid, res.columns, res.necessaryField, limitArr);
  167. gridcolumns=res.columns;
  168. startdate=Ext.Date.add(new Date(startdate), Ext.Date.DAY, -1);
  169. enddate=Ext.Date.add(new Date(enddate), Ext.Date.DAY, -1);
  170. if(startdate!=null){
  171. var lastcolumns=[];
  172. Ext.Array.each(gridcolumns,function(item,m){
  173. if(item.dataIndex=='ml_begindate'){
  174. var columns=new Array();
  175. var count=(enddate-startdate)/86400000;
  176. for(var i=0;i<count+1;i++){
  177. var field=Ext.Date.format(Ext.Date.add(startdate,Ext.Date.DAY,i),'Y-m-d');
  178. gridfields.push({
  179. name:field,
  180. type:'int'
  181. });
  182. columns.push({
  183. readOnly:false,
  184. header:field.substring(5,10),
  185. cls: "x-grid-header-1",
  186. dataIndex:field,
  187. width : 60,
  188. xtype:'numbercolumn',
  189. align:'right',
  190. format:'0',
  191. editor:{
  192. xtype:'numberfield',
  193. format:'0',
  194. hideTrigger: true
  195. }
  196. });
  197. }
  198. item.columns=columns;
  199. }
  200. lastcolumns[m]=item;
  201. });
  202. gridcolumns=lastcolumns;
  203. }
  204. var store = me.setStore(gridfields, data, grid.groupField, grid.necessaryField);
  205. console.log(data);
  206. console.log(gridcolumns);
  207. grid.store=store;
  208. grid.columns=gridcolumns;
  209. var form = Ext.ComponentQuery.query('form')[0];
  210. if(form){
  211. if(form.readOnly){
  212. grid.readOnly = true;//grid不可编辑
  213. }
  214. }
  215. /*var lockedView = grid.view.lockedView;
  216. if(lockedView){
  217. var tableEl = lockedView.el;
  218. console.log(tableEl);
  219. if(tableEl){
  220. tableEl.dom.style.marginBottom = '27px';
  221. }
  222. }*/
  223. } else {
  224. grid.hide();
  225. var form = Ext.ComponentQuery.query('form')[0];
  226. me.updateFormPosition(form);//字段较少时,修改form布局
  227. }
  228. }
  229. });
  230. },
  231. setRenderer: function(grid, column){
  232. if(!column.haveRendered && column.renderer != null && column.renderer != ""){
  233. if(!grid.RenderUtil){
  234. grid.RenderUtil = Ext.create('erp.util.RenderUtil');
  235. }
  236. var renderName = column.renderer;
  237. if(contains(column.renderer, ':', true)){
  238. var args = new Array();
  239. Ext.each(column.renderer.split(':'), function(a, index){
  240. if(index == 0){
  241. renderName = a;
  242. } else {
  243. args.push(a);
  244. }
  245. });
  246. if(!grid.RenderUtil.args[renderName]){
  247. grid.RenderUtil.args[renderName] = new Object();
  248. }
  249. grid.RenderUtil.args[renderName][column.dataIndex] = args;
  250. }
  251. column.renderer = grid.RenderUtil[renderName];
  252. column.haveRendered = true;
  253. }
  254. },
  255. setLogicType: function(grid, column){
  256. var logic = column.logic;
  257. if(logic != null){
  258. if(logic == 'detno'){
  259. grid.detno = column.dataIndex;
  260. column.width = 40;
  261. column.renderer = function(val, meta) {
  262. meta.tdCls = Ext.baseCSSPrefix + 'grid-cell-special';
  263. return val;
  264. };
  265. } else if(logic == 'keyField'){
  266. grid.keyField = column.dataIndex;
  267. } else if(logic == 'mainField'){
  268. grid.mainField = column.dataIndex;
  269. } else if(logic == 'necessaryField'){
  270. grid.necessaryField = column.dataIndex;
  271. if(!grid.necessaryFields){
  272. grid.necessaryFields = new Array();
  273. }
  274. grid.necessaryFields.push(column.dataIndex);
  275. if(!column.haveRendered){
  276. column.renderer = function(val, meta, record, x, y, store, view){
  277. var c = this.columns[y];
  278. if(val != null && val.toString().trim() != ''){
  279. if(c.xtype == 'datecolumn'){
  280. val = Ext.Date.format(val, 'Y-m-d');
  281. }
  282. return val;
  283. } else {
  284. if(c.xtype == 'datecolumn'){
  285. val = '';
  286. }
  287. return '<img src="' + basePath + 'resource/images/icon/need.png" title="必填字段">' +
  288. '<span style="color:blue;padding-left:2px;" title="必填字段">' + val + '</span>';
  289. }
  290. };
  291. }
  292. } else if(logic == 'groupField'){
  293. grid.groupField = column.dataIndex;
  294. }
  295. }
  296. },
  297. setStore: function(fields, data, groupField, necessaryField){
  298. griddata=data;
  299. if(!Ext.isChrome){
  300. Ext.each(fields, function(f){
  301. if(f.type == 'date'){
  302. f.dateFormat = 'Y-m-d H:m:s';
  303. }
  304. });
  305. }
  306. return Ext.create('Ext.data.Store', {
  307. fields: fields,
  308. data: data,
  309. groupField: groupField,
  310. getSum: function(records, field) {
  311. if (arguments.length < 2) {
  312. return 0;
  313. }
  314. var total = 0,
  315. i = 0,
  316. len = records.length;
  317. if(necessaryField) {
  318. for (; i < len; ++i) {//重写getSum,grid在合计时,只合计填写了必要信息的行
  319. var necessary = records[i].get(necessaryField);
  320. if(necessary != null && necessary != ''){
  321. total += records[i].get(field);
  322. }
  323. }
  324. } else {
  325. for (; i < len; ++i) {
  326. total += records[i].get(field);
  327. }
  328. }
  329. return total;
  330. },
  331. getCount: function() {
  332. if(necessaryField) {
  333. var count = 0;
  334. Ext.each(this.data.items, function(item){//重写getCount,grid在合计时,只合计填写了必要信息的行
  335. if(item.data[necessaryField] != null && item.data[necessaryField] != ''){
  336. count++;
  337. }
  338. });
  339. return count;
  340. }
  341. return this.data.items.length;
  342. }
  343. });
  344. },
  345. setToolbar: function(grid, columns, necessaryField, limitArr){
  346. var items = [];
  347. var bool = true;
  348. if(!grid.dockedItems)
  349. return;
  350. Ext.each(grid.dockedItems.items, function(item){
  351. if(item.dock == 'bottom' && item.items){//bbar已存在
  352. bool = false;
  353. }
  354. });
  355. if(bool){
  356. Ext.each(columns, function(column){
  357. if(limitArr.length == 0 || !Ext.Array.contains(limitArr, column.dataIndex)) {
  358. if(column.summaryType == 'sum'){
  359. items.push('-',{
  360. id: column.dataIndex + '_sum',
  361. itemId: column.dataIndex,
  362. xtype: 'tbtext',
  363. text: column.header + '(sum):0'
  364. });
  365. } else if(column.summaryType == 'average') {
  366. items.push('-',{
  367. id: column.dataIndex + '_average',
  368. itemId: column.dataIndex,
  369. xtype: 'tbtext',
  370. text: column.header + '(average):0'
  371. });
  372. } else if(column.summaryType == 'count') {
  373. items.push('-',{
  374. id: column.dataIndex + '_count',
  375. itemId: column.dataIndex,
  376. xtype: 'tbtext',
  377. text: column.header + '(count):0'
  378. });
  379. }
  380. }
  381. if(column.dataIndex == necessaryField){
  382. column.renderer = function(val){
  383. if(val != null && val.toString().trim() != ''){
  384. return val;
  385. } else {
  386. return '<img src="' + basePath + 'resource/images/icon/need.png" title="必填字段">' +
  387. '<span style="color:blue;padding-left:2px;" title="必填字段">' + val + '</span>';
  388. }
  389. };
  390. }
  391. });
  392. grid.addDocked({
  393. xtype: 'toolbar',
  394. dock: 'bottom',
  395. items: items
  396. });
  397. } else {
  398. var bars = Ext.ComponentQuery.query('erpToolbar');
  399. if(bars.length > 0){
  400. Ext.each(columns, function(column){
  401. if(column.summaryType == 'sum'){
  402. bars[0].add('-');
  403. bars[0].add({
  404. id: column.dataIndex + '_sum',
  405. itemId: column.dataIndex,
  406. xtype: 'tbtext',
  407. text: column.header + '(sum):0'
  408. });
  409. } else if(column.summaryType == 'average') {
  410. bars[0].add('-');
  411. bars[0].add({
  412. id: column.dataIndex + '_average',
  413. itemId: column.dataIndex,
  414. xtype: 'tbtext',
  415. text: column.header + '(average):0'
  416. });
  417. } else if(column.summaryType == 'count') {
  418. bars[0].add('-');
  419. bars[0].add({
  420. id: column.dataIndex + '_count',
  421. itemId: column.dataIndex,
  422. xtype: 'tbtext',
  423. text: column.header + '(count):0'
  424. });
  425. }
  426. });
  427. }
  428. }
  429. },
  430. add10EmptyData: function(detno, data){
  431. if(detno){
  432. var index = data.length == 0 ? 0 : Number(data[data.length-1][detno]);
  433. for(var i=0;i<10;i++){
  434. var o = new Object();
  435. o[detno] = index + i + 1;
  436. data.push(o);
  437. }
  438. } else {
  439. for(var i=0;i<10;i++){
  440. var o = new Object();
  441. data.push(o);
  442. }
  443. }
  444. },
  445. checkGridDirty: function(grid){
  446. var s = grid.getStore().data.items;//获取store里面的数据
  447. var msg = '';
  448. for(var i=0;i<s.length;i++){
  449. if(s[i].dirty){//明细行被修改过哦
  450. msg = msg + '第' + (i+1) + '行:';
  451. var changed = Ext.Object.getKeys(s[i].modified);//拿到被修改了的字段
  452. for(var j = 0;j < changed.length;j++){
  453. Ext.each(grid.columns, function(c, index){
  454. if(index == 0){
  455. return;
  456. }
  457. if(c.dataIndex == changed[j] && c.logic != 'ignore'){//由字段dataindex匹配其text
  458. msg = msg + '<font color=blue>' + c.text + '</font>&nbsp;';
  459. }
  460. });
  461. }
  462. msg = msg + ";";
  463. }
  464. }
  465. if(msg != '' && msg != ';'){//明细行被修改过哦
  466. msg = "明细行<font color=green>" + msg.substring(0, msg.length-1) + "</font>已编辑过";
  467. }
  468. return msg;
  469. },
  470. getGridStore: function(grid){
  471. if(grid == null){
  472. grid = Ext.getCmp('grid');
  473. }
  474. var me = this,
  475. jsonGridData = new Array();
  476. var form = Ext.getCmp('form');
  477. var s = grid.getStore().data.items;//获取store里面的数据
  478. console.log(s);
  479. var dd;
  480. for(var i=0;i<s.length;i++){//将grid里面各行的数据获取并拼成jsonGridData
  481. var data = s[i].data;
  482. dd = new Object();
  483. if(s[i].dirty && !me.isBlank(grid, data)){
  484. Ext.each(gridcolumns, function(c){
  485. console.log(c.logic);
  486. if(c.logic != 'ignore'){//只需显示,无需后台操作的字段,自动略去
  487. if(c.dataIndex=='wd_date'){
  488. Ext.Array.each(c.columns,function(column){
  489. dd[column.dataIndex]=data[column.dataIndex];
  490. });
  491. }else {
  492. if(c.xtype == 'datecolumn'){
  493. c.format = c.format || 'Y-m-d';
  494. if(Ext.isDate(data[c.dataIndex])){
  495. dd[c.dataIndex] = Ext.Date.format(data[c.dataIndex], c.format);
  496. } else {
  497. if(c.editor){
  498. dd[c.dataIndex] = Ext.Date.format(new Date(), c.format);//如果用户没输入日期,或输入有误,就给个默认日期,
  499. }
  500. }
  501. } else if(c.xtype == 'datetimecolumn'){
  502. if(Ext.isDate(data[c.dataIndex])){
  503. dd[c.dataIndex] = Ext.Date.format(data[c.dataIndex], 'Y-m-d H:i:s');//在这里把GMT日期转化成Y-m-d H:i:s格式日期
  504. } else {
  505. if(c.editor){
  506. dd[c.dataIndex] = Ext.Date.format(new Date(), 'Y-m-d H:i:s');//默认日期,
  507. }
  508. }
  509. } else if(c.xtype == 'numbercolumn'){//赋个默认值0吧,不然不好保存
  510. if(data[c.dataIndex] == null || data[c.dataIndex] == ''){
  511. dd[c.dataIndex] = '0';//也可以从data里面去掉这些字段
  512. } else {
  513. dd[c.dataIndex] = s[i].data[c.dataIndex];
  514. }
  515. } else {
  516. dd[c.dataIndex] = s[i].data[c.dataIndex];
  517. }
  518. }
  519. }
  520. });
  521. if(grid.mainField && form && form.keyField){//例如,将pu_id的值赋给pd_puid
  522. dd[grid.mainField] = Ext.getCmp(form.keyField).value;
  523. }
  524. jsonGridData.push(Ext.JSON.encode(dd));
  525. }
  526. }
  527. return jsonGridData;
  528. },
  529. isBlank: function(grid, data) {
  530. var ff = grid.necessaryFields,bool = true;
  531. if(ff) {
  532. Ext.each(ff, function(f) {
  533. if(!Ext.isEmpty(data[f]) && data[f] != 0) {
  534. bool = false;
  535. }else {
  536. bool=true;
  537. return
  538. }
  539. });
  540. } else {
  541. if(!grid.necessaryField || !Ext.isEmpty(data[grid.necessaryField])) {
  542. bool = false;
  543. }
  544. }
  545. return bool;
  546. },
  547. });