GridPanel.js 10 KB

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