KnowledgeSearchGrid.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. Ext.require([
  2. 'erp.util.*'
  3. ]);
  4. Ext.define('erp.view.oa.knowledge.KnowledgeSearchGrid',{
  5. extend: 'Ext.grid.Panel',
  6. alias: 'widget.erpSearchGridPanel',
  7. layout : 'auto',
  8. id: 'grid',
  9. emptyText : '无数据',
  10. columnLines : true,
  11. autoScroll : true,
  12. store: [],
  13. columns: new Array(),
  14. searchValue: null,
  15. /**
  16. * @private
  17. * The row indexes where matching strings are found. (used by previous and next buttons)
  18. */
  19. indexes: [],
  20. /**
  21. * @private
  22. * The row index of the first search, it could change if next or previous buttons are used.
  23. */
  24. currentIndex: null,
  25. /**
  26. * @private
  27. * The generated regular expression used for searching.
  28. */
  29. searchRegExp: null,
  30. /**
  31. * @private
  32. * Case sensitive mode.
  33. */
  34. caseSensitive: false,
  35. /**
  36. * @private
  37. * Regular expression mode.
  38. */
  39. regExpMode: false,
  40. /**
  41. * @cfg {String} matchCls
  42. * The matched string css classe.
  43. */
  44. matchCls: 'x-livesearch-match',
  45. defaultStatusText: 'Nothing Found',
  46. bodyStyle:'background-color:#f1f1f1;',
  47. selModel: Ext.create('Ext.selection.CheckboxModel',{
  48. headerWidth: 0
  49. }),
  50. dockedItems: [{
  51. xtype: 'toolbar',
  52. dock: 'top',
  53. style:'background:#CDCDB4;height:45px;font-size:16px;',
  54. bodyStyle: 'background:#CDCDB4; padding:0px;font-size:16px;',
  55. items: [{
  56. xtype: 'textfield',
  57. fieldLabel:'<h1>全文检索:</h1>',
  58. labelSeparator:"",
  59. width:300,
  60. name:'search',
  61. id:'search',
  62. labelStyle:'font-size:16px',
  63. fieldStyle : 'background:#DEDEDE;width:200px;font-size:16px;height:22px',
  64. }, {
  65. xtype: 'button',
  66. margin: '0 0 0 4px',
  67. text: '<',
  68. id:'prev',
  69. tooltip: '上一行',
  70. },{
  71. xtype: 'button',
  72. margin: '0 0 0 4px',
  73. id:'next',
  74. text: '>',
  75. tooltip: '下一行',
  76. }, '-', {
  77. xtype: 'checkbox',
  78. margin: '0 0 0 4px',
  79. id:'regular',
  80. hideLabel:true,
  81. },'<span style="font-size:15px;">正规表达式</span>', {
  82. xtype: 'checkbox',
  83. hideLabel: true,
  84. id:'case',
  85. margin: '0 0 0 4px',
  86. }, '<span style="font-size:14px;">区分大小写</span>','->',
  87. {
  88. xtype: 'textfield',
  89. fieldLabel:'<h1>匹配数:</h1>',
  90. labelAlign:'right',
  91. id:'matchs',
  92. emptyText:'未找到匹配值',
  93. labelSeparator:"",
  94. readOnly:true,
  95. fieldStyle : 'background:#CDCDB4 ;border-bottom-style:1px solid;padding:2px 2px;vertical-align:middle;border-top:none;border-right:none;color:#CD661D;border-bottom-style:1px solid;border-left:none; ',
  96. },'-',{
  97. xtype:'button',
  98. id:'previous',
  99. iconCls:'prev',
  100. disabled:true,
  101. },{
  102. xtype:'button',
  103. id:'nextone',
  104. iconCls:'next',
  105. disabled:true,
  106. style:'margin-right:20px;margin-left:5px;'
  107. }],
  108. },{
  109. id : 'pagingtoolbar',
  110. xtype: 'erpDatalistToolbar',
  111. dock: 'bottom',
  112. displayInfo: true
  113. }],
  114. plugins: [Ext.create('Ext.ux.grid.GridHeaderFilters')],
  115. BaseUtil: Ext.create('erp.util.BaseUtil'),
  116. RenderUtil:Ext.create('erp.util.RenderUtil'),
  117. tagsRe: /<[^>]*>/gm,
  118. tagsProtect: '\x0f',
  119. regExpProtect: /\\|\/|\+|\\|\.|\[|\]|\{|\}|\?|\$|\*|\^|\|/gm,
  120. initComponent : function(){
  121. condition = this.BaseUtil.getUrlParam('urlcondition');
  122. condition = (condition == null) ? "" : condition;
  123. condition = condition.replace(/@/,"'%").replace(/@/,"%'");
  124. this.defaultCondition = condition;//固定条件;从url里面获取
  125. caller = this.BaseUtil.getUrlParam('whoami');
  126. this.getCount(caller, condition);
  127. this.callParent(arguments);
  128. } ,
  129. getColumnsAndStore: function(c, d, g, s){
  130. c = c || caller;
  131. d = d || condition;
  132. g = g || page;
  133. s = s || pageSize;
  134. var me = this;
  135. var f = d;
  136. if(me.filterCondition){
  137. if(d == null || d == ''){
  138. f = me.filterCondition;
  139. } else {
  140. f += ' AND ' + me.filterCondition;
  141. }
  142. }
  143. me.BaseUtil.getActiveTab().setLoading(true);//loading...
  144. Ext.Ajax.request({//拿到grid的columns
  145. url : basePath + 'common/datalist.action',
  146. params: {
  147. caller: c,
  148. condition: f,
  149. page: g,
  150. pageSize: s
  151. },
  152. method : 'post',
  153. callback : function(options,success,response){
  154. me.BaseUtil.getActiveTab().setLoading(false);
  155. var res = new Ext.decode(response.responseText);
  156. if(res.exception || res.exceptionInfo){
  157. showError(res.exceptionInfo);
  158. return;
  159. }
  160. var data = res.data != null ? Ext.decode(res.data.replace(/,}/g, '}').replace(/,]/g, ']')) : [];//一定要去掉多余逗号,ie对此很敏感
  161. if(me.columns && me.columns.length > 2){
  162. me.store.loadData(data);
  163. if(me.store.data.items.length != data.length){
  164. me.store.add(data);
  165. }
  166. if(me.lastSelected && me.lastSelected.length > 0){//grid刷新后,仍然选中上次选中的record
  167. Ext.each(me.store.data.items, function(item){
  168. if(item.data[keyField] == me.lastSelected[0].data[keyField]){
  169. me.selModel.select(item);
  170. }
  171. });
  172. }
  173. } else {
  174. var store = Ext.create('Ext.data.Store', {
  175. fields: res.fields,
  176. data: data
  177. });
  178. //处理render
  179. var grid = this;
  180. Ext.Array.each(res.columns, function(column, y) {
  181. if(!column.haveRendered && column.renderer != null && column.renderer != ""){
  182. if(!grid.RenderUtil){
  183. grid.RenderUtil = Ext.create('erp.util.RenderUtil');
  184. }
  185. var renderName = column.renderer;
  186. if(contains(column.renderer, ':', true)){
  187. var args = new Array();
  188. Ext.each(column.renderer.split(':'), function(a, index){
  189. if(index == 0){
  190. renderName = a;
  191. } else {
  192. args.push(a);
  193. }
  194. });
  195. if(!grid.RenderUtil.args[renderName]){
  196. grid.RenderUtil.args[renderName] = new Object();
  197. }
  198. grid.RenderUtil.args[renderName][column.dataIndex] = args;
  199. //这里只能用column.dataIndex来标志,不能用x,y,index等,
  200. //grid在render时,checkbox占一列
  201. }
  202. column.renderer = grid.RenderUtil[renderName];
  203. column.haveRendered = true;
  204. }
  205. });
  206. me.reconfigure(store, res.columns);//用这个方法每次都会add一个checkbox列
  207. }
  208. //修改pagingtoolbar信息
  209. Ext.getCmp('pagingtoolbar').afterOnLoad();
  210. //拿到datalist对应的单表的关键词
  211. keyField = res.keyField;//form表主键字段
  212. pfField = res.pfField;//grid表主键字段
  213. url = basePath + res.url;//grid行选择之后iframe嵌入的页面链接
  214. relative = res.relative;
  215. if(res.vastbutton && res.vastbutton == 'erpAddButton'){//[新增]功能
  216. Ext.getCmp('erpAddButton').show();
  217. }
  218. }
  219. });
  220. },
  221. getCount: function(c, d){
  222. c = c || caller;
  223. d = d || condition;
  224. var me = this;
  225. var f = d;
  226. if(me.filterCondition){
  227. if(d == null || d == ''){
  228. f = me.filterCondition;
  229. } else {
  230. f += ' AND ' + me.filterCondition;
  231. }
  232. }
  233. Ext.Ajax.request({//拿到grid的数据总数count
  234. url : basePath + '/common/datalistCount.action',
  235. params: {
  236. caller: c,
  237. condition: f
  238. },
  239. method : 'post',
  240. callback : function(options,success,response){
  241. var res = new Ext.decode(response.responseText);
  242. if(res.exception || res.exceptionInfo){
  243. showError(res.exceptionInfo);
  244. return;
  245. }
  246. dataCount = res.count;
  247. me.getColumnsAndStore(c, d);
  248. }
  249. });
  250. },
  251. listeners: {
  252. 'headerfiltersapply': function(grid, filters) {
  253. if(this.allowFilter){
  254. var condition = null;
  255. for(var fn in filters){
  256. var value = filters[fn];
  257. if(!Ext.isEmpty(value)){
  258. if(Ext.isDate(value)){
  259. value = Ext.Date.toString(value);
  260. value = fn + "=to_date('" + value + "','yyyy-MM-dd') ";
  261. } else {
  262. var exp_t = /^(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/,
  263. exp_d = /^(\d{4})\-(\d{2})\-(\d{2})$/;
  264. if(exp_d.test(value)){
  265. value = fn + "=to_date('" + value + "','yyyy-MM-dd') ";
  266. } else if(exp_t.test(value)){
  267. value = fn + "=to_date('" + value + "','yyyy-MM-dd HH24:mi:ss') ";
  268. } else{
  269. value = fn + " LIKE '%" + value + "%' ";
  270. }
  271. }
  272. if(condition == null){
  273. condition = value;
  274. } else {
  275. condition = condition + " AND " + value;
  276. }
  277. }
  278. }
  279. this.filterCondition = condition;
  280. page = 1;
  281. this.getCount();
  282. } else {
  283. this.allowFilter = true;
  284. }
  285. return false;
  286. }
  287. },
  288. reconfigure: function(store, columns){
  289. //改写reconfigure方法
  290. var d = this.headerCt;
  291. if (this.columns.length <= 1 && columns) {//this.columns.length > 1表示grid的columns已存在,没必要remove再add
  292. if(!Ext.isChrome){//ie,firefox下,format出现NaN-NaN-NaN,暂时作string处理
  293. Ext.each(columns, function(c){
  294. if(c.xtype == 'datecolumn'){
  295. c.xtype = "";
  296. c.format = "";
  297. }
  298. });
  299. }
  300. d.suspendLayout = true;
  301. d.removeAll();
  302. d.add(columns);
  303. }
  304. if (store) {
  305. if(!Ext.isChrome){//ie,firefox下,format出现NaN-NaN-NaN
  306. Ext.each(store.fields, function(f){
  307. if(f.type == 'date'){
  308. f.type = "string";
  309. f.format = "";
  310. }
  311. });
  312. }
  313. this.bindStore(store);
  314. } else {
  315. this.getView().refresh();
  316. }
  317. if (columns) {
  318. d.suspendLayout = false;
  319. this.forceComponentLayout();
  320. }
  321. this.fireEvent("reconfigure", this);
  322. },
  323. getSearchValue: function() {
  324. var me = this,
  325. value = Ext.getCmp('search').getValue();
  326. if (value === '') {
  327. return null;
  328. }
  329. if (!me.regExpMode) {
  330. value = value.replace(me.regExpProtect, function(m) {
  331. return '\\' + m;
  332. });
  333. } else {
  334. try {
  335. new RegExp(value);
  336. } catch (error) {
  337. me.statusBar.setStatus({
  338. text: error.message,
  339. iconCls: 'x-status-error'
  340. });
  341. return null;
  342. }
  343. // this is stupid
  344. if (value === '^' || value === '$') {
  345. return null;
  346. }
  347. }
  348. return value;
  349. },
  350. });