GridPanel.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. Ext.require([
  2. 'erp.util.*'
  3. ]);
  4. Ext.define('erp.view.common.editDatalist.GridPanel',{
  5. extend: 'Ext.grid.Panel',
  6. alias: 'widget.erpEditDatalistGridPanel',
  7. layout : 'fit',
  8. id: 'grid',
  9. emptyText : '无数据',
  10. columnLines : true,
  11. autoScroll : true,
  12. store: [],
  13. columns: new Array(),
  14. bodyStyle:'background-color:#f1f1f1;',
  15. selModel: Ext.create('Ext.selection.CheckboxModel',{
  16. headerWidth: 0
  17. }),
  18. dockedItems: [{
  19. id : 'pagingtoolbar',
  20. xtype: 'erpEditDatalistToolbar',
  21. dock: 'bottom',
  22. displayInfo: true
  23. }],
  24. plugins: [Ext.create('Ext.ux.grid.GridHeaderFilters'), Ext.create('Ext.grid.plugin.CellEditing', {
  25. clicksToEdit: 1
  26. }), Ext.create('erp.view.core.plugin.CopyPasteMenu')],
  27. BaseUtil: Ext.create('erp.util.BaseUtil'),
  28. GridUtil: Ext.create('erp.util.GridUtil'),
  29. RenderUtil:Ext.create('erp.util.RenderUtil'),
  30. initComponent : function(){
  31. condition = this.BaseUtil.getUrlParam('urlcondition');
  32. condition = (condition == null) ? "" : condition;
  33. condition = condition.replace(/@/,"'%").replace(/@/,"%'");
  34. this.defaultCondition = condition;//固定条件;从url里面获取
  35. caller = this.caller || this.BaseUtil.getUrlParam('whoami');
  36. this.getCount(caller, condition);
  37. this.callParent(arguments);
  38. this.addEvents({
  39. keydown: true
  40. });
  41. } ,
  42. getColumnsAndStore: function(c, d, g, s){
  43. c = c || caller;
  44. d = d || this.getCondition();
  45. g = g || page;
  46. s = s || pageSize;
  47. var me = this;
  48. var f = d;
  49. /*if(me.filterCondition){
  50. if(d == null || d == ''){
  51. f = me.filterCondition;
  52. } else {
  53. f += ' AND ' + me.filterCondition;
  54. }
  55. }*/
  56. this.setLoading(true);//loading...
  57. Ext.Ajax.request({//拿到grid的columns
  58. url : basePath + 'common/datalist.action',
  59. params: {
  60. caller: c,
  61. condition: f,
  62. page: g,
  63. pageSize: s
  64. },
  65. method : 'post',
  66. callback : function(options,success,response){
  67. me.setLoading(false);
  68. var res = new Ext.decode(response.responseText);
  69. if(res.exception || res.exceptionInfo){
  70. showError(res.exceptionInfo);
  71. return;
  72. }
  73. var data = res.data != null ? Ext.decode(res.data.replace(/,}/g, '}').replace(/,]/g, ']')) : [];//一定要去掉多余逗号,ie对此很敏感
  74. if(me.columns && me.columns.length > 2){
  75. me.store.loadData(data);
  76. if(me.lastSelected && me.lastSelected.length > 0){//grid刷新后,仍然选中上次选中的record
  77. Ext.each(me.store.data.items, function(item){
  78. if(item.data[keyField] == me.lastSelected[0].data[keyField]){
  79. me.selModel.select(item);
  80. }
  81. });
  82. }
  83. //修改pagingtoolbar信息
  84. Ext.getCmp('pagingtoolbar').afterOnLoad(page);
  85. } else {
  86. if(!Ext.isChrome){
  87. Ext.each(res.fields, function(f){
  88. if(f.type == 'date'){
  89. f.dateFormat = 'Y-m-d H:m:s';
  90. }
  91. });
  92. }
  93. var store = Ext.create('Ext.data.Store', {
  94. fields: res.fields,
  95. data: data,
  96. //模糊查询的结果在Ext.Array.filter()方法之后,部分数据被过滤掉,设置为false不调用该方法
  97. //yingp
  98. filterOnLoad: false
  99. });
  100. //处理render
  101. var grid = this;
  102. Ext.Array.each(res.columns, function(column, y) {
  103. if(!column.haveRendered && column.renderer != null && column.renderer != ""){
  104. if(!grid.RenderUtil){
  105. grid.RenderUtil = Ext.create('erp.util.RenderUtil');
  106. }
  107. var renderName = column.renderer;
  108. if(contains(column.renderer, ':', true)){
  109. var args = new Array();
  110. Ext.each(column.renderer.split(':'), function(a, index){
  111. if(index == 0){
  112. renderName = a;
  113. } else {
  114. args.push(a);
  115. }
  116. });
  117. if(!grid.RenderUtil.args[renderName]){
  118. grid.RenderUtil.args[renderName] = new Object();
  119. }
  120. grid.RenderUtil.args[renderName][column.dataIndex] = args;
  121. //这里只能用column.dataIndex来标志,不能用x,y,index等,
  122. //grid在render时,checkbox占一列
  123. }
  124. column.renderer = grid.RenderUtil[renderName];
  125. column.haveRendered = true;
  126. }
  127. });
  128. me.reconfigure(store, res.columns);//用这个方法每次都会add一个checkbox列
  129. me.getButtons();
  130. //修改pagingtoolbar信息
  131. Ext.getCmp('pagingtoolbar').afterOnLoad();
  132. }
  133. //拿到datalist对应的单表的关键词
  134. keyField = res.keyField;//form表主键字段
  135. pfField = res.pfField;//grid表主键字段
  136. url = basePath + res.url;//grid行选择之后iframe嵌入的页面链接
  137. relative = res.relative;
  138. }
  139. });
  140. },
  141. getCount: function(c, d){
  142. c = c || caller;
  143. d = d || this.getCondition();
  144. var me = this;
  145. var f = d;
  146. /*if(me.filterCondition){
  147. if(d == null || d == ''){
  148. f = me.filterCondition;
  149. } else {
  150. f += ' AND ' + me.filterCondition;
  151. }
  152. }*/
  153. Ext.Ajax.request({//拿到grid的数据总数count
  154. url : basePath + '/common/datalistCount.action',
  155. params: {
  156. caller: c,
  157. condition: f
  158. },
  159. method : 'post',
  160. callback : function(options,success,response){
  161. var res = new Ext.decode(response.responseText);
  162. if(res.exception || res.exceptionInfo){
  163. showError(res.exceptionInfo);
  164. return;
  165. }
  166. dataCount = res.count;
  167. me.getColumnsAndStore(c);
  168. }
  169. });
  170. },
  171. listeners: {
  172. 'itemclick': function(view,record){
  173. this.lastSelectRecord = record;
  174. },
  175. 'headerfiltersapply': function(grid, filters) {
  176. if(this.allowFilter){
  177. var condition = null;
  178. for(var fn in filters){
  179. var value = filters[fn],f = grid.getHeaderFilterField(fn);
  180. if(!Ext.isEmpty(value)){
  181. if(f.filtertype) {
  182. if (f.filtertype == 'numberfield') {
  183. value = fn + "=" + value + " ";
  184. }
  185. } else if(f.xtype == 'combo') {
  186. value = fn + " = '" + value + "' ";
  187. } else {
  188. if(Ext.isDate(value)){
  189. value = Ext.Date.toString(value);
  190. value = fn + "=to_date('" + value + "','yyyy-MM-dd') ";
  191. } else {
  192. var exp_t = /^(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/,
  193. exp_d = /^(\d{4})\-(\d{2})\-(\d{2})$/;
  194. if(exp_d.test(value)){
  195. value = fn + "=to_date('" + value + "','yyyy-MM-dd') ";
  196. } else if(exp_t.test(value)){
  197. value = fn + "=to_date('" + value + "','yyyy-MM-dd HH24:mi:ss') ";
  198. } else{
  199. value = fn + " LIKE '%" + value + "%' ";
  200. }
  201. }
  202. }
  203. if(condition == null){
  204. condition = value;
  205. } else {
  206. condition = condition + " AND " + value;
  207. }
  208. }
  209. }
  210. this.filterCondition = condition;
  211. page = 1;
  212. this.getCount();
  213. } else {
  214. this.allowFilter = true;
  215. }
  216. return false;
  217. },
  218. viewready: function (grid) {// tip显示单元格完整信息
  219. var view = grid.view;
  220. grid.mon(view, {
  221. uievent: function (type, view, cell, recordIndex, cellIndex, e) {
  222. grid.cellIndex = cellIndex;
  223. grid.recordIndex = recordIndex;
  224. }
  225. });
  226. grid.tip = Ext.create('Ext.tip.ToolTip', {
  227. target: view.el,
  228. delegate: '.x-grid-cell',
  229. trackMouse: true,
  230. renderTo: Ext.getBody(),
  231. listeners: {
  232. beforeshow: function updateTipBody(tip) {
  233. if (!Ext.isEmpty(grid.cellIndex) && grid.cellIndex !== -1) {
  234. header = grid.headerCt.getGridColumns()[grid.cellIndex];
  235. tip.update(grid.getStore().getAt(grid.recordIndex).get(header.dataIndex));
  236. }
  237. }
  238. }
  239. });
  240. }
  241. },
  242. reconfigure: function(store, columns){
  243. //改写reconfigure方法
  244. var d = this.headerCt;
  245. if (this.columns.length <= 1 && columns) {//this.columns.length > 1表示grid的columns已存在,没必要remove再add
  246. d.suspendLayout = true;
  247. d.removeAll();
  248. d.add(columns);
  249. }
  250. if (store) {
  251. this.bindStore(store);
  252. } else {
  253. this.getView().refresh();
  254. }
  255. if (columns) {
  256. d.suspendLayout = false;
  257. this.forceComponentLayout();
  258. }
  259. this.fireEvent("reconfigure", this);
  260. },
  261. getCondition: function(){
  262. var condition = '';
  263. if(!Ext.isEmpty(this.defaultCondition)) {
  264. condition = this.defaultCondition;
  265. }
  266. if(!Ext.isEmpty(this.filterCondition)) {
  267. if(condition == '') {
  268. condition = this.filterCondition;
  269. } else {
  270. condition = '(' + condition + ') AND (' + this.filterCondition + ')';
  271. }
  272. }
  273. /*return condition.replace(/=/g, '%3D');*/
  274. return condition;// .replace(/=/g, '%3D')
  275. },
  276. /**
  277. * buttons由原先在datalist里面配置改为在GridButton表配置;
  278. * 支持多个button;
  279. * 支持传递actionName
  280. */
  281. getButtons: function(){
  282. Ext.Ajax.request({
  283. url : basePath + "common/gridButton.action",
  284. params: {
  285. caller: caller
  286. },
  287. method : 'post',
  288. callback : function(options,success,response){
  289. var localJson = new Ext.decode(response.responseText);
  290. if(localJson.exceptionInfo){
  291. showError(localJson.exceptionInfo);
  292. }
  293. if(localJson.buttons){
  294. var buttons = Ext.decode(localJson.buttons);
  295. Ext.each(buttons, function(b){
  296. var btn = Ext.ComponentQuery.query(b.xtype);
  297. if(btn.length > 0){
  298. btn[0].url = b.url;
  299. btn[0].show();
  300. }
  301. });
  302. }
  303. }
  304. });
  305. },
  306. getEffectData: function(){
  307. var grid = this;
  308. var fields = new Array();
  309. fields.push({name: keyField, type: 'number'});
  310. Ext.each(grid.columns, function(c, index){
  311. if (c.editor != null || (c.getEditor && c.getEditor() != null)) {
  312. var type = 'string';
  313. if(c.xtype == 'datecolumn'){
  314. type = 'date';
  315. } else if(c.xtype == 'datetimecolumn'){
  316. type = 'datetime';
  317. }
  318. fields.push({
  319. name: c.dataIndex,
  320. type: type
  321. });
  322. }
  323. });
  324. var data = new Array();
  325. var o = null;
  326. grid.store.each(function(record){
  327. if(record.dirty) {
  328. o = new Object();
  329. Ext.each(fields, function(f){
  330. var v = record.data[f.name];
  331. if(f.type == 'date'){
  332. if (Ext.isDate(v)) {
  333. v = Ext.Date.format(v, 'Y-m-d');
  334. }
  335. } else if(f.type == 'datetime'){
  336. if (Ext.isDate(v)) {
  337. v = Ext.Date.format(v, 'Y-m-d H:i:s');
  338. }
  339. }
  340. o[f.name] = v;
  341. });
  342. data.push(o);
  343. }
  344. });
  345. return data;
  346. }
  347. });