PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/plugins/2dc_jqgrid/public/javascripts/jqgrid/grid.custom.js

https://github.com/huconco/jqgrid_demo_app
JavaScript | 437 lines | 417 code | 0 blank | 20 comment | 121 complexity | 728d4060c420f618833875d9deed1f27 MD5 | raw file
  1. ;(function($){
  2. /**
  3. * jqGrid extension for custom methods
  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.html
  9. **/
  10. $.fn.extend({
  11. getColProp : function(colname){
  12. var ret ={}, $t = this[0];
  13. if ( !$t.grid ) { return; }
  14. var cM = $t.p.colModel;
  15. for ( var i =0;i<cM.length;i++ ) {
  16. if ( cM[i].name == colname ) {
  17. ret = cM[i];
  18. break;
  19. }
  20. };
  21. return ret;
  22. },
  23. setColProp : function(colname, obj){
  24. //do not set width will not work
  25. return this.each(function(){
  26. if ( this.grid ) {
  27. if ( obj ) {
  28. var cM = this.p.colModel;
  29. for ( var i =0;i<cM.length;i++ ) {
  30. if ( cM[i].name == colname ) {
  31. $.extend(this.p.colModel[i],obj);
  32. break;
  33. }
  34. }
  35. }
  36. }
  37. });
  38. },
  39. sortGrid : function(colname,reload){
  40. return this.each(function(){
  41. var $t=this,idx=-1;
  42. if ( !$t.grid ) { return;}
  43. if ( !colname ) { colname = $t.p.sortname; }
  44. for ( var i=0;i<$t.p.colModel.length;i++ ) {
  45. if ( $t.p.colModel[i].index == colname || $t.p.colModel[i].name==colname ) {
  46. idx = i;
  47. break;
  48. }
  49. }
  50. if ( idx!=-1 ){
  51. var sort = $t.p.colModel[idx].sortable;
  52. if ( typeof sort !== 'boolean' ) { sort = true; }
  53. if ( typeof reload !=='boolean' ) { reload = false; }
  54. if ( sort ) { $t.sortData(colname, idx, reload); }
  55. }
  56. });
  57. },
  58. GridDestroy : function () {
  59. return this.each(function(){
  60. if ( this.grid ) {
  61. if ( this.p.pager ) {
  62. $(this.p.pager).remove();
  63. }
  64. var gid = this.id;
  65. $("#lui_"+gid).remove();
  66. try {
  67. $("#editmod"+gid).remove();
  68. $("#delmod"+gid).remove();
  69. $("#srchmod"+gid).remove();
  70. } catch (_) {}
  71. $(this.grid.bDiv).remove();
  72. $(this.grid.hDiv).remove();
  73. $(this.grid.cDiv).remove();
  74. if(this.p.toolbar[0]) { $(this.grid.uDiv).remove(); }
  75. this.p = null;
  76. this.grid =null;
  77. }
  78. });
  79. },
  80. GridUnload : function(){
  81. return this.each(function(){
  82. if ( !this.grid ) {return;}
  83. var defgrid = {id: $(this).attr('id'),cl: $(this).attr('class')};
  84. if (this.p.pager) {
  85. $(this.p.pager).empty();
  86. }
  87. var newtable = document.createElement('table');
  88. $(newtable).attr({id:defgrid['id']});
  89. newtable.className = defgrid['cl'];
  90. var gid = this.id;
  91. $("#lui_"+gid).remove();
  92. try {
  93. $("#editmod"+gid).remove();
  94. $("#delmod"+gid).remove();
  95. $("#srchmod"+gid).remove();
  96. } catch (_) {}
  97. if(this.p.toolbar[0]) { $(this.grid.uDiv).remove(); }
  98. $(this.grid.cDiv).remove();
  99. $(this.grid.bDiv).remove();
  100. $(this.grid.hDiv).before(newtable).remove();
  101. // here code to remove modals of form editing
  102. this.p = null;
  103. this.grid =null;
  104. });
  105. },
  106. filterGrid : function(gridid,p){
  107. p = $.extend({
  108. gridModel : false,
  109. gridNames : false,
  110. gridToolbar : false,
  111. filterModel: [], // label/name/stype/defval/surl/sopt
  112. formtype : "horizontal", // horizontal/vertical
  113. autosearch: true, // if set to false a serch button should be enabled.
  114. formclass: "filterform",
  115. tableclass: "filtertable",
  116. buttonclass: "filterbutton",
  117. searchButton: "Search",
  118. clearButton: "Clear",
  119. enableSearch : false,
  120. enableClear: false,
  121. beforeSearch: null,
  122. afterSearch: null,
  123. beforeClear: null,
  124. afterClear: null,
  125. url : '',
  126. marksearched: true
  127. },p || {});
  128. return this.each(function(){
  129. var self = this;
  130. this.p = p;
  131. if(this.p.filterModel.length == 0 && this.p.gridModel===false) { alert("No filter is set"); return;}
  132. if( !gridid) {alert("No target grid is set!"); return;}
  133. this.p.gridid = gridid.indexOf("#") != -1 ? gridid : "#"+gridid;
  134. var gcolMod = $(this.p.gridid).getGridParam('colModel');
  135. if(gcolMod) {
  136. if( this.p.gridModel === true) {
  137. var thegrid = $(this.p.gridid)[0];
  138. var sh;
  139. // we should use the options search, edittype, editoptions
  140. // additionally surl and defval can be added in grid colModel
  141. $.each(gcolMod, function (i,n) {
  142. var tmpFil = [];
  143. this.search = this.search === false ? false : true;
  144. if(this.editrules && this.editrules.searchhidden === true) {
  145. sh = true;
  146. } else {
  147. if(this.hidden === true ) {
  148. sh = false;
  149. } else {
  150. sh = true;
  151. }
  152. }
  153. if( this.search === true && sh === true) {
  154. if(self.p.gridNames===true) {
  155. tmpFil.label = thegrid.p.colNames[i];
  156. } else {
  157. tmpFil.label = '';
  158. }
  159. tmpFil.name = this.name;
  160. tmpFil.index = this.index || this.name;
  161. // we support only text and selects, so all other to text
  162. tmpFil.stype = this.edittype || 'text';
  163. if(tmpFil.stype != 'select' ) {
  164. tmpFil.stype = 'text';
  165. }
  166. tmpFil.defval = this.defval || '';
  167. tmpFil.surl = this.surl || '';
  168. tmpFil.sopt = this.editoptions || {};
  169. tmpFil.width = this.width;
  170. self.p.filterModel.push(tmpFil);
  171. }
  172. });
  173. } else {
  174. $.each(self.p.filterModel,function(i,n) {
  175. for(var j=0;j<gcolMod.length;j++) {
  176. if(this.name == gcolMod[j].name) {
  177. this.index = gcolMod[j].index || this.name;
  178. break;
  179. }
  180. }
  181. if(!this.index) {
  182. this.index = this.name;
  183. }
  184. });
  185. }
  186. } else {
  187. alert("Could not get grid colModel"); return;
  188. }
  189. var triggerSearch = function() {
  190. var sdata={}, j=0, v;
  191. var gr = $(self.p.gridid)[0];
  192. if($.isFunction(self.p.beforeSearch)){self.p.beforeSearch();}
  193. $.each(self.p.filterModel,function(i,n){
  194. switch (this.stype) {
  195. case 'select' :
  196. v = $("select[name="+this.name+"]",self).val();
  197. if(v) {
  198. sdata[this.index] = v;
  199. if(self.p.marksearched){
  200. $("#jqgh_"+this.name,gr.grid.hDiv).addClass("dirty-cell");
  201. }
  202. j++;
  203. } else {
  204. if(self.p.marksearched){
  205. $("#jqgh_"+this.name,gr.grid.hDiv).removeClass("dirty-cell");
  206. }
  207. // remove from postdata
  208. try {
  209. delete gr.p.postData[this.index];
  210. } catch(e) {}
  211. }
  212. break;
  213. default:
  214. v = $("input[name="+this.name+"]",self).val();
  215. if(v) {
  216. sdata[this.index] = v;
  217. if(self.p.marksearched){
  218. $("#jqgh_"+this.name,gr.grid.hDiv).addClass("dirty-cell");
  219. }
  220. j++;
  221. } else {
  222. if(self.p.marksearched){
  223. $("#jqgh_"+this.name,gr.grid.hDiv).removeClass("dirty-cell");
  224. }
  225. // remove from postdata
  226. try {
  227. delete gr.p.postData[this.index];
  228. } catch (e) {}
  229. }
  230. }
  231. });
  232. var sd = j>0 ? true : false;
  233. gr.p.postData = $.extend(gr.p.postData,sdata);
  234. var saveurl;
  235. if(self.p.url) {
  236. saveurl = $(gr).getGridParam('url');
  237. $(gr).setGridParam({url:self.p.url});
  238. }
  239. $(gr).setGridParam({search:sd,page:1}).trigger("reloadGrid");
  240. if(saveurl) {$(gr).setGridParam({url:saveurl});}
  241. if($.isFunction(self.p.afterSearch)){self.p.afterSearch();}
  242. };
  243. var clearSearch = function(){
  244. var sdata={}, v, j=0;
  245. var gr = $(self.p.gridid)[0];
  246. if($.isFunction(self.p.beforeClear)){self.p.beforeClear();}
  247. $.each(self.p.filterModel,function(i,n){
  248. v = (this.defval) ? this.defval : "";
  249. if(!this.stype){this.stype=='text';}
  250. switch (this.stype) {
  251. case 'select' :
  252. if(v) {
  253. var v1;
  254. $("select[name="+this.name+"] option",self).each(function (){
  255. if ($(this).text() == v) {
  256. this.selected = true;
  257. v1 = $(this).val();
  258. return false;
  259. }
  260. });
  261. // post the key and not the text
  262. sdata[this.index] = v1 || "";
  263. if(self.p.marksearched){
  264. $("#jqgh_"+this.name,gr.grid.hDiv).addClass("dirty-cell");
  265. }
  266. j++;
  267. } else {
  268. if(self.p.marksearched){
  269. $("#jqgh_"+this.name,gr.grid.hDiv).removeClass("dirty-cell");
  270. }
  271. // remove from postdata
  272. try {
  273. delete gr.p.postData[this.index];
  274. } catch(e) {}
  275. }
  276. break;
  277. case 'text':
  278. $("input[name="+this.name+"]",self).val(v);
  279. if(v) {
  280. sdata[this.index] = v;
  281. if(self.p.marksearched){
  282. $("#jqgh_"+this.name,gr.grid.hDiv).addClass("dirty-cell");
  283. }
  284. j++;
  285. } else {
  286. if(self.p.marksearched){
  287. $("#jqgh_"+this.name,gr.grid.hDiv).removeClass("dirty-cell");
  288. }
  289. // remove from postdata
  290. try {
  291. delete gr.p.postData[this.index];
  292. } catch(e) {}
  293. }
  294. }
  295. });
  296. var sd = j>0 ? true : false;
  297. gr.p.postData = $.extend(gr.p.postData,sdata);
  298. var saveurl;
  299. if(self.p.url) {
  300. saveurl = $(gr).getGridParam('url');
  301. $(gr).setGridParam({url:self.p.url});
  302. }
  303. $(gr).setGridParam({search:sd,page:1}).trigger("reloadGrid");
  304. if(saveurl) {$(gr).setGridParam({url:saveurl});}
  305. if($.isFunction(self.p.afterClear)){self.p.afterClear();}
  306. };
  307. var formFill = function(){
  308. var tr = document.createElement("tr");
  309. var tr1, sb, cb,tl,td, td1;
  310. if(self.p.formtype=='horizontal'){
  311. $(tbl).append(tr);
  312. }
  313. $.each(self.p.filterModel,function(i,n){
  314. tl = document.createElement("td");
  315. $(tl).append("<label for='"+this.name+"'>"+this.label+"</label>");
  316. td = document.createElement("td");
  317. var $t=this;
  318. if(!this.stype) { this.stype='text';}
  319. switch (this.stype)
  320. {
  321. case "select":
  322. if(this.surl) {
  323. // data returned should have already constructed html select
  324. $(td).load(this.surl,function(){
  325. if($t.defval) $("select",this).val($t.defval);
  326. $("select",this).attr({name:$t.name, id: "sg_"+$t.name});
  327. if($t.sopt) $("select",this).attr($t.sopt);
  328. if(self.p.gridToolbar===true && $t.width) {
  329. $("select",this).width($t.width);
  330. }
  331. if(self.p.autosearch===true){
  332. $("select",this).change(function(e){
  333. triggerSearch();
  334. return false;
  335. });
  336. }
  337. });
  338. } else {
  339. // sopt to construct the values
  340. if($t.sopt.value) {
  341. var so = $t.sopt.value.split(";"), sv, ov;
  342. var elem = document.createElement("select");
  343. $(elem).attr({name:$t.name, id: "sg_"+$t.name}).attr($t.sopt);
  344. for(var k=0; k<so.length;k++){
  345. sv = so[k].split(":");
  346. ov = document.createElement("option");
  347. ov.value = sv[0]; ov.innerHTML = sv[1];
  348. if (sv[1]==$t.defval) ov.selected ="selected";
  349. elem.appendChild(ov);
  350. }
  351. if(self.p.gridToolbar===true && $t.width) {
  352. $(elem).width($t.width);
  353. }
  354. $(td).append(elem);
  355. if(self.p.autosearch===true){
  356. $(elem).change(function(e){
  357. triggerSearch();
  358. return false;
  359. });
  360. }
  361. }
  362. }
  363. break;
  364. case 'text':
  365. var df = this.defval ? this.defval: "";
  366. $(td).append("<input type='text' name='"+this.name+"' id='sg_"+this.name+"' value='"+df+"'/>");
  367. if($t.sopt) $("input",td).attr($t.sopt);
  368. if(self.p.gridToolbar===true && $t.width) {
  369. if($.browser.msie) {
  370. $("input",td).width($t.width-4);
  371. } else {
  372. $("input",td).width($t.width-2);
  373. }
  374. }
  375. if(self.p.autosearch===true){
  376. $("input",td).keypress(function(e){
  377. var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
  378. if(key == 13){
  379. triggerSearch();
  380. return false;
  381. }
  382. return this;
  383. });
  384. }
  385. break;
  386. }
  387. if(self.p.formtype=='horizontal'){
  388. if(self.p.grodToolbar===true && self.p.gridNames===false) {
  389. $(tr).append(td);
  390. } else {
  391. $(tr).append(tl).append(td);
  392. }
  393. $(tr).append(td);
  394. } else {
  395. tr1 = document.createElement("tr");
  396. $(tr1).append(tl).append(td);
  397. $(tbl).append(tr1);
  398. }
  399. });
  400. td = document.createElement("td");
  401. if(self.p.enableSearch === true){
  402. sb = "<input type='button' id='sButton' class='"+self.p.buttonclass+"' value='"+self.p.searchButton+"'/>";
  403. $(td).append(sb);
  404. $("input#sButton",td).click(function(){
  405. triggerSearch();
  406. return false;
  407. });
  408. }
  409. if(self.p.enableClear === true) {
  410. cb = "<input type='button' id='cButton' class='"+self.p.buttonclass+"' value='"+self.p.clearButton+"'/>";
  411. $(td).append(cb);
  412. $("input#cButton",td).click(function(){
  413. clearSearch();
  414. return false;
  415. });
  416. }
  417. if(self.p.enableClear === true || self.p.enableSearch === true) {
  418. if(self.p.formtype=='horizontal') {
  419. $(tr).append(td);
  420. } else {
  421. tr1 = document.createElement("tr");
  422. $(tr1).append("<td>&nbsp;</td>").append(td);
  423. $(tbl).append(tr1);
  424. }
  425. }
  426. };
  427. var frm = $("<form name='SearchForm' style=display:inline;' class='"+this.p.formclass+"'></form>");
  428. var tbl =$("<table class='"+this.p.tableclass+"' cellspacing='0' cellpading='0' border='0'><tbody></tbody></table>");
  429. $(frm).append(tbl);
  430. formFill();
  431. $(this).append(frm);
  432. this.triggerSearch = function () {triggerSearch();};
  433. this.clearSearch = function () {clearSearch();};
  434. });
  435. }
  436. });
  437. })(jQuery);