GridUtil.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. Ext.define('erp.util.GridUtil',{
  2. /**
  3. * 取grid配置及数据
  4. * 包括dbfind的配置
  5. * @param no 需要自动编号的字段
  6. */
  7. getGridColumnsAndStore: function(grid, url, param, no){
  8. var me = this;
  9. grid.setLoading(true);
  10. Ext.Ajax.request({//拿到grid的columns
  11. url : basePath + url,
  12. params: param,
  13. async: (grid.sync ? false : true),
  14. method : 'post',
  15. callback : function(options,success,response){
  16. grid.setLoading(false);
  17. if (!response) return;
  18. var res = new Ext.decode(response.responseText);
  19. if(res.exceptionInfo){
  20. showError(res.exceptionInfo);return;
  21. }
  22. if(res.columns){
  23. var limits = res.limits, limitArr = new Array();
  24. if(limits != null && limits.length > 0) {//权限外字段
  25. limitArr = Ext.Array.pluck(limits, 'lf_field');
  26. }
  27. Ext.each(res.columns, function(column, y){
  28. // column有取别名
  29. if(column.dataIndex.indexOf(' ') > -1) {
  30. column.dataIndex = column.dataIndex.split(' ')[1];
  31. }
  32. //power
  33. if(limitArr.length > 0 && Ext.Array.contains(limitArr, column.dataIndex)) {
  34. column.hidden = true;
  35. }
  36. //renderer
  37. me.setRenderer(grid, column);
  38. //logictype
  39. me.setLogicType(grid, column, {
  40. headerColor: res.necessaryFieldColor
  41. });
  42. });
  43. //data
  44. var data = [];
  45. if(!res.data || res.data.length == 2){
  46. if (grid.buffered) {
  47. me.add10EmptyData(grid.detno, data);
  48. me.add10EmptyData(grid.detno, data);//添加20条空白数据
  49. } else {
  50. grid.on('reconfigure', function(){// 改为Grid加载后再添加空行,节约200~700ms
  51. me.add10EmptyItems(grid, 40, false);
  52. });
  53. }
  54. } else {
  55. data = Ext.decode(res.data.replace(/,}/g, '}').replace(/,]/g, ']'));
  56. }
  57. //store
  58. var store = me.setStore(grid, res.fields, data, grid.groupField, grid.necessaryField);
  59. //view
  60. if(grid.selModel && grid.selModel.views == null){
  61. grid.selModel.views = [];
  62. }
  63. //dbfind
  64. if(res.dbfinds && res.dbfinds.length > 0){
  65. grid.dbfinds = res.dbfinds;
  66. }
  67. //reconfigure
  68. if(grid.sync) {//同步加载的Grid
  69. grid.reconfigure(store, res.columns);
  70. grid.on('afterrender', function(){
  71. me.setToolbar(grid, grid.columns, grid.necessaryField, limitArr);
  72. });
  73. } else {
  74. //toolbar
  75. if (grid.generateSummaryData === undefined) {// 改为Grid加载后再添加合计,节约60ms
  76. me.setToolbar(grid, res.columns, grid.necessaryField, limitArr);
  77. }
  78. grid.reconfigure(store, res.columns);
  79. }
  80. if(grid.buffered) {//缓冲数据的Grid
  81. grid.verticalScroller = Ext.create('Ext.grid.PagingScroller', {
  82. activePrefetch: false,
  83. store: store
  84. });
  85. store.guaranteeRange(0, Math.min(store.pageSize, store.prefetchData.length) - 1);
  86. }
  87. var form = Ext.ComponentQuery.query('form');
  88. if(form && form.length > 0){
  89. grid.readOnly = form[0].readOnly;//grid不可编辑
  90. }
  91. } else {
  92. grid.hide();
  93. var form = Ext.ComponentQuery.query('form')[0];
  94. if(form && !form.isStatic) {
  95. if(form.items.items.length == 0) {
  96. form.on('afterload', function(){
  97. me.updateFormPosition(form);//字段较少时,修改form布局
  98. });
  99. } else {
  100. me.updateFormPosition(form);//字段较少时,修改form布局
  101. }
  102. }
  103. }
  104. }
  105. });
  106. },
  107. setRenderer: function(grid, column){
  108. if(!column.haveRendered){
  109. if(column.renderer != null && column.renderer != "") {
  110. if(!grid.RenderUtil){
  111. grid.RenderUtil = Ext.create('erp.util.RenderUtil');
  112. }
  113. var renderName = column.renderer;
  114. if(contains(column.renderer, ':', true)){
  115. var args = new Array();
  116. Ext.each(column.renderer.split(':'), function(a, index){
  117. if(index == 0){
  118. renderName = a;
  119. } else {
  120. args.push(a);
  121. }
  122. });
  123. if(!grid.RenderUtil.args[renderName]){
  124. grid.RenderUtil.args[renderName] = new Object();
  125. }
  126. grid.RenderUtil.args[renderName][column.dataIndex] = args;
  127. }
  128. column.renderer = grid.RenderUtil[renderName];
  129. column.haveRendered = true;
  130. } else if(column.readOnly){
  131. column.renderer = function(val, meta, record, x, y, store, view){
  132. meta.style = "background: #e0e0e0;";
  133. var c = this.columns[y];
  134. if(val != null && val.toString().trim() != ''){
  135. if(c.xtype == 'datecolumn' && typeof val === 'object'){
  136. val = Ext.Date.format(val, 'Y-m-d');
  137. } else if(c.xtype == 'numbercolumn' && val.toString().trim() == '0') {
  138. val = '';
  139. }
  140. return val;
  141. } else {
  142. if(c.xtype == 'datecolumn'){
  143. val = '';
  144. }
  145. return val;
  146. }
  147. }
  148. column.haveRendered = true;
  149. }
  150. }
  151. },
  152. setLogicType: function(grid, column, headerCss){
  153. var logic = column.logic;
  154. if(logic != null){
  155. if(logic == 'detno'){
  156. grid.detno = column.dataIndex;
  157. column.width = 40;
  158. column.align = 'center';
  159. column.renderer = function(val, meta) {
  160. meta.tdCls = Ext.baseCSSPrefix + 'grid-cell-special';
  161. return val;
  162. };
  163. } else if(logic == 'keyField'){
  164. grid.keyField = column.dataIndex;
  165. } else if(logic == 'mainField'){
  166. grid.mainField = column.dataIndex;
  167. }else if(logic == 'orNecessField'){
  168. if(!grid.orNecessField){
  169. grid.orNecessField = new Array();
  170. }
  171. grid.orNecessField.push(column.dataIndex);
  172. }else if(logic == 'necessaryField'){
  173. grid.necessaryField = column.dataIndex;
  174. if(!grid.necessaryFields){
  175. grid.necessaryFields = new Array();
  176. }
  177. grid.necessaryFields.push(column.dataIndex);
  178. if(!column.haveRendered){
  179. column.renderer = function(val, meta, record, x, y, store, view){
  180. var c = this.columns[y];
  181. if(val != null && val.toString().trim() != ''){
  182. if(c.xtype == 'datecolumn' && typeof val === 'object'){
  183. val = Ext.Date.format(val, 'Y-m-d');
  184. } else if(c.xtype == 'numbercolumn') {
  185. val = Ext.util.Format.number(val, c.format || '0,000.00');
  186. } else if(c.xtype == 'combocolumn'){
  187. if(!Ext.isEmpty(val)) {
  188. var g = view.ownerCt,h = g.columns[y],f = h.field, k;
  189. if ((k = (h.editor || h.filter)) && k.store) {
  190. var t = null,dd = k.store.data;
  191. t = Ext.Array.filter(dd, function(d, index){
  192. return d.value == val;
  193. });
  194. if (t && t.length > 0) {
  195. return t[0].display;
  196. }
  197. } else if (f) {
  198. if(f.store) {
  199. var t = f.store.findRecord('value', val);
  200. if (t)
  201. return t.get('display');
  202. } else
  203. return f.rawValue;
  204. }
  205. return val;
  206. }
  207. }
  208. return val;
  209. } else {
  210. if(c.xtype == 'datecolumn'){
  211. val = '';
  212. }
  213. return '<img src="' + basePath + 'resource/images/icon/need.png" title="必填字段">' +
  214. '<span style="color:blue;padding-left:2px;" title="必填字段">' + val + '</span>';
  215. }
  216. };
  217. }
  218. if(headerCss.headerColor)
  219. column.style = 'color:#' + headerCss.headerColor;
  220. } else if(logic == 'groupField'){
  221. grid.groupField = column.dataIndex;
  222. }
  223. }
  224. },
  225. setStore: function(grid, fields, data, groupField, necessaryField){
  226. Ext.each(fields, function(f){
  227. if(f.name.indexOf(' ') > -1) {// column有取别名
  228. f.name = f.name.split(' ')[1];
  229. }
  230. if(!Ext.isChrome){
  231. if(f.type == 'date'){
  232. f.dateFormat = 'Y-m-d H:m:s';
  233. }
  234. }
  235. });
  236. var modelName = 'ext-model-' + grid.id;
  237. Ext.define(modelName, {
  238. extend: 'Ext.data.Model',
  239. fields: fields
  240. });
  241. var config = {
  242. model: modelName,
  243. groupField: groupField,
  244. getSum: function(records, field) {
  245. if (arguments.length < 2) {
  246. return 0;
  247. }
  248. var total = 0,
  249. i = 0,
  250. len = records.length;
  251. if(necessaryField) {
  252. for (; i < len; ++i) {//重写getSum,grid在合计时,只合计填写了必要信息的行
  253. var necessary = records[i].get(necessaryField);
  254. if(necessary != null && necessary != ''){
  255. total += records[i].get(field);
  256. }
  257. }
  258. } else {
  259. for (; i < len; ++i) {
  260. total += records[i].get(field);
  261. }
  262. }
  263. return total;
  264. },
  265. getCount: function() {
  266. if(necessaryField) {
  267. var count = 0;
  268. Ext.each(this.data.items, function(item){//重写getCount,grid在合计时,只合计填写了必要信息的行
  269. if(item.data[necessaryField] != null && item.data[necessaryField] != ''){
  270. count++;
  271. }
  272. });
  273. return count;
  274. }
  275. return this.data.items.length;
  276. }
  277. };
  278. if(grid.buffered) {//grid数据缓存
  279. config.buffered = true;
  280. config.pageSize = 200;
  281. config.purgePageCount = 0;
  282. config.proxy = {
  283. type: 'memory'
  284. };
  285. } else {
  286. config.data = data;
  287. }
  288. var store = Ext.create('Ext.data.Store', config);
  289. store.each(function(item, x){
  290. item.index = x;
  291. });
  292. if(grid.buffered) {
  293. var ln = data.length, records = [], i = 0;
  294. for (; i < ln; i++) {
  295. records.push(Ext.create(modelName, data[i]));
  296. }
  297. store.cacheRecords(records);
  298. }
  299. return store;
  300. },
  301. setToolbar: function(grid, columns, necessaryField, limitArr){
  302. var items = [];
  303. var bool = true;
  304. if(!grid.dockedItems)
  305. return;
  306. Ext.each(grid.dockedItems.items, function(item){
  307. if(item.dock == 'bottom' && item.items){//bbar已存在
  308. bool = false;
  309. }
  310. });
  311. if(bool){
  312. Ext.each(columns, function(column){
  313. if(limitArr.length == 0 || !Ext.Array.contains(limitArr, column.dataIndex)) {
  314. if(column.summaryType == 'sum'){
  315. items.push('-',{
  316. id: column.dataIndex + '_sum',
  317. itemId: column.dataIndex,
  318. xtype: 'tbtext',
  319. text: column.text + '(sum):0'
  320. });
  321. } else if(column.summaryType == 'average') {
  322. items.push('-',{
  323. id: column.dataIndex + '_average',
  324. itemId: column.dataIndex,
  325. xtype: 'tbtext',
  326. text: column.text + '(average):0'
  327. });
  328. } else if(column.summaryType == 'count') {
  329. items.push('-',{
  330. id: column.dataIndex + '_count',
  331. itemId: column.dataIndex,
  332. xtype: 'tbtext',
  333. text: column.text + '(count):0'
  334. });
  335. } else if(column.summaryType == 'results') {
  336. items.push('-',{
  337. id: column.dataIndex + '_results',
  338. itemId: column.dataIndex,
  339. xtype: 'tbtext',
  340. text: '筛选结果:0条'
  341. });
  342. }
  343. }
  344. if(column.dataIndex == necessaryField){
  345. column.renderer = function(val){
  346. if(val != null && val.toString().trim() != ''){
  347. return val;
  348. } else {
  349. return '<img src="' + basePath + 'resource/images/icon/need.png" title="必填字段">' +
  350. '<span style="color:blue;padding-left:2px;" title="必填字段">' + val + '</span>';
  351. }
  352. };
  353. }
  354. });
  355. grid.addDocked({
  356. xtype: 'toolbar',
  357. dock: 'bottom',
  358. items: items
  359. });
  360. } else {
  361. var bars = Ext.ComponentQuery.query('erpToolbar');
  362. if(bars.length > 0){
  363. Ext.each(columns, function(column){
  364. if(column.summaryType == 'sum'){
  365. bars[0].add('-');
  366. bars[0].add({
  367. id: column.dataIndex + '_sum',
  368. itemId: column.dataIndex,
  369. xtype: 'tbtext',
  370. text: column.text + '(sum):0'
  371. });
  372. } else if(column.summaryType == 'average') {
  373. bars[0].add('-');
  374. bars[0].add({
  375. id: column.dataIndex + '_average',
  376. itemId: column.dataIndex,
  377. xtype: 'tbtext',
  378. text: column.text + '(average):0'
  379. });
  380. } else if(column.summaryType == 'count') {
  381. bars[0].add('-');
  382. bars[0].add({
  383. id: column.dataIndex + '_count',
  384. itemId: column.dataIndex,
  385. xtype: 'tbtext',
  386. text: column.text + '(count):0'
  387. });
  388. }
  389. });
  390. }
  391. }
  392. },
  393. updateFormPosition: function(form){
  394. var height = window.innerHeight;
  395. var width = window.innerWidth;
  396. if(Ext.isIE){
  397. height = screen.height*0.75;
  398. width = screen.width*0.75;
  399. }
  400. if(form){
  401. if(form.items.items.length > 12){
  402. form.setHeight(height);
  403. } else {//少于12个字段的单form页面,强制居中显示
  404. if(form.items.items == 0){
  405. form.on('afterlayout', function(){
  406. if(0 < form.items.items.length <= 12){
  407. Ext.each(form.items.items, function(item){
  408. if(item.columnWidth >= 0.25 && item.columnWidth < 0.6){
  409. item.setWidth(width*0.65*0.5);
  410. item.columnWidth = 0.5;
  411. } else if(item.columnWidth >= 0.6) {
  412. item.setWidth(width*0.65);
  413. item.columnWidth = 1;
  414. }
  415. });
  416. }
  417. });
  418. } else {
  419. Ext.each(form.items.items, function(item){
  420. if(item.columnWidth >= 0.25 && item.columnWidth < 0.6){
  421. item.setWidth(width*0.65*0.5);
  422. item.columnWidth = 0.5;
  423. } else if(item.columnWidth >= 0.6) {
  424. item.setWidth(width*0.65);
  425. item.columnWidth = 1;
  426. }
  427. });
  428. }
  429. form.setHeight('60%');
  430. form.setWidth('70%');
  431. form.el.applyStyles('margin:10% auto;border-width: 0 1px 1px 1px;');
  432. }
  433. }
  434. },
  435. loadNewStore: function(grid, param){
  436. var me = this;
  437. grid.setLoading(true);//loading...
  438. Ext.Ajax.request({//拿到grid的columns
  439. url : basePath + "common/loadNewGridStore.action",
  440. params: param,
  441. method : 'post',
  442. callback : function(options,success,response){
  443. grid.setLoading(false);
  444. var res = new Ext.decode(response.responseText);
  445. if(res.exceptionInfo){
  446. showError(res.exceptionInfo);return;
  447. }
  448. var data = res.data;
  449. if(!data || data.length == 0){
  450. grid.store.removeAll();
  451. me.add10EmptyItems(grid);
  452. } else {
  453. if(grid.buffered) {
  454. var ln = data.length, records = [], i = 0;
  455. for (; i < ln; i++) {
  456. records.push(Ext.create(grid.store.model.getName(), data[i]));
  457. }
  458. grid.store.purgeRecords();
  459. grid.store.cacheRecords(records);
  460. grid.store.totalCount = ln;
  461. grid.store.guaranteedStart = -1;
  462. grid.store.guaranteedEnd = -1;
  463. var a = grid.store.pageSize - 1;
  464. a = a > ln - 1 ? ln - 1 : a;
  465. grid.store.guaranteeRange(0, a);
  466. } else {
  467. grid.store.loadData(data);
  468. }
  469. }
  470. //自定义event
  471. grid.addEvents({
  472. storeloaded: true
  473. });
  474. grid.fireEvent('storeloaded', grid, data);
  475. }
  476. });
  477. },
  478. /**
  479. * 从index行开始,往grid里面加十空行
  480. * @param detno 编号字段
  481. * @param data 需要添加空白数据的data
  482. */
  483. add10EmptyData: function(detno, data){
  484. if(detno){
  485. var index = data.length == 0 ? 0 : Number(data[data.length-1][detno]);
  486. for(var i=0;i<20;i++){
  487. var o = new Object();
  488. o[detno] = index + i + 1;
  489. data.push(o);
  490. }
  491. } else {
  492. for(var i=0;i<20;i++){
  493. var o = new Object();
  494. data.push(o);
  495. }
  496. }
  497. },
  498. /**
  499. * 从index行开始,往grid里面加十空行
  500. * @param grid
  501. */
  502. add10EmptyItems: function(grid, count, append){
  503. var store = grid.store,
  504. items = store.data.items, arr = new Array();
  505. var detno = grid.detno;
  506. count = count || 10;
  507. append = append === undefined ? true : false;
  508. if(typeof grid.sequenceFn === 'function')
  509. grid.sequenceFn.call(grid, count);
  510. else {
  511. if(detno){
  512. var index = items.length == 0 ? 0 : Number(store.last().get(detno));
  513. for(var i=0;i < count;i++ ){
  514. var o = new Object();
  515. o[detno] = index + i + 1;
  516. arr.push(o);
  517. }
  518. } else {
  519. for(var i=0;i < count;i++ ){
  520. var o = new Object();
  521. arr.push(o);
  522. }
  523. }
  524. store.loadData(arr, append);
  525. var i = 0;
  526. store.each(function(item, x){
  527. if(item.index) {
  528. i = item.index;
  529. } else {
  530. if (i) {
  531. i++;
  532. item.index = i;
  533. } else {
  534. item.index = x;
  535. }
  536. }
  537. });
  538. }
  539. },
  540. isBlank: function(grid, data) {
  541. if(typeof grid.isEmptyRecord === 'function') {
  542. return grid.isEmptyRecord.call(grid, data);
  543. } else {
  544. var ff = grid.necessaryFields,bool = true;
  545. var of = grid.orNecessField, c;
  546. if(ff) {
  547. bool = false;
  548. Ext.each(ff, function(f) {
  549. c = grid.down('gridcolumn[dataIndex=' + f + ']');
  550. if(Ext.isEmpty(data[f]) || (data[f] == 0 && c
  551. && c.xtype != 'ynnvcolumn' && c.xtype != 'yncolumn')) {
  552. bool = true;return;
  553. }
  554. });
  555. } else if(of){
  556. Ext.each(of,function(f){
  557. if(!Ext.isEmpty(data[f]) && data[f] != 0){
  558. bool = false;
  559. return;
  560. }
  561. });
  562. } else {
  563. if(!grid.necessaryField || !Ext.isEmpty(data[grid.necessaryField])) {
  564. bool = false;
  565. }
  566. }
  567. return bool;
  568. }
  569. },
  570. isEmpty: function(grid) {
  571. if(grid == null){
  572. grid = Ext.getCmp('grid');
  573. }
  574. var me = this, i = 0, items = grid.getStore().data.items, l = items.length;
  575. for(;i < l;i++) {
  576. if(!me.isBlank(grid, items[i].data)) {
  577. return false;
  578. }
  579. }
  580. return true;
  581. },
  582. isDirty: function(grid) {
  583. if(grid == null){
  584. grid = Ext.getCmp('grid');
  585. }
  586. var i = 0, items = grid.getStore().data.items, l = items.length;
  587. for(;i < l;i++) {
  588. if( items[i].dirty ) {
  589. return true;
  590. }
  591. }
  592. return false;
  593. },
  594. /**
  595. * 获取grid已编辑但部分必填字段没填写的行,并提示
  596. */
  597. getUnFinish: function(grid) {
  598. var me = this, cols = {};
  599. Ext.Array.each(grid.columns, function(c){
  600. if(c.dataIndex)
  601. cols[c.dataIndex] = c.text || c.header;
  602. });
  603. var errs = [];
  604. grid.store.each(function(record){
  605. if(record.dirty && me.isBlank(grid, record.data)) {
  606. var ff = grid.necessaryFields, s = '';
  607. if(ff) {
  608. Ext.each(ff, function(f) {
  609. c = grid.down('gridcolumn[dataIndex=' + f + ']');
  610. if(c.logic != 'ignore' && (Ext.isEmpty(record.get(f)) || (record.get(f) == 0 && c
  611. && c.xtype != 'ynnvcolumn' && c.xtype != 'yncolumn'))) {
  612. s += '<u> ' + (c.text || c.header) + ' </u> ';
  613. }
  614. });
  615. } else if(grid.necessaryField) {
  616. var f = grid.necessaryField, c = grid.down('gridcolumn[dataIndex=' + f + ']');
  617. if(c.logic != 'ignore' && (Ext.isEmpty(record.get(f)) || (record.get(f) == 0 && c
  618. && c.xtype != 'ynnvcolumn' && c.xtype != 'yncolumn'))) {
  619. s += '<u> ' + (c.text || c.header) + ' </u> ';
  620. }
  621. }
  622. if(s.length > 0)
  623. errs.push((grid.detno ? ('行: ' + record.get(grid.detno) + ', ') : '') + s);
  624. }
  625. });
  626. return errs.join('<br>');
  627. },
  628. /**
  629. * 获取grid已保存但部分必填字段没填写的行,并提示
  630. */
  631. getInvalid: function(grid) {
  632. if(grid.keyField) {
  633. var me = this, cols = {};
  634. Ext.Array.each(grid.columns, function(c){
  635. if(c.dataIndex)
  636. cols[c.dataIndex] = c.text || c.header;
  637. });
  638. var errs = [];
  639. grid.store.each(function(record){
  640. var key = record.get(grid.keyField);
  641. if(key && key > 0 && me.isBlank(grid, record.data)) {
  642. var ff = grid.necessaryFields, s = '';
  643. if(ff) {
  644. Ext.each(ff, function(f) {
  645. c = grid.down('gridcolumn[dataIndex=' + f + ']');
  646. if(c.logic != 'ignore' && (Ext.isEmpty(record.get(f)) || (record.get(f) == 0 && c
  647. && c.xtype != 'ynnvcolumn' && c.xtype != 'yncolumn'))) {
  648. s += '<u> ' + (c.text || c.header) + ' </u> ';
  649. }
  650. });
  651. } else if(grid.necessaryField) {
  652. var f = grid.necessaryField, c = grid.down('gridcolumn[dataIndex=' + f + ']');
  653. if(c.logic != 'ignore' && (Ext.isEmpty(record.get(f)) || (record.get(f) == 0 && c
  654. && c.xtype != 'ynnvcolumn' && c.xtype != 'yncolumn'))) {
  655. s += '<u> ' + (c.text || c.header) + ' </u> ';
  656. }
  657. }
  658. if(s.length > 0)
  659. errs.push((grid.detno ? ('行: ' + record.get(grid.detno) + ', ') : '') + s);
  660. }
  661. });
  662. return errs.join('<br>');
  663. }
  664. return null;
  665. },
  666. /**
  667. * 拿到grid里面要提交的数据
  668. */
  669. getGridStore: function(grid){
  670. if(grid == null){
  671. grid = Ext.getCmp('grid');
  672. }
  673. var me = this,
  674. jsonGridData = new Array();
  675. var form = Ext.getCmp('form');
  676. if(grid!=null){
  677. var s = grid.getStore().data.items;//获取store里面的数据
  678. var dd;
  679. for(var i=0;i<s.length;i++){//将grid里面各行的数据获取并拼成jsonGridData
  680. var data = s[i].data;
  681. dd = new Object();
  682. if((s[i].dirty||(getUrlParam('_copyConf')!=null && getUrlParam('gridCondition')==null)) //通用复制单据保存时数据不是dirty的
  683. && !me.isBlank(grid, data)){
  684. Ext.each(grid.columns, function(c){
  685. if((!c.isCheckerHd)&&(c.logic != 'ignore') && c.dataIndex){//只需显示,无需后台操作的字段,自动略去
  686. if(c.xtype == 'datecolumn'){
  687. c.format = c.format || 'Y-m-d';
  688. if(Ext.isDate(data[c.dataIndex])){
  689. dd[c.dataIndex] = Ext.Date.format(data[c.dataIndex], c.format);
  690. } else {
  691. if(c.editor&&c.logic!='unauto'){
  692. dd[c.dataIndex] = Ext.Date.format(new Date(), c.format);//如果用户没输入日期,或输入有误,就给个默认日期,
  693. }
  694. }
  695. } else if(c.xtype == 'datetimecolumn'){
  696. if(Ext.isDate(data[c.dataIndex])){
  697. dd[c.dataIndex] = Ext.Date.format(data[c.dataIndex], 'Y-m-d H:i:s');//在这里把GMT日期转化成Y-m-d H:i:s格式日期
  698. } else {
  699. if(c.editor&&c.logic!='unauto'){
  700. dd[c.dataIndex] = Ext.Date.format(new Date(), 'Y-m-d H:i:s');//默认日期,
  701. }
  702. }
  703. } else if(c.xtype == 'numbercolumn'){//赋个默认值0吧,不然不好保存
  704. if(data[c.dataIndex] == null || data[c.dataIndex] == '' || String(data[c.dataIndex]) == 'NaN'){
  705. dd[c.dataIndex] = '0';//也可以从data里面去掉这些字段
  706. } else {
  707. dd[c.dataIndex] = "" + s[i].data[c.dataIndex];
  708. }
  709. } else {
  710. dd[c.dataIndex] = s[i].data[c.dataIndex];
  711. }
  712. if (c.defaultValue && (dd[c.dataIndex] == null || dd[c.dataIndex] == '0')) {
  713. dd[c.dataIndex] = c.defaultValue;
  714. }
  715. }
  716. });
  717. if(grid.mainField && form && form.keyField){//例如,将pu_id的值赋给pd_puid
  718. dd[grid.mainField] = Ext.getCmp(form.keyField).value;
  719. }
  720. jsonGridData.push(Ext.JSON.encode(dd));
  721. }
  722. }
  723. }
  724. return jsonGridData;
  725. },
  726. getAllGridStore: function(grid){
  727. if(grid == null){
  728. grid = Ext.getCmp('grid');
  729. }
  730. var jsonGridData = new Array();
  731. var form = Ext.getCmp('form');
  732. grid.getStore().each(function(item){//将grid里面各行的数据获取并拼成jsonGridData
  733. var data = Ext.clone(item.data);
  734. if(data[grid.necessaryField] != null && data[grid.necessaryField] != ""){
  735. if(grid.mainField && form && form.keyField){//例如,将pu_id的值赋给pd_puid
  736. data[grid.mainField] = Ext.getCmp(form.keyField).value;
  737. }
  738. Ext.each(grid.columns, function(c){
  739. if(c.xtype == 'datecolumn'){
  740. if(Ext.isDate(data[c.dataIndex])){
  741. data[c.dataIndex] = Ext.Date.toString(data[c.dataIndex]);//在这里把GMT日期转化成Y-m-d格式日期
  742. } else {
  743. data[c.dataIndex] = Ext.Date.format(new Date(), 'Y-m-d');//如果用户没输入日期,或输入有误,就给个默认日期,
  744. //或干脆return;并且提示一下用户
  745. }
  746. } else if(c.xtype == 'numbercolumn'){//赋个默认值0吧,不然不好保存
  747. if(data[c.dataIndex] == null || data[c.dataIndex] == ''){
  748. data[c.dataIndex] = '0';//也可以从data里面去掉这些字段
  749. } else {
  750. data[c.dataIndex] = "" + data[c.dataIndex];
  751. }
  752. }
  753. });
  754. jsonGridData.push(Ext.JSON.encode(data));
  755. }
  756. });
  757. return jsonGridData;
  758. },
  759. /**
  760. * 检查grid是否已修改
  761. */
  762. checkGridDirty: function(grid){
  763. var store = grid.getStore().data.items;//获取store里面的数据
  764. var msg = '';
  765. for(var i=0;i<store.length;i++){
  766. if(store[i].dirty){//明细行被修改过哦
  767. var changed = Ext.Object.getKeys(store[i].modified);//拿到被修改了的字段
  768. var s = '';
  769. for(var j = 0;j < changed.length;j++){
  770. Ext.each(grid.columns, function(c, index){
  771. if((!c.isCheckerHd) && (c.logic != 'ignore') && (c.logic != 'detno')){
  772. if(c.dataIndex == changed[j]){//由字段dataindex匹配其text
  773. if(s == '') {
  774. s = '第' + (i+1) + '行:';
  775. }
  776. s += '<font color=blue>' + c.text + '</font>&nbsp;';
  777. }
  778. }
  779. });
  780. }
  781. if(s != '')
  782. msg += s;
  783. }
  784. }
  785. if(msg != '' && msg != ';'){//明细行被修改过哦
  786. msg = "明细行<font color=green>" + msg.substring(0, msg.length-1) + "</font>已编辑过";
  787. } else {
  788. msg = '';
  789. }
  790. return msg;
  791. },
  792. getRecordByCode: function(param){
  793. Ext.Ajax.request({
  794. url : basePath + 'common/getRecordByCode.action',
  795. params: param,
  796. method : 'post',
  797. callback : function(options,success,response){
  798. //var res = new Ext.decode(response.responseText);
  799. }
  800. });
  801. },
  802. onGridItemClick: function(selModel, record, id){
  803. var me = this.GridUtil || this;
  804. var grid = selModel.ownerCt;
  805. if(grid && !grid.readOnly && !grid.NoAdd){
  806. var index = grid.store.indexOf(record);
  807. if(index == grid.store.indexOf(grid.store.last())){
  808. me.add10EmptyItems(grid);//就再加10行
  809. }
  810. var btn = grid.down('erpDeleteDetailButton');
  811. if(btn)
  812. btn.setDisabled(false);
  813. btn = grid.down('erpAddDetailButton');
  814. if(btn)
  815. btn.setDisabled(false);
  816. btn = grid.down('copydetail');
  817. if(btn)
  818. btn.setDisabled(false);
  819. btn = grid.down('pastedetail');
  820. if(btn)
  821. btn.setDisabled(false);
  822. btn = grid.down('updetail');
  823. if(btn)
  824. btn.setDisabled(false);
  825. btn = grid.down('downdetail');
  826. if(btn)
  827. btn.setDisabled(false);
  828. if(grid.down('tbtext[name=row]')){
  829. grid.down('tbtext[name=row]').setText(index);
  830. }
  831. }
  832. },
  833. onGridItemClickForEditGrid: function(selModel, record, id){
  834. var grid = id == null ? Ext.getCmp('grid') : Ext.getCmp(id);
  835. var index = null;
  836. if(grid.detno){
  837. index = record.data[grid.detno];
  838. index = index == null ? (record.index + 1) : index;
  839. if(index.toString() == 'NaN'){
  840. index = '';
  841. }
  842. if(index == grid.store.data.items[grid.store.data.items.length-1].data[grid.detno]){//如果选择了最后一行
  843. this.add10EmptyItems(grid);//就再加10行
  844. }
  845. } else {
  846. index = record.index + 1;
  847. if(index.toString() == 'NaN'){
  848. index = '';
  849. }
  850. if(index == grid.store.data.items[grid.store.data.items.length-1].index + 1){//如果选择了最后一行
  851. this.add10EmptyItems(grid);//就再加10行
  852. }
  853. }
  854. // var btn = grid.down('erpDeleteButton');
  855. // btn.setDisabled(false);
  856. },
  857. deleteDetailForEditGrid:function(btn){
  858. var grid = btn.ownerCt.ownerCt;
  859. var records = grid.selModel.getSelection();
  860. if(records.length > 0){
  861. if(grid.keyField){
  862. if(records[0].data[grid.keyField] != null && records[0].data[grid.keyField] > 0){
  863. warnMsg($I18N.common.msg.ask_del, function(btn){
  864. if(btn == 'yes'){
  865. grid.BaseUtil.getActiveTab().setLoading(true);//loading...
  866. Ext.Ajax.request({
  867. url : basePath + "common/deleteDetail.action",
  868. params: {
  869. caller: caller,
  870. condition: grid.keyField + "=" + records[0].data[grid.keyField]
  871. },
  872. method : 'post',
  873. callback : function(options,success,response){
  874. grid.BaseUtil.getActiveTab().setLoading(false);
  875. var localJson = new Ext.decode(response.responseText);
  876. if(localJson.exceptionInfo){
  877. showError(localJson.exceptionInfo);return;
  878. }
  879. if(localJson.success){
  880. grid.store.remove(records[0]);
  881. delSuccess(function(){
  882. });//@i18n/i18n.js
  883. } else {
  884. delFailure();
  885. }
  886. }
  887. });
  888. }
  889. });
  890. } else {
  891. grid.store.remove(records[0]);
  892. }
  893. } else {
  894. if(records[0].data[grid.keyField] != null && records[0].data[grid.keyField] > 0){
  895. showError("grid未配置keyField,无法删除该行数据!");
  896. } else {
  897. grid.store.remove(records[0]);
  898. }
  899. }
  900. }
  901. },
  902. onSave: function(grid, saveUrl){
  903. var me = this;
  904. if(grid){
  905. var param = me.getGridStore();
  906. if(grid.necessaryField.length > 0 && (param == null || param == '')){
  907. showError('明细表还未添加数据');return;
  908. } else {
  909. var url = saveUrl || grid.saveUrl || Ext.getCmp('form').saveUrl;
  910. me.getActiveTab().setLoading(true);//loading...
  911. Ext.Ajax.request({
  912. url : basePath + url,
  913. params : {
  914. param: unescape(param.toString().replace(/\\/g,"%"))
  915. },
  916. method : 'post',
  917. callback : function(options,success,response){
  918. me.getActiveTab().setLoading(false);
  919. var localJson = new Ext.decode(response.responseText);
  920. if(localJson.success){
  921. saveSuccess(function(){
  922. //add成功后刷新grid
  923. me.loadNewStore(grid, {
  924. caller: caller,
  925. condition: grid.mainField + '=' + Ext.getCmp(Ext.getCmp('form').keyField).value
  926. });
  927. });
  928. } else if(localJson.exceptionInfo){
  929. var str = localJson.exceptionInfo;
  930. if(str.trim().substr(0, 12) == 'AFTERSUCCESS'){
  931. str = str.replace('AFTERSUCCESS', '');
  932. saveSuccess(function(){
  933. //add成功后刷新grid
  934. me.loadNewStore(grid, {
  935. caller: caller,
  936. condition: grid.mainField + '=' + Ext.getCmp(Ext.getCmp('form').keyField).value
  937. });
  938. });
  939. showError(str);
  940. } else {
  941. showError(str);
  942. return;
  943. }
  944. } else{
  945. saveFailure();//@i18n/i18n.js
  946. }
  947. }
  948. });
  949. }
  950. }
  951. },
  952. onUpdate: function(grid, updateUrl){
  953. var me = this;
  954. if(grid){
  955. var param = me.getGridStore();
  956. if(grid.necessaryField.length > 0 && (param == null || param == '')){
  957. showError('明细表还未添加数据');return;
  958. } else {
  959. var url = updateUrl || grid.updateUrl || Ext.getCmp('form').updateUrl;
  960. me.update(param, url, function(){
  961. me.loadNewStore(grid, {
  962. caller: caller,
  963. condition: grid.mainField + '=' + Ext.getCmp(Ext.getCmp('form').keyField).value
  964. });
  965. });
  966. }
  967. }
  968. },
  969. /**
  970. * @param param
  971. * @param param2
  972. * ...
  973. * @param updateUrl
  974. * @param callback
  975. */
  976. update: function() {
  977. if(arguments.length < 3)
  978. return;
  979. var me = this, params = {}, url = arguments[arguments.length - 2],
  980. callback = arguments[arguments.length - 1];
  981. params.param = unescape(arguments[0].toString().replace(/\\/g,"%"));
  982. for(var i=1; i<arguments.length - 2; i++) {
  983. if (arguments[i] != null)
  984. params['param' + (i + 1)] = unescape(arguments[i].toString());
  985. }
  986. me.getActiveTab().setLoading(true);
  987. Ext.Ajax.request({
  988. url : basePath + url,
  989. params : params,
  990. method : 'post',
  991. callback : function(options,success,response){
  992. me.getActiveTab().setLoading(false);
  993. var localJson = new Ext.decode(response.responseText);
  994. if(localJson.success){
  995. updateSuccess(function(){
  996. callback.call(me);
  997. });
  998. } else if(localJson.exceptionInfo){
  999. var str = localJson.exceptionInfo;
  1000. if(str.trim().substr(0, 12) == 'AFTERSUCCESS'){
  1001. str = str.replace('AFTERSUCCESS', '');
  1002. updateSuccess(function(){
  1003. callback.call(me);
  1004. });
  1005. showError(str);
  1006. } else {
  1007. showError(str);
  1008. return;
  1009. }
  1010. } else{
  1011. saveFailure();
  1012. }
  1013. }
  1014. });
  1015. },
  1016. getActiveTab: function(){
  1017. var tab = null;
  1018. if(Ext.getCmp("content-panel")){
  1019. tab = Ext.getCmp("content-panel").getActiveTab();
  1020. }
  1021. if(!tab && parent.Ext.getCmp("content-panel"))
  1022. tab = parent.Ext.getCmp("content-panel").getActiveTab();
  1023. if(!tab && parent.parent.Ext.getCmp("content-panel"))
  1024. tab = parent.parent.Ext.getCmp("content-panel").getActiveTab();
  1025. if(!tab){
  1026. var win = parent.Ext.ComponentQuery.query('window');
  1027. if(win.length > 0){
  1028. tab = win[win.length-1];
  1029. }
  1030. }
  1031. return tab;
  1032. },
  1033. getGridSelected:function(grid){
  1034. var items = grid.selModel.getSelection(),data = new Array();
  1035. Ext.each(items, function(item, index){
  1036. if(this.data[grid.keyField] != null && this.data[grid.keyField] != ''
  1037. && this.data[grid.keyField] != '0' && this.data[grid.keyField] != 0){
  1038. var o = new Object();
  1039. if(grid.keyField){
  1040. o[grid.keyField] = item.data[grid.keyField];
  1041. }
  1042. if(grid.toField){
  1043. Ext.each(grid.toField, function(f, index){
  1044. var v = Ext.getCmp(f).value;
  1045. if(v != null && v.toString().trim() != '' && v.toString().trim() != 'null'){
  1046. if(Ext.isDate(v)){
  1047. v = Ext.Date.toString(v);
  1048. }
  1049. o[f] = v;
  1050. } else {
  1051. o[f] = '';
  1052. }
  1053. });
  1054. }
  1055. if(grid.necessaryFields){
  1056. Ext.each(grid.necessaryFields, function(f, index){
  1057. var v = item.data[f];
  1058. if(Ext.isDate(v)){
  1059. v = Ext.Date.toString(v);
  1060. }
  1061. if(Ext.isNumber(v)){
  1062. v = (v).toString();
  1063. }
  1064. o[f] = v;
  1065. });
  1066. }
  1067. data.push(o);
  1068. }
  1069. });
  1070. return data;
  1071. }
  1072. });