grid.addons.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. (function($){
  2. /*
  3. * jqGrid methods without support. Use as you wish
  4. * Tony Tomov tony@trirand.com
  5. * http://trirand.com/blog/
  6. * Dual licensed under the MIT and GPL licenses:
  7. * http://www.opensource.org/licenses/mit-license.php
  8. * http://www.gnu.org/licenses/gpl-2.0.html
  9. *
  10. * This list of deprecated methods.
  11. * If you instead want to use them, please include this file after the grid main file.
  12. * Some methods will be then overwritten.
  13. *
  14. */
  15. /*global jQuery, $ */
  16. $.jgrid.extend({
  17. // This is the ols search Filter method used in navigator.
  18. searchGrid : function (p) {
  19. p = $.extend({
  20. recreateFilter: false,
  21. drag: true,
  22. sField:'searchField',
  23. sValue:'searchString',
  24. sOper: 'searchOper',
  25. sFilter: 'filters',
  26. loadDefaults: true, // this options activates loading of default filters from grid's postData for Multipe Search only.
  27. beforeShowSearch: null,
  28. afterShowSearch : null,
  29. onInitializeSearch: null,
  30. closeAfterSearch : false,
  31. closeAfterReset: false,
  32. closeOnEscape : false,
  33. multipleSearch : false,
  34. cloneSearchRowOnAdd: true,
  35. // translation
  36. // if you want to change or remove the order change it in sopt
  37. // ['bw','eq','ne','lt','le','gt','ge','ew','cn']
  38. sopt: null,
  39. // Note: stringResult is intentionally declared "undefined by default".
  40. // you are velcome to define stringResult expressly in the options you pass to searchGrid()
  41. // stringResult is a "safeguard" measure to insure we post sensible data when communicated as form-encoded
  42. // see http://github.com/tonytomov/jqGrid/issues/#issue/36
  43. //
  44. // If this value is not expressly defined in the incoming options,
  45. // lower in the code we will infer the value based on value of multipleSearch
  46. stringResult: undefined,
  47. onClose : null,
  48. // useDataProxy allows ADD, EDIT and DEL code to bypass calling $.ajax
  49. // directly when grid's 'dataProxy' property (grid.p.dataProxy) is a function.
  50. // Used for "editGridRow" and "delGridRow" below and automatically flipped to TRUE
  51. // when ajax setting's 'url' (grid's 'editurl') property is undefined.
  52. // When 'useDataProxy' is true, instead of calling $.ajax.call(gridDOMobj, o, i) we call
  53. // gridDOMobj.p.dataProxy.call(gridDOMobj, o, i)
  54. //
  55. // Behavior is extremely similar to when 'datatype' is a function, but arguments are slightly different.
  56. // Normally the following is fed to datatype.call(a, b, c):
  57. // a = Pointer to grid's table DOM element, b = grid.p.postdata, c = "load_"+grid's ID
  58. // In cases of "edit" and "del" the following is fed:
  59. // a = Pointer to grid's table DOM element (same),
  60. // b = extended Ajax Options including postdata in "data" property. (different object type)
  61. // c = "set_"+grid's ID in case of "edit" and "del_"+grid's ID in case of "del" (same type, different content)
  62. // The major difference is that complete ajax options object, with attached "complete" and "error"
  63. // callback functions is fed instead of only post data.
  64. // This allows you to emulate a $.ajax call (including calling "complete"/"error"),
  65. // while retrieving the data locally in the browser.
  66. useDataProxy: false,
  67. overlay : true
  68. }, $.jgrid.search, p || {});
  69. return this.each(function() {
  70. var $t = this;
  71. if(!$t.grid) {return;}
  72. var fid = "fbox_"+$t.p.id,
  73. showFrm = true;
  74. function applyDefaultFilters(gridDOMobj, filterSettings) {
  75. /*
  76. gridDOMobj = ointer to grid DOM object ( $(#list)[0] )
  77. What we need from gridDOMobj:
  78. gridDOMobj.SearchFilter is the pointer to the Search box, once it's created.
  79. gridDOMobj.p.postData - dictionary of post settings. These can be overriden at grid creation to
  80. contain default filter settings. We will parse these and will populate the search with defaults.
  81. filterSettings - same settings object you (would) pass to $().jqGrid('searchGrid', filterSettings);
  82. */
  83. // Pulling default filter settings out of postData property of grid's properties.:
  84. var defaultFilters = gridDOMobj.p.postData[filterSettings.sFilter];
  85. // example of what we might get: {"groupOp":"and","rules":[{"field":"amount","op":"eq","data":"100"}]}
  86. // suppose we have imported this with grid import, the this is a string.
  87. if(typeof(defaultFilters) == "string") {
  88. defaultFilters = $.jgrid.parse(defaultFilters);
  89. }
  90. if (defaultFilters) {
  91. if (defaultFilters.groupOp) {
  92. gridDOMobj.SearchFilter.setGroupOp(defaultFilters.groupOp);
  93. }
  94. if (defaultFilters.rules) {
  95. var f, i = 0, li = defaultFilters.rules.length, success = false;
  96. for (; i < li; i++) {
  97. f = defaultFilters.rules[i];
  98. // we are not trying to counter all issues with filter declaration here. Just the basics to avoid lookup exceptions.
  99. if (f.field !== undefined && f.op !== undefined && f.data !== undefined) {
  100. success = gridDOMobj.SearchFilter.setFilter({
  101. 'sfref':gridDOMobj.SearchFilter.$.find(".sf:last"),
  102. 'filter':$.extend({},f)
  103. });
  104. if (success) { gridDOMobj.SearchFilter.add(); }
  105. }
  106. }
  107. }
  108. }
  109. } // end of applyDefaultFilters
  110. function hideFilter(selector) {
  111. if(p.onClose){
  112. var fclm = p.onClose(selector);
  113. if(typeof fclm == 'boolean' && !fclm) { return; }
  114. }
  115. selector.hide();
  116. if(p.overlay === true) {
  117. $(".jqgrid-overlay:first","#gbox_"+$t.p.id).hide();
  118. }
  119. }
  120. function showFilter(){
  121. var fl = $(".ui-searchFilter").length;
  122. if(fl > 1) {
  123. var zI = $("#"+fid).css("zIndex");
  124. $("#"+fid).css({zIndex:parseInt(zI,10)+fl});
  125. }
  126. $("#"+fid).show();
  127. if(p.overlay === true) {
  128. $(".jqgrid-overlay:first","#gbox_"+$t.p.id).show();
  129. }
  130. try{$(':input:visible',"#"+fid)[0].focus();}catch(_){}
  131. }
  132. function searchFilters(filters) {
  133. var hasFilters = (filters !== undefined),
  134. grid = $("#"+$t.p.id),
  135. sdata={};
  136. if(p.multipleSearch===false) {
  137. sdata[p.sField] = filters.rules[0].field;
  138. sdata[p.sValue] = filters.rules[0].data;
  139. sdata[p.sOper] = filters.rules[0].op;
  140. if(sdata.hasOwnProperty(p.sFilter) ) {
  141. delete sdata[p.sFilter];
  142. }
  143. } else {
  144. sdata[p.sFilter] = filters;
  145. $.each([p.sField, p.sValue, p.sOper], function(i, n){
  146. if(sdata.hasOwnProperty(n)) { delete sdata[n];}
  147. });
  148. }
  149. grid[0].p.search = hasFilters;
  150. $.extend(grid[0].p.postData,sdata);
  151. grid.trigger("reloadGrid",[{page:1}]);
  152. if(p.closeAfterSearch) { hideFilter($("#"+fid)); }
  153. }
  154. function resetFilters(op) {
  155. var reload = op && op.hasOwnProperty("reload") ? op.reload : true,
  156. grid = $("#"+$t.p.id),
  157. sdata={};
  158. grid[0].p.search = false;
  159. if(p.multipleSearch===false) {
  160. sdata[p.sField] = sdata[p.sValue] = sdata[p.sOper] = "";
  161. } else {
  162. sdata[p.sFilter] = "";
  163. }
  164. $.extend(grid[0].p.postData,sdata);
  165. if(reload) {
  166. grid.trigger("reloadGrid",[{page:1}]);
  167. }
  168. if(p.closeAfterReset) { hideFilter($("#"+fid)); }
  169. }
  170. if($.fn.searchFilter) {
  171. if(p.recreateFilter===true) {$("#"+fid).remove();}
  172. if( $("#"+fid).html() !== null ) {
  173. if ( $.isFunction(p.beforeShowSearch) ) {
  174. showFrm = p.beforeShowSearch($("#"+fid));
  175. if(typeof(showFrm) == "undefined") {
  176. showFrm = true;
  177. }
  178. }
  179. if(showFrm === false) { return; }
  180. showFilter();
  181. if( $.isFunction(p.afterShowSearch) ) { p.afterShowSearch($("#"+fid)); }
  182. } else {
  183. var fields = [],
  184. colNames = $("#"+$t.p.id).jqGrid("getGridParam","colNames"),
  185. colModel = $("#"+$t.p.id).jqGrid("getGridParam","colModel"),
  186. stempl = ['eq','ne','lt','le','gt','ge','bw','bn','in','ni','ew','en','cn','nc'],
  187. j,pos,k,oprtr=[];
  188. if (p.sopt !==null) {
  189. k=0;
  190. for(j=0;j<p.sopt.length;j++) {
  191. if( (pos= $.inArray(p.sopt[j],stempl)) != -1 ){
  192. oprtr[k] = {op:p.sopt[j],text: p.odata[pos]};
  193. k++;
  194. }
  195. }
  196. } else {
  197. for(j=0;j<stempl.length;j++) {
  198. oprtr[j] = {op:stempl[j],text: p.odata[j]};
  199. }
  200. }
  201. $.each(colModel, function(i, v) {
  202. var searchable = (typeof v.search === 'undefined') ? true: v.search ,
  203. hidden = (v.hidden === true),
  204. soptions = $.extend({}, {text: colNames[i], itemval: v.index || v.name}, this.searchoptions),
  205. ignoreHiding = (soptions.searchhidden === true);
  206. if(typeof soptions.sopt !== 'undefined') {
  207. k=0;
  208. soptions.ops =[];
  209. if(soptions.sopt.length>0) {
  210. for(j=0;j<soptions.sopt.length;j++) {
  211. if( (pos= $.inArray(soptions.sopt[j],stempl)) != -1 ){
  212. soptions.ops[k] = {op:soptions.sopt[j],text: p.odata[pos]};
  213. k++;
  214. }
  215. }
  216. }
  217. }
  218. if(typeof(this.stype) === 'undefined') { this.stype='text'; }
  219. if(this.stype == 'select') {
  220. if ( soptions.dataUrl !== undefined) {}
  221. else {
  222. var eov;
  223. if(soptions.value) {
  224. eov = soptions.value;
  225. } else if(this.editoptions) {
  226. eov = this.editoptions.value;
  227. }
  228. if(eov) {
  229. soptions.dataValues =[];
  230. if(typeof(eov) === 'string') {
  231. var so = eov.split(";"),sv;
  232. for(j=0;j<so.length;j++) {
  233. sv = so[j].split(":");
  234. soptions.dataValues[j] ={value:sv[0],text:sv[1]};
  235. }
  236. } else if (typeof(eov) === 'object') {
  237. j=0;
  238. for (var key in eov) {
  239. if(eov.hasOwnProperty(key)) {
  240. soptions.dataValues[j] ={value:key,text:eov[key]};
  241. j++;
  242. }
  243. }
  244. }
  245. }
  246. }
  247. }
  248. if ((ignoreHiding && searchable) || (searchable && !hidden)) {
  249. fields.push(soptions);
  250. }
  251. });
  252. if(fields.length>0){
  253. $("<div id='"+fid+"' role='dialog' tabindex='-1'></div>").insertBefore("#gview_"+$t.p.id);
  254. // Before we create searchFilter we need to decide if we want to get back a string or a JS object.
  255. // see http://github.com/tonytomov/jqGrid/issues/#issue/36 for background on the issue.
  256. // If p.stringResult is defined, it was explisitly passed to us by user. Honor the choice, whatever it is.
  257. if (p.stringResult===undefined) {
  258. // to provide backward compatibility, inferring stringResult value from multipleSearch
  259. p.stringResult = p.multipleSearch;
  260. }
  261. // we preserve the return value here to retain access to .add() and other good methods of search form.
  262. $t.SearchFilter = $("#"+fid).searchFilter(fields, { groupOps: p.groupOps, operators: oprtr, onClose:hideFilter, resetText: p.Reset, searchText: p.Find, windowTitle: p.caption, rulesText:p.rulesText, matchText:p.matchText, onSearch: searchFilters, onReset: resetFilters,stringResult:p.stringResult, ajaxSelectOptions: $.extend({},$.jgrid.ajaxOptions,$t.p.ajaxSelectOptions ||{}), clone: p.cloneSearchRowOnAdd });
  263. $(".ui-widget-overlay","#"+fid).remove();
  264. if($t.p.direction=="rtl") { $(".ui-closer","#"+fid).css("float","left"); }
  265. if (p.drag===true) {
  266. $("#"+fid+" table thead tr:first td:first").css('cursor','move');
  267. if(jQuery.fn.jqDrag) {
  268. $("#"+fid).jqDrag($("#"+fid+" table thead tr:first td:first"));
  269. } else {
  270. try {
  271. $("#"+fid).draggable({handle: $("#"+fid+" table thead tr:first td:first")});
  272. } catch (e) {}
  273. }
  274. }
  275. if(p.multipleSearch === false) {
  276. $(".ui-del, .ui-add, .ui-del, .ui-add-last, .matchText, .rulesText", "#"+fid).hide();
  277. $("select[name='groupOp']","#"+fid).hide();
  278. }
  279. if (p.multipleSearch === true && p.loadDefaults === true) {
  280. applyDefaultFilters($t, p);
  281. }
  282. if ( $.isFunction(p.onInitializeSearch) ) { p.onInitializeSearch( $("#"+fid) ); }
  283. if ( $.isFunction(p.beforeShowSearch) ) {
  284. showFrm = p.beforeShowSearch($("#"+fid));
  285. if(typeof(showFrm) == "undefined") {
  286. showFrm = true;
  287. }
  288. }
  289. if(showFrm === false) { return; }
  290. showFilter();
  291. if( $.isFunction(p.afterShowSearch) ) { p.afterShowSearch($("#"+fid)); }
  292. if(p.closeOnEscape===true){
  293. $("#"+fid).keydown( function( e ) {
  294. if( e.which == 27 ) {
  295. hideFilter($("#"+fid));
  296. }
  297. if (e.which == 13) {
  298. $(".ui-search", this).click();
  299. }
  300. });
  301. }
  302. }
  303. }
  304. }
  305. });
  306. },
  307. // methods taken from grid.custom.
  308. updateGridRows : function (data, rowidname, jsonreader) {
  309. var nm, success=false, title;
  310. this.each(function(){
  311. var t = this, vl, ind, srow, sid;
  312. if(!t.grid) {return false;}
  313. if(!rowidname) { rowidname = "id"; }
  314. if( data && data.length >0 ) {
  315. $(data).each(function(j){
  316. srow = this;
  317. ind = t.rows.namedItem(srow[rowidname]);
  318. if(ind) {
  319. sid = srow[rowidname];
  320. if(jsonreader === true){
  321. if(t.p.jsonReader.repeatitems === true) {
  322. if(t.p.jsonReader.cell) {srow = srow[t.p.jsonReader.cell];}
  323. for (var k=0;k<srow.length;k++) {
  324. vl = t.formatter( sid, srow[k], k, srow, 'edit');
  325. title = t.p.colModel[k].title ? {"title":$.jgrid.stripHtml(vl)} : {};
  326. if(t.p.treeGrid===true && nm == t.p.ExpandColumn) {
  327. $("td:eq("+k+") > span:first",ind).html(vl).attr(title);
  328. } else {
  329. $("td:eq("+k+")",ind).html(vl).attr(title);
  330. }
  331. }
  332. success = true;
  333. return true;
  334. }
  335. }
  336. $(t.p.colModel).each(function(i){
  337. nm = jsonreader===true ? this.jsonmap || this.name :this.name;
  338. if( srow[nm] !== undefined) {
  339. vl = t.formatter( sid, srow[nm], i, srow, 'edit');
  340. title = this.title ? {"title":$.jgrid.stripHtml(vl)} : {};
  341. if(t.p.treeGrid===true && nm == t.p.ExpandColumn) {
  342. $("td:eq("+i+") > span:first",ind).html(vl).attr(title);
  343. } else {
  344. $("td:eq("+i+")",ind).html(vl).attr(title);
  345. }
  346. success = true;
  347. }
  348. });
  349. }
  350. });
  351. }
  352. });
  353. return success;
  354. },
  355. // Form search - sorry for this method. Instead use ne jqFilter method.
  356. filterGrid : function(gridid,p){
  357. p = $.extend({
  358. gridModel : false,
  359. gridNames : false,
  360. gridToolbar : false,
  361. filterModel: [], // label/name/stype/defval/surl/sopt
  362. formtype : "horizontal", // horizontal/vertical
  363. autosearch: true, // if set to false a serch button should be enabled.
  364. formclass: "filterform",
  365. tableclass: "filtertable",
  366. buttonclass: "filterbutton",
  367. searchButton: "Search",
  368. clearButton: "Clear",
  369. enableSearch : false,
  370. enableClear: false,
  371. beforeSearch: null,
  372. afterSearch: null,
  373. beforeClear: null,
  374. afterClear: null,
  375. url : '',
  376. marksearched: true
  377. },p || {});
  378. return this.each(function(){
  379. var self = this;
  380. this.p = p;
  381. if(this.p.filterModel.length === 0 && this.p.gridModel===false) { alert("No filter is set"); return;}
  382. if( !gridid) {alert("No target grid is set!"); return;}
  383. this.p.gridid = gridid.indexOf("#") != -1 ? gridid : "#"+gridid;
  384. var gcolMod = $(this.p.gridid).jqGrid("getGridParam",'colModel');
  385. if(gcolMod) {
  386. if( this.p.gridModel === true) {
  387. var thegrid = $(this.p.gridid)[0];
  388. var sh;
  389. // we should use the options search, edittype, editoptions
  390. // additionally surl and defval can be added in grid colModel
  391. $.each(gcolMod, function (i,n) {
  392. var tmpFil = [];
  393. this.search = this.search === false ? false : true;
  394. if(this.editrules && this.editrules.searchhidden === true) {
  395. sh = true;
  396. } else {
  397. if(this.hidden === true ) {
  398. sh = false;
  399. } else {
  400. sh = true;
  401. }
  402. }
  403. if( this.search === true && sh === true) {
  404. if(self.p.gridNames===true) {
  405. tmpFil.label = thegrid.p.colNames[i];
  406. } else {
  407. tmpFil.label = '';
  408. }
  409. tmpFil.name = this.name;
  410. tmpFil.index = this.index || this.name;
  411. // we support only text and selects, so all other to text
  412. tmpFil.stype = this.edittype || 'text';
  413. if(tmpFil.stype != 'select' ) {
  414. tmpFil.stype = 'text';
  415. }
  416. tmpFil.defval = this.defval || '';
  417. tmpFil.surl = this.surl || '';
  418. tmpFil.sopt = this.editoptions || {};
  419. tmpFil.width = this.width;
  420. self.p.filterModel.push(tmpFil);
  421. }
  422. });
  423. } else {
  424. $.each(self.p.filterModel,function(i,n) {
  425. for(var j=0;j<gcolMod.length;j++) {
  426. if(this.name == gcolMod[j].name) {
  427. this.index = gcolMod[j].index || this.name;
  428. break;
  429. }
  430. }
  431. if(!this.index) {
  432. this.index = this.name;
  433. }
  434. });
  435. }
  436. } else {
  437. alert("Could not get grid colModel"); return;
  438. }
  439. var triggerSearch = function() {
  440. var sdata={}, j=0, v;
  441. var gr = $(self.p.gridid)[0], nm;
  442. gr.p.searchdata = {};
  443. if($.isFunction(self.p.beforeSearch)){self.p.beforeSearch();}
  444. $.each(self.p.filterModel,function(i,n){
  445. nm = this.index;
  446. if(this.stype === 'select') {
  447. v = $("select[name="+nm+"]",self).val();
  448. if(v) {
  449. sdata[nm] = v;
  450. if(self.p.marksearched){
  451. $("#jqgh_"+this.name,gr.grid.hDiv).addClass("dirty-cell");
  452. }
  453. j++;
  454. } else {
  455. if(self.p.marksearched){
  456. $("#jqgh_"+this.name,gr.grid.hDiv).removeClass("dirty-cell");
  457. }
  458. try {
  459. delete gr.p.postData[this.index];
  460. } catch (e) {}
  461. }
  462. } else {
  463. v = $("input[name="+nm+"]",self).val();
  464. if(v) {
  465. sdata[nm] = v;
  466. if(self.p.marksearched){
  467. $("#jqgh_"+this.name,gr.grid.hDiv).addClass("dirty-cell");
  468. }
  469. j++;
  470. } else {
  471. if(self.p.marksearched){
  472. $("#jqgh_"+this.name,gr.grid.hDiv).removeClass("dirty-cell");
  473. }
  474. try {
  475. delete gr.p.postData[this.index];
  476. } catch(x) {}
  477. }
  478. }
  479. });
  480. var sd = j>0 ? true : false;
  481. $.extend(gr.p.postData,sdata);
  482. var saveurl;
  483. if(self.p.url) {
  484. saveurl = $(gr).jqGrid("getGridParam",'url');
  485. $(gr).jqGrid("setGridParam",{url:self.p.url});
  486. }
  487. $(gr).jqGrid("setGridParam",{search:sd}).trigger("reloadGrid",[{page:1}]);
  488. if(saveurl) {$(gr).jqGrid("setGridParam",{url:saveurl});}
  489. if($.isFunction(self.p.afterSearch)){self.p.afterSearch();}
  490. };
  491. var clearSearch = function(){
  492. var sdata={}, v, j=0;
  493. var gr = $(self.p.gridid)[0], nm;
  494. if($.isFunction(self.p.beforeClear)){self.p.beforeClear();}
  495. $.each(self.p.filterModel,function(i,n){
  496. nm = this.index;
  497. v = (this.defval) ? this.defval : "";
  498. if(!this.stype){this.stype='text';}
  499. switch (this.stype) {
  500. case 'select' :
  501. var v1;
  502. $("select[name="+nm+"] option",self).each(function (i){
  503. if(i===0) { this.selected = true; }
  504. if ($(this).text() == v) {
  505. this.selected = true;
  506. v1 = $(this).val();
  507. return false;
  508. }
  509. });
  510. if(v1) {
  511. // post the key and not the text
  512. sdata[nm] = v1;
  513. if(self.p.marksearched){
  514. $("#jqgh_"+this.name,gr.grid.hDiv).addClass("dirty-cell");
  515. }
  516. j++;
  517. } else {
  518. if(self.p.marksearched){
  519. $("#jqgh_"+this.name,gr.grid.hDiv).removeClass("dirty-cell");
  520. }
  521. try {
  522. delete gr.p.postData[this.index];
  523. } catch (e) {}
  524. }
  525. break;
  526. case 'text':
  527. $("input[name="+nm+"]",self).val(v);
  528. if(v) {
  529. sdata[nm] = v;
  530. if(self.p.marksearched){
  531. $("#jqgh_"+this.name,gr.grid.hDiv).addClass("dirty-cell");
  532. }
  533. j++;
  534. } else {
  535. if(self.p.marksearched){
  536. $("#jqgh_"+this.name,gr.grid.hDiv).removeClass("dirty-cell");
  537. }
  538. try {
  539. delete gr.p.postData[this.index];
  540. } catch (k) {}
  541. }
  542. break;
  543. }
  544. });
  545. var sd = j>0 ? true : false;
  546. $.extend(gr.p.postData,sdata);
  547. var saveurl;
  548. if(self.p.url) {
  549. saveurl = $(gr).jqGrid("getGridParam",'url');
  550. $(gr).jqGrid("setGridParam",{url:self.p.url});
  551. }
  552. $(gr).jqGrid("setGridParam",{search:sd}).trigger("reloadGrid",[{page:1}]);
  553. if(saveurl) {$(gr).jqGrid("setGridParam",{url:saveurl});}
  554. if($.isFunction(self.p.afterClear)){self.p.afterClear();}
  555. };
  556. var tbl;
  557. var formFill = function(){
  558. var tr = document.createElement("tr");
  559. var tr1, sb, cb,tl,td;
  560. if(self.p.formtype=='horizontal'){
  561. $(tbl).append(tr);
  562. }
  563. $.each(self.p.filterModel,function(i,n){
  564. tl = document.createElement("td");
  565. $(tl).append("<label for='"+this.name+"'>"+this.label+"</label>");
  566. td = document.createElement("td");
  567. var $t=this;
  568. if(!this.stype) { this.stype='text';}
  569. switch (this.stype)
  570. {
  571. case "select":
  572. if(this.surl) {
  573. // data returned should have already constructed html select
  574. $(td).load(this.surl,function(){
  575. if($t.defval) { $("select",this).val($t.defval); }
  576. $("select",this).attr({name:$t.index || $t.name, id: "sg_"+$t.name});
  577. if($t.sopt) { $("select",this).attr($t.sopt); }
  578. if(self.p.gridToolbar===true && $t.width) {
  579. $("select",this).width($t.width);
  580. }
  581. if(self.p.autosearch===true){
  582. $("select",this).change(function(e){
  583. triggerSearch();
  584. return false;
  585. });
  586. }
  587. });
  588. } else {
  589. // sopt to construct the values
  590. if($t.sopt.value) {
  591. var oSv = $t.sopt.value;
  592. var elem = document.createElement("select");
  593. $(elem).attr({name:$t.index || $t.name, id: "sg_"+$t.name}).attr($t.sopt);
  594. var so, sv, ov;
  595. if(typeof oSv === "string") {
  596. so = oSv.split(";");
  597. for(var k=0; k<so.length;k++){
  598. sv = so[k].split(":");
  599. ov = document.createElement("option");
  600. ov.value = sv[0]; ov.innerHTML = sv[1];
  601. if (sv[1]==$t.defval) { ov.selected ="selected"; }
  602. elem.appendChild(ov);
  603. }
  604. } else if(typeof oSv === "object" ) {
  605. for ( var key in oSv) {
  606. if(oSv.hasOwnProperty(key)) {
  607. i++;
  608. ov = document.createElement("option");
  609. ov.value = key; ov.innerHTML = oSv[key];
  610. if (oSv[key]==$t.defval) { ov.selected ="selected"; }
  611. elem.appendChild(ov);
  612. }
  613. }
  614. }
  615. if(self.p.gridToolbar===true && $t.width) {
  616. $(elem).width($t.width);
  617. }
  618. $(td).append(elem);
  619. if(self.p.autosearch===true){
  620. $(elem).change(function(e){
  621. triggerSearch();
  622. return false;
  623. });
  624. }
  625. }
  626. }
  627. break;
  628. case 'text':
  629. var df = this.defval ? this.defval: "";
  630. $(td).append("<input type='text' name='"+(this.index || this.name)+"' id='sg_"+this.name+"' value='"+df+"'/>");
  631. if($t.sopt) { $("input",td).attr($t.sopt); }
  632. if(self.p.gridToolbar===true && $t.width) {
  633. if($.browser.msie) {
  634. $("input",td).width($t.width-4);
  635. } else {
  636. $("input",td).width($t.width-2);
  637. }
  638. }
  639. if(self.p.autosearch===true){
  640. $("input",td).keypress(function(e){
  641. var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
  642. if(key == 13){
  643. triggerSearch();
  644. return false;
  645. }
  646. return this;
  647. });
  648. }
  649. break;
  650. }
  651. if(self.p.formtype=='horizontal'){
  652. if(self.p.gridToolbar===true && self.p.gridNames===false) {
  653. $(tr).append(td);
  654. } else {
  655. $(tr).append(tl).append(td);
  656. }
  657. $(tr).append(td);
  658. } else {
  659. tr1 = document.createElement("tr");
  660. $(tr1).append(tl).append(td);
  661. $(tbl).append(tr1);
  662. }
  663. });
  664. td = document.createElement("td");
  665. if(self.p.enableSearch === true){
  666. sb = "<input type='button' id='sButton' class='"+self.p.buttonclass+"' value='"+self.p.searchButton+"'/>";
  667. $(td).append(sb);
  668. $("input#sButton",td).click(function(){
  669. triggerSearch();
  670. return false;
  671. });
  672. }
  673. if(self.p.enableClear === true) {
  674. cb = "<input type='button' id='cButton' class='"+self.p.buttonclass+"' value='"+self.p.clearButton+"'/>";
  675. $(td).append(cb);
  676. $("input#cButton",td).click(function(){
  677. clearSearch();
  678. return false;
  679. });
  680. }
  681. if(self.p.enableClear === true || self.p.enableSearch === true) {
  682. if(self.p.formtype=='horizontal') {
  683. $(tr).append(td);
  684. } else {
  685. tr1 = document.createElement("tr");
  686. $(tr1).append("<td>&#160;</td>").append(td);
  687. $(tbl).append(tr1);
  688. }
  689. }
  690. };
  691. var frm = $("<form name='SearchForm' style=display:inline;' class='"+this.p.formclass+"'></form>");
  692. tbl =$("<table class='"+this.p.tableclass+"' cellspacing='0' cellpading='0' border='0'><tbody></tbody></table>");
  693. $(frm).append(tbl);
  694. formFill();
  695. $(this).append(frm);
  696. this.triggerSearch = triggerSearch;
  697. this.clearSearch = clearSearch;
  698. });
  699. }
  700. });
  701. })(jQuery);