GridPanel.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. Ext.define('erp.view.common.dbfind.GridPanel',{
  2. extend: 'Ext.grid.Panel',
  3. alias: 'widget.erpDbfindGridPanel',
  4. layout : 'fit',
  5. id: 'dbfindGridPanel',
  6. emptyText : $I18N.common.grid.emptyText,
  7. columnLines : true,
  8. autoScroll : true,
  9. store: [],
  10. columns: [],
  11. selModel: Ext.create('Ext.selection.CheckboxModel',{
  12. headerWidth: 0
  13. }),
  14. BaseUtil: Ext.create('erp.util.BaseUtil'),
  15. bodyStyle:'background-color:#f1f1f1;',
  16. dockedItems: [{
  17. id : 'pagingtoolbar',
  18. xtype: 'erpDbfindToolbar',
  19. dock: 'bottom',
  20. displayInfo: true
  21. }],
  22. plugins: [Ext.create('Ext.ux.grid.GridHeaderFilters')],
  23. initComponent : function(){
  24. this.callParent(arguments);
  25. this.getCount();
  26. },
  27. getColumnsAndStore: function(){
  28. var me = this;
  29. var c = condition;
  30. if(me.filterCondition){
  31. c += ' AND ' + me.filterCondition;
  32. }
  33. me.setLoading(true);
  34. Ext.Ajax.request({//拿到grid的columns
  35. url : basePath + 'common/dbfind.action',
  36. method : 'post',
  37. params : {
  38. which : which,
  39. caller : caller,
  40. field: key,
  41. condition: c,
  42. ob: dbOrderby,// dynamic order by
  43. page: page,
  44. pageSize: pageSize
  45. },
  46. callback : function(options,success,response){
  47. me.setLoading(false);
  48. var res = new Ext.decode(response.responseText);
  49. if(res.exceptionInfo){
  50. showError(res.exceptionInfo);return;
  51. }
  52. if(res.autoHeight) {
  53. me.addCls('custom-grid-autoheight');//自适应样式
  54. }
  55. var data = Ext.decode(res.data.replace(/,}/g, '}').replace(/,]/g, ']'));
  56. if(me.columns && me.columns.length > 2){
  57. me.store.loadData(data);
  58. if(me.store.data.items.length != data.length){
  59. me.store.removeAll();
  60. me.store.add(data);
  61. }
  62. //修改pagingtoolbar信息
  63. Ext.getCmp('pagingtoolbar').afterOnLoad();
  64. } else {
  65. me.reconfigure(Ext.create('Ext.data.Store', {
  66. storeId: 'dbfindGridPanelStore',
  67. fields: res.fields,
  68. data: data
  69. }), res.columns);
  70. //修改pagingtoolbar信息
  71. Ext.getCmp('pagingtoolbar').afterOnLoad();
  72. dbfinds = res.dbfinds;
  73. //通用变更单dbfind 采用变更原有单据的放大镜
  74. if(trigger.isCommonChange){
  75. Ext.Array.each(dbfinds,function(d){
  76. d.field=d.field+'-new';
  77. });
  78. }
  79. me.resetable = res.reset;//允许重置条件
  80. trigger.dbfinds = dbfinds;
  81. if(me.resetable){
  82. var win = parent.Ext.getCmp('dbwin');
  83. win && win.down('#reset').show();
  84. }
  85. }
  86. }
  87. });
  88. },
  89. getCount: function(){
  90. var me = this;
  91. var c = condition;
  92. if(me.filterCondition){
  93. c += ' AND ' + me.filterCondition;
  94. }
  95. Ext.Ajax.request({//拿到grid的数据总数count
  96. url : basePath + 'common/dbfindCount.action',
  97. params : {
  98. which : which,
  99. caller : caller,
  100. field: key,
  101. condition: c
  102. },
  103. method : 'post',
  104. callback : function(options,success,response){
  105. var res = new Ext.decode(response.responseText);
  106. dataCount = res.count;
  107. me.getColumnsAndStore();
  108. }
  109. });
  110. },
  111. listeners: {
  112. 'headerfiltersapply': function(grid, filters) {
  113. if(this.allowFilter){
  114. var condition = null;
  115. for(var fn in filters){
  116. var value = filters[fn], f = grid.getHeaderFilterField(fn);
  117. if(f.xtype == 'datefield')
  118. value = f.getRawValue();
  119. if(!Ext.isEmpty(value)) {
  120. if("null"!=value){
  121. if(f.filtertype) {
  122. if (f.filtertype == 'numberfield') {
  123. value = fn + "=" + value + " ";
  124. }
  125. } else {
  126. if(Ext.isDate(value)){
  127. value = Ext.Date.toString(value);
  128. value = "to_char(" + fn + ",'yyyy-MM-dd')='" + value + "' ";
  129. } else {
  130. var exp_t = /^(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/,
  131. exp_d = /^(\d{4})\-(\d{2})\-(\d{2})$/;
  132. if(exp_d.test(value)){
  133. value = "to_char(" + fn + ",'yyyy-MM-dd')='" + value + "' ";
  134. } else if(exp_t.test(value)){
  135. value = "to_char(" + fn + ",'yyyy-MM-dd')='" + value.substr(0, 10) + "' ";
  136. } else{
  137. if (f.xtype == 'combo' || f.xtype == 'combofield') {
  138. if (value == '-所有-') {
  139. continue;
  140. } else if (value == '-无-') {
  141. value = 'nvl(' + fn + ',\' \')=\' \'';
  142. } else {
  143. value = fn + " LIKE '" + value + "%' ";
  144. }
  145. } else if(f.xtype == 'datefield') {
  146. value = "to_char(" + fn + ",'yyyy-MM-dd') like '%" + value + "%' ";
  147. } else if(f.column && f.column.xtype == 'numbercolumn'){
  148. if(f.column.format) {
  149. var precision = f.column.format.substr(f.column.format.indexOf('.') + 1).length;
  150. value = "to_char(round(" + fn + "," + precision + ")) like '%" + value + "%' ";
  151. } else
  152. value = "to_char(" + fn + ") like '%" + value + "%' ";
  153. } else {
  154. /**字符串转换下简体*/
  155. var SimplizedValue=this.BaseUtil.Simplized(value);
  156. //可能就是按繁体筛选
  157. if(f.ignoreCase) {// 忽略大小写
  158. fn = 'upper(' + fn + ')';
  159. value = value.toUpperCase();
  160. }
  161. if(!f.autoDim) {
  162. if(SimplizedValue!=value){
  163. value = "("+fn + " LIKE '" + value + "%' or "+fn+" LIKE '"+SimplizedValue+"%')";
  164. }else value = fn + " LIKE '" + value + "%' ";
  165. } else if(f.exactSearch){
  166. value=fn+"='"+value+"'";
  167. } else {
  168. if(SimplizedValue!=value){
  169. value = "("+fn + " LIKE '%" + value + "%' or "+fn+" LIKE '%"+SimplizedValue+"%')";
  170. }else value = fn + " LIKE '%" + value + "%' ";
  171. }
  172. }
  173. }
  174. }
  175. }
  176. }else value ="nvl("+fn+",' ')=' '";
  177. if(condition == null){
  178. condition = value;
  179. } else {
  180. condition = condition + " AND " + value;
  181. }
  182. }
  183. }
  184. this.filterCondition = condition;
  185. page = 1;
  186. this.getCount();
  187. } else {
  188. this.allowFilter = true;
  189. }
  190. return false;
  191. }
  192. },
  193. reconfigure: function(store, columns){
  194. //改写reconfigure方法
  195. var d = this.headerCt;
  196. if (this.columns.length <= 1 && columns) {//this.columns.length > 1表示grid的columns已存在,没必要remove再add
  197. if(!Ext.isChrome){//ie,firefox下,format出现NaN-NaN-NaN,暂时作string处理
  198. Ext.each(columns, function(c){
  199. if(c.xtype == 'datecolumn'){
  200. c.xtype = "";
  201. c.format = "";
  202. }
  203. });
  204. }
  205. d.suspendLayout = true;
  206. d.removeAll();
  207. d.add(columns);
  208. }
  209. if (store) {
  210. if(!Ext.isChrome){//ie,firefox下,format出现NaN-NaN-NaN
  211. Ext.each(store.fields, function(f){
  212. if(f.type == 'date'){
  213. f.type = "string";
  214. f.format = "";
  215. }
  216. });
  217. }
  218. this.bindStore(store);
  219. } else {
  220. this.getView().refresh();
  221. }
  222. if (columns) {
  223. d.suspendLayout = false;
  224. this.forceComponentLayout();
  225. }
  226. this.fireEvent("reconfigure", this);
  227. },
  228. /**
  229. * 重置条件
  230. * dbfindSet.ds_allowreset(1-允许)
  231. * dbfindSetUI.ds_allowreset(1-允许)
  232. */
  233. resetCondition: function(){
  234. condition = this.getCondition();
  235. this.filterCondition = '';
  236. var fields = this.plugins[0].fields;
  237. Ext.each(Ext.Object.getKeys(fields), function(key){
  238. fields[key].reset();
  239. });
  240. },
  241. getCondition: function() {
  242. condition = 'upper(' + key + ") like '%%'";
  243. if(!trigger.ownerCt || trigger.column){//如果是grid的dbfind
  244. condition = 'upper(' + keyField + ") like '%%'";
  245. if(dbGridCondition && dbGridCondition != null){
  246. condition += " AND " + decodeURIComponent(dbGridCondition).replace(/\s{1}IS\s{1}/g, '=');
  247. }
  248. }
  249. if(dbCondition && dbCondition != null){
  250. condition += " AND " + decodeURIComponent(dbCondition).replace(/\s{1}IS\s{1}/g, '=');
  251. }
  252. if(dbBaseCondition && dbBaseCondition != null){
  253. condition += " AND " + decodeURIComponent(dbBaseCondition).replace(/\s{1}IS\s{1}/g, '=').replace(/@/g,'%');
  254. }
  255. return condition;
  256. }
  257. });