GridPanel.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. Ext.require([
  2. 'erp.util.*'
  3. ]);
  4. Ext.define('erp.view.common.datalist.GridPanel',{
  5. extend: 'Ext.grid.Panel',
  6. requires: ['erp.view.core.plugin.CopyPasteMenu'],
  7. alias: 'widget.erpDatalistGridPanel',
  8. layout : 'fit',
  9. id: 'grid',
  10. emptyText : '无数据',
  11. columnLines : true,
  12. autoScroll : true,
  13. store: [],
  14. columns: new Array(),
  15. bodyStyle:'background-color:#f1f1f1;',
  16. selModel: Ext.create('Ext.selection.CheckboxModel',{
  17. headerWidth: 0
  18. }),
  19. dockedItems: [{
  20. id : 'pagingtoolbar',
  21. xtype: 'erpDatalistToolbar',
  22. dock: 'bottom',
  23. displayInfo: true
  24. }],
  25. plugins: [Ext.create('Ext.ux.grid.GridHeaderFilters'), Ext.create('erp.view.core.plugin.CopyPasteMenu')],
  26. BaseUtil: Ext.create('erp.util.BaseUtil'),
  27. GridUtil: Ext.create('erp.util.GridUtil'),
  28. RenderUtil:Ext.create('erp.util.RenderUtil'),
  29. autoQuery: true,
  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. if(this.autoQuery) {
  37. this.getCount(caller, condition);
  38. } else {
  39. //易方数据量大 可能明细显示的是datalist
  40. var gridCondition=this.BaseUtil.getUrlParam('gridCondition');
  41. if(gridCondition){
  42. condition=gridCondition.replace(/IS/g, "=");
  43. this.getCount(caller,condition);
  44. }
  45. else this.getCount(caller, '1=2');
  46. }
  47. this.callParent(arguments);
  48. this.addEvents({
  49. keydown: true
  50. });
  51. } ,
  52. _noc: 0,
  53. _f: 0,
  54. getColumnsAndStore: function(c, d, g, s){
  55. c = c || caller;
  56. d = d || this.getCondition();
  57. g = g || page;
  58. s = s || pageSize;
  59. var me = this;
  60. var f = d;
  61. this.setLoading(true);//loading...
  62. Ext.Ajax.request({//拿到grid的columns
  63. url : basePath + 'common/datalist.action',
  64. params: {
  65. _noc: (me._noc || getUrlParam('_noc')),
  66. _f: (me._f || getUrlParam('_f')),
  67. caller: c,
  68. condition: f,
  69. page: g,
  70. pageSize: s,
  71. search: me.searchGrid ? 1 : 0// 使用高级查询时,方法有变化
  72. },
  73. method : 'post',
  74. callback : function(options,success,response){
  75. me.setLoading(false);
  76. if (!response) return;
  77. var res = new Ext.decode(response.responseText);
  78. if(res.exception || res.exceptionInfo){
  79. showError(res.exceptionInfo);
  80. return;
  81. }
  82. var data = res.data != null ? Ext.decode(res.data.replace(/,}/g, '}').replace(/,]/g, ']')) : [];//一定要去掉多余逗号,ie对此很敏感
  83. if(me.columns && me.columns.length > 2){
  84. me.store.loadData(data);
  85. if(me.lastSelected && me.lastSelected.length > 0){//grid刷新后,仍然选中上次选中的record
  86. Ext.each(me.store.data.items, function(item){
  87. if(item.data[keyField] == me.lastSelected[0].data[keyField]){
  88. me.selModel.select(item);
  89. }
  90. });
  91. }
  92. //修改pagingtoolbar信息
  93. Ext.getCmp('pagingtoolbar').afterOnLoad();
  94. } else {
  95. if(!Ext.isChrome){
  96. Ext.each(res.fields, function(f){
  97. if(f.type == 'date'){
  98. f.dateFormat = 'Y-m-d H:m:s';
  99. }
  100. });
  101. }
  102. var store = Ext.create('Ext.data.Store', {
  103. fields: res.fields,
  104. data: data,
  105. //模糊查询的结果在Ext.Array.filter()方法之后,部分数据被过滤掉,设置为false不调用该方法
  106. //yingp
  107. filterOnLoad: false
  108. });
  109. //处理render
  110. var grid = this;
  111. Ext.Array.each(res.columns, function(column, y) {
  112. if(!column.haveRendered && column.renderer != null && column.renderer != ""){
  113. if(!grid.RenderUtil){
  114. grid.RenderUtil = Ext.create('erp.util.RenderUtil');
  115. }
  116. var renderName = column.renderer;
  117. if(contains(column.renderer, ':', true)){
  118. var args = new Array();
  119. Ext.each(column.renderer.split(':'), function(a, index){
  120. if(index == 0){
  121. renderName = a;
  122. } else {
  123. args.push(a);
  124. }
  125. });
  126. if(!grid.RenderUtil.args[renderName]){
  127. grid.RenderUtil.args[renderName] = new Object();
  128. }
  129. grid.RenderUtil.args[renderName][column.dataIndex] = args;
  130. //这里只能用column.dataIndex来标志,不能用x,y,index等,
  131. //grid在render时,checkbox占一列
  132. }
  133. column.renderer = grid.RenderUtil[renderName];
  134. column.haveRendered = true;
  135. }
  136. });
  137. var col = Ext.Array.insert(res.columns, 0, [{xtype: 'rownumberer', width: 35, cls: 'x-grid-header-1', align: 'center'}]);
  138. me.reconfigure(store, col);//用这个方法每次都会add一个checkbox列
  139. }
  140. //修改pagingtoolbar信息
  141. Ext.getCmp('pagingtoolbar').afterOnLoad();
  142. //拿到datalist对应的单表的关键词
  143. keyField = res.keyField;//form表主键字段
  144. pfField = res.pfField;//grid表主键字段
  145. url = basePath + res.url;//grid行选择之后iframe嵌入的页面链接
  146. relative = res.relative;
  147. if(res.vastbutton && res.vastbutton == 'erpAddButton'){//[新增]功能
  148. Ext.getCmp('erpAddButton').show();
  149. }
  150. }
  151. });
  152. },
  153. getCount: function(c, d, m){
  154. c = c || caller;
  155. d = d || this.getCondition();
  156. var me = this;
  157. var f = d;
  158. if(!me.filterCondition && !me.searchGrid && !m) {// 大数据如果没有筛选条件
  159. var _f = me._f || getUrlParam('_f');
  160. if(_f == 1) {
  161. dataCount = 1000*pageSize;// 直接作1000页数据处理
  162. me.noCount = true;
  163. me.getColumnsAndStore(c, d);
  164. return;
  165. }
  166. }
  167. Ext.Ajax.request({//拿到grid的数据总数count
  168. url : basePath + '/common/datalistCount.action',
  169. params: {
  170. caller: c,
  171. condition: f,
  172. search: me.searchGrid ? 1 : 0// 使用高级查询时,count的方法有变化
  173. },
  174. method : 'post',
  175. callback : function(options,success,response){
  176. var res = new Ext.decode(response.responseText);
  177. if(res.exception || res.exceptionInfo){
  178. showError(res.exceptionInfo);
  179. return;
  180. }
  181. dataCount = res.count;
  182. me.getColumnsAndStore(c, d);
  183. }
  184. });
  185. },
  186. listeners: {
  187. 'headerfiltersapply': function(grid, filters) {
  188. if(this.allowFilter){
  189. var condition = null;
  190. for(var fn in filters){
  191. var value = filters[fn],f = grid.getHeaderFilterField(fn);
  192. if(!Ext.isEmpty(value)){
  193. if("null"!=value){
  194. if(f.filtertype) {
  195. if (f.filtertype == 'numberfield') {
  196. value = fn + "=" + value + " ";
  197. }
  198. } else {
  199. if(Ext.isDate(value)){
  200. value = Ext.Date.toString(value);
  201. value = "to_char(" + fn + ",'yyyy-MM-dd')='" + value + "' ";
  202. } else {
  203. var exp_t = /^(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/,
  204. exp_d = /^(\d{4})\-(\d{2})\-(\d{2})$/;
  205. if(exp_d.test(value)){
  206. value = "to_char(" + fn + ",'yyyy-MM-dd')='" + value + "' ";
  207. } else if(exp_t.test(value)){
  208. value = "to_char(" + fn + ",'yyyy-MM-dd')='" + value.substr(0, 10) + "' ";
  209. } else{
  210. if (f.xtype == 'combo' || f.xtype == 'combofield') {
  211. if (value == '-所有-') {
  212. continue;
  213. } else if (value == '-无-') {
  214. value = 'nvl(' + fn + ',\' \')=\' \'';
  215. } else {
  216. value = fn + " LIKE '" + value + "%' ";
  217. }
  218. } else {
  219. /**字符串转换下简体*/
  220. var SimplizedValue=this.BaseUtil.Simplized(value);
  221. //可能就是按繁体筛选
  222. if(f.ignoreCase) {// 忽略大小写
  223. fn = 'upper(' + fn + ')';
  224. value = value.toUpperCase();
  225. }
  226. if(!f.autoDim) {
  227. if(SimplizedValue!=value){
  228. value = "("+fn + " LIKE '" + value + "%' or "+fn+" LIKE '"+SimplizedValue+"%')";
  229. }else value = fn + " LIKE '" + value + "%' ";
  230. } else if(f.exactSearch){
  231. value=fn+"='"+value+"'";
  232. }else {
  233. if(SimplizedValue!=value){
  234. value = "("+fn + " LIKE '%" + value + "%' or "+fn+" LIKE '%"+SimplizedValue+"%')";
  235. }else value = fn + " LIKE '%" + value + "%' ";
  236. }
  237. }
  238. }
  239. }
  240. }
  241. }else value ="nvl("+fn+",' ')=' '";
  242. if(condition == null){
  243. condition = value;
  244. } else {
  245. condition = condition + " AND " + value;
  246. }
  247. }
  248. }
  249. this.filterCondition = condition;
  250. page = 1;
  251. this.getCount();
  252. } else {
  253. this.allowFilter = true;
  254. }
  255. return false;
  256. }
  257. },
  258. reconfigure: function(store, columns){
  259. //改写reconfigure方法
  260. var d = this.headerCt;
  261. if (this.columns.length <= 1 && columns) {//this.columns.length > 1表示grid的columns已存在,没必要remove再add
  262. d.suspendLayout = true;
  263. d.removeAll();
  264. d.add(columns);
  265. }
  266. if (store) {
  267. this.bindStore(store);
  268. } else {
  269. this.getView().refresh();
  270. }
  271. if (columns) {
  272. d.suspendLayout = false;
  273. this.forceComponentLayout();
  274. }
  275. this.fireEvent("reconfigure", this);
  276. },
  277. getCondition: function(){
  278. var condition = '';
  279. if(!Ext.isEmpty(this.defaultCondition)) {
  280. condition = this.defaultCondition;
  281. }
  282. if (this.searchGrid) {
  283. var s = this.searchGrid.getCondition();
  284. if(s != null && s.length > 0) {
  285. if(condition.length > 0)
  286. condition += ' AND (' + s + ')';
  287. else
  288. condition = s;
  289. }
  290. }
  291. if(this.formCondition) {
  292. if(condition.length > 0)
  293. condition += ' AND (' + this.formCondition + ')';
  294. else
  295. condition = this.formCondition;
  296. }
  297. if(!Ext.isEmpty(this.filterCondition)) {
  298. if(condition == '') {
  299. condition = this.filterCondition;
  300. } else {
  301. condition = '(' + condition + ') AND (' + this.filterCondition + ')';
  302. }
  303. }
  304. return condition;// .replace(/=/g, '%3D')
  305. }
  306. });