WCPlanGrid.js 19 KB

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