PageRenderTime 48ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/app/jquery/jquery-jqgrid.custom.js

https://code.google.com/
JavaScript | 809 lines | 744 code | 21 blank | 44 comment | 213 complexity | 2cd0240120905d8850cb2a573e851a3f MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, BSD-2-Clause, LGPL-2.1, GPL-2.0, MIT
  1. (function($){
  2. /**
  3. * jqGrid extension for custom methods
  4. * Tony Tomov tony@trirand.com
  5. * http://trirand.com/blog/
  6. *
  7. * Wildraid wildraid@mail.ru
  8. * Oleg Kiriljuk oleg.kiriljuk@ok-soft-gmbh.com
  9. * Dual licensed under the MIT and GPL licenses:
  10. * http://www.opensource.org/licenses/mit-license.php
  11. * http://www.gnu.org/licenses/gpl-2.0.html
  12. **/
  13. /*global jQuery, $ */
  14. $.jgrid.extend({
  15. getColProp : function(colname){
  16. var ret ={}, $t = this[0];
  17. if ( !$t.grid ) { return false; }
  18. var cM = $t.p.colModel;
  19. for ( var i =0;i<cM.length;i++ ) {
  20. if ( cM[i].name == colname ) {
  21. ret = cM[i];
  22. break;
  23. }
  24. }
  25. return ret;
  26. },
  27. setColProp : function(colname, obj){
  28. //do not set width will not work
  29. return this.each(function(){
  30. if ( this.grid ) {
  31. if ( obj ) {
  32. var cM = this.p.colModel;
  33. for ( var i =0;i<cM.length;i++ ) {
  34. if ( cM[i].name == colname ) {
  35. $.extend(this.p.colModel[i],obj);
  36. break;
  37. }
  38. }
  39. }
  40. }
  41. });
  42. },
  43. sortGrid : function(colname,reload, sor){
  44. return this.each(function(){
  45. var $t=this,idx=-1;
  46. if ( !$t.grid ) { return;}
  47. if ( !colname ) { colname = $t.p.sortname; }
  48. for ( var i=0;i<$t.p.colModel.length;i++ ) {
  49. if ( $t.p.colModel[i].index == colname || $t.p.colModel[i].name==colname ) {
  50. idx = i;
  51. break;
  52. }
  53. }
  54. if ( idx!=-1 ){
  55. var sort = $t.p.colModel[idx].sortable;
  56. if ( typeof sort !== 'boolean' ) { sort = true; }
  57. if ( typeof reload !=='boolean' ) { reload = false; }
  58. if ( sort ) { $t.sortData("jqgh_"+$t.p.id+"_" + colname, idx, reload, sor); }
  59. }
  60. });
  61. },
  62. GridDestroy : function () {
  63. return this.each(function(){
  64. if ( this.grid ) {
  65. if ( this.p.pager ) { // if not part of grid
  66. $(this.p.pager).remove();
  67. }
  68. var gid = this.id;
  69. try {
  70. $("#gbox_"+gid).remove();
  71. } catch (_) {}
  72. }
  73. });
  74. },
  75. GridUnload : function(){
  76. return this.each(function(){
  77. if ( !this.grid ) {return;}
  78. var defgrid = {id: $(this).attr('id'),cl: $(this).attr('class')};
  79. if (this.p.pager) {
  80. $(this.p.pager).empty().removeClass("ui-state-default ui-jqgrid-pager corner-bottom");
  81. }
  82. var newtable = document.createElement('table');
  83. $(newtable).attr({id:defgrid.id});
  84. newtable.className = defgrid.cl;
  85. var gid = this.id;
  86. $(newtable).removeClass("ui-jqgrid-btable");
  87. if( $(this.p.pager).parents("#gbox_"+gid).length === 1 ) {
  88. $(newtable).insertBefore("#gbox_"+gid).show();
  89. $(this.p.pager).insertBefore("#gbox_"+gid);
  90. } else {
  91. $(newtable).insertBefore("#gbox_"+gid).show();
  92. }
  93. $("#gbox_"+gid).remove();
  94. });
  95. },
  96. setGridState : function(state) {
  97. return this.each(function(){
  98. if ( !this.grid ) {return;}
  99. var $t = this;
  100. if(state == 'hidden'){
  101. $(".ui-jqgrid-bdiv, .ui-jqgrid-hdiv","#gview_"+$t.p.id).slideUp("fast");
  102. if($t.p.pager) {$($t.p.pager).slideUp("fast");}
  103. if($t.p.toppager) {$($t.p.toppager).slideUp("fast");}
  104. if($t.p.toolbar[0]===true) {
  105. if( $t.p.toolbar[1]=='both') {
  106. $($t.grid.ubDiv).slideUp("fast");
  107. }
  108. $($t.grid.uDiv).slideUp("fast");
  109. }
  110. if($t.p.footerrow) { $(".ui-jqgrid-sdiv","#gbox_"+$t.p.id).slideUp("fast"); }
  111. $(".ui-jqgrid-titlebar-close span",$t.grid.cDiv).removeClass("ui-icon-circle-triangle-n").addClass("ui-icon-circle-triangle-s");
  112. $t.p.gridstate = 'hidden';
  113. } else if(state=='visible') {
  114. $(".ui-jqgrid-hdiv, .ui-jqgrid-bdiv","#gview_"+$t.p.id).slideDown("fast");
  115. if($t.p.pager) {$($t.p.pager).slideDown("fast");}
  116. if($t.p.toppager) {$($t.p.toppager).slideDown("fast");}
  117. if($t.p.toolbar[0]===true) {
  118. if( $t.p.toolbar[1]=='both') {
  119. $($t.grid.ubDiv).slideDown("fast");
  120. }
  121. $($t.grid.uDiv).slideDown("fast");
  122. }
  123. if($t.p.footerrow) { $(".ui-jqgrid-sdiv","#gbox_"+$t.p.id).slideDown("fast"); }
  124. $(".ui-jqgrid-titlebar-close span",$t.grid.cDiv).removeClass("ui-icon-circle-triangle-s").addClass("ui-icon-circle-triangle-n");
  125. $t.p.gridstate = 'visible';
  126. }
  127. });
  128. },
  129. filterToolbar : function(p){
  130. p = $.extend({
  131. autosearch: true,
  132. searchOnEnter : true,
  133. beforeSearch: null,
  134. afterSearch: null,
  135. beforeClear: null,
  136. afterClear: null,
  137. searchurl : '',
  138. stringResult: false,
  139. groupOp: 'AND',
  140. defaultSearch : "bw"
  141. },p || {});
  142. return this.each(function(){
  143. var $t = this;
  144. if(this.ftoolbar) { return; }
  145. var triggerToolbar = function() {
  146. var sdata={}, j=0, v, nm, sopt={},so;
  147. $.each($t.p.colModel,function(i,n){
  148. nm = this.index || this.name;
  149. so = (this.searchoptions && this.searchoptions.sopt) ? this.searchoptions.sopt[0] : this.stype=='select'? 'eq' : p.defaultSearch;
  150. v = $("#gs_"+$.jgrid.jqID(this.name), (this.frozen===true && $t.p.frozenColumns === true) ? $t.grid.fhDiv : $t.grid.hDiv).val();
  151. if(v) {
  152. sdata[nm] = v;
  153. sopt[nm] = so;
  154. j++;
  155. } else {
  156. try {
  157. delete $t.p.postData[nm];
  158. } catch (z) {}
  159. }
  160. });
  161. var sd = j>0 ? true : false;
  162. if(p.stringResult === true || $t.p.datatype == "local") {
  163. var ruleGroup = "{\"groupOp\":\"" + p.groupOp + "\",\"rules\":[";
  164. var gi=0;
  165. $.each(sdata,function(i,n){
  166. if (gi > 0) {ruleGroup += ",";}
  167. ruleGroup += "{\"field\":\"" + i + "\",";
  168. ruleGroup += "\"op\":\"" + sopt[i] + "\",";
  169. n+="";
  170. ruleGroup += "\"data\":\"" + n.replace(/\\/g,'\\\\').replace(/\"/g,'\\"') + "\"}";
  171. gi++;
  172. });
  173. ruleGroup += "]}";
  174. $.extend($t.p.postData,{filters:ruleGroup});
  175. $.each(['searchField', 'searchString', 'searchOper'], function(i, n){
  176. if($t.p.postData.hasOwnProperty(n)) { delete $t.p.postData[n];}
  177. });
  178. } else {
  179. $.extend($t.p.postData,sdata);
  180. }
  181. var saveurl;
  182. if($t.p.searchurl) {
  183. saveurl = $t.p.url;
  184. $($t).jqGrid("setGridParam",{url:$t.p.searchurl});
  185. }
  186. var bsr = false;
  187. if($.isFunction(p.beforeSearch)){bsr = p.beforeSearch.call($t);}
  188. if(!bsr) { $($t).jqGrid("setGridParam",{search:sd}).trigger("reloadGrid",[{page:1}]); }
  189. if(saveurl) {$($t).jqGrid("setGridParam",{url:saveurl});}
  190. if($.isFunction(p.afterSearch)){p.afterSearch();}
  191. };
  192. var clearToolbar = function(trigger){
  193. var sdata={}, v, j=0, nm;
  194. trigger = (typeof trigger != 'boolean') ? true : trigger;
  195. $.each($t.p.colModel,function(i,n){
  196. v = (this.searchoptions && this.searchoptions.defaultValue) ? this.searchoptions.defaultValue : "";
  197. nm = this.index || this.name;
  198. switch (this.stype) {
  199. case 'select' :
  200. var v1;
  201. $("#gs_"+$.jgrid.jqID(this.name)+" option",(this.frozen===true && $t.p.frozenColumns === true) ? $t.grid.fhDiv : $t.grid.hDiv).each(function (i){
  202. if(i===0) { this.selected = true; }
  203. if ($(this).text() == v) {
  204. this.selected = true;
  205. v1 = $(this).val();
  206. return false;
  207. }
  208. });
  209. if (v1) {
  210. // post the key and not the text
  211. sdata[nm] = v1;
  212. j++;
  213. } else {
  214. try {
  215. delete $t.p.postData[nm];
  216. } catch(e) {}
  217. }
  218. break;
  219. case 'text':
  220. $("#gs_"+$.jgrid.jqID(this.name),(this.frozen===true && $t.p.frozenColumns === true) ? $t.grid.fhDiv : $t.grid.hDiv).val(v);
  221. if(v) {
  222. sdata[nm] = v;
  223. j++;
  224. } else {
  225. try {
  226. delete $t.p.postData[nm];
  227. } catch (y){}
  228. }
  229. break;
  230. }
  231. });
  232. var sd = j>0 ? true : false;
  233. if(p.stringResult === true || $t.p.datatype == "local") {
  234. var ruleGroup = "{\"groupOp\":\"" + p.groupOp + "\",\"rules\":[";
  235. var gi=0;
  236. $.each(sdata,function(i,n){
  237. if (gi > 0) {ruleGroup += ",";}
  238. ruleGroup += "{\"field\":\"" + i + "\",";
  239. ruleGroup += "\"op\":\"" + "eq" + "\",";
  240. n+="";
  241. ruleGroup += "\"data\":\"" + n.replace(/\\/g,'\\\\').replace(/\"/g,'\\"') + "\"}";
  242. gi++;
  243. });
  244. ruleGroup += "]}";
  245. $.extend($t.p.postData,{filters:ruleGroup});
  246. $.each(['searchField', 'searchString', 'searchOper'], function(i, n){
  247. if($t.p.postData.hasOwnProperty(n)) { delete $t.p.postData[n];}
  248. });
  249. } else {
  250. $.extend($t.p.postData,sdata);
  251. }
  252. var saveurl;
  253. if($t.p.searchurl) {
  254. saveurl = $t.p.url;
  255. $($t).jqGrid("setGridParam",{url:$t.p.searchurl});
  256. }
  257. var bcv = false;
  258. if($.isFunction(p.beforeClear)){bcv = p.beforeClear.call($t);}
  259. if(!bcv) {
  260. if(trigger) {
  261. $($t).jqGrid("setGridParam",{search:sd}).trigger("reloadGrid",[{page:1}]);
  262. }
  263. }
  264. if(saveurl) {$($t).jqGrid("setGridParam",{url:saveurl});}
  265. if($.isFunction(p.afterClear)){p.afterClear();}
  266. };
  267. var toggleToolbar = function(){
  268. var trow = $("tr.ui-search-toolbar",$t.grid.hDiv),
  269. trow2 = $t.p.frozenColumns === true ? $("tr.ui-search-toolbar",$t.grid.hDiv) : false;
  270. if(trow.css("display")=='none') {
  271. trow.show();
  272. if(trow2) {
  273. trow2.show();
  274. }
  275. } else {
  276. trow.hide();
  277. if(trow2) {
  278. trow2.hide();
  279. }
  280. }
  281. };
  282. // create the row
  283. function bindEvents(selector, events) {
  284. var jElem = $(selector);
  285. if (jElem[0]) {
  286. jQuery.each(events, function() {
  287. if (this.data !== undefined) {
  288. jElem.bind(this.type, this.data, this.fn);
  289. } else {
  290. jElem.bind(this.type, this.fn);
  291. }
  292. });
  293. }
  294. }
  295. var tr = $("<tr class='ui-search-toolbar' role='rowheader'></tr>");
  296. var timeoutHnd;
  297. $.each($t.p.colModel,function(i,n){
  298. var cm=this, thd , th, soptions,surl,self;
  299. th = $("<th role='columnheader' class='ui-state-default ui-th-column ui-th-"+$t.p.direction+"'></th>");
  300. thd = $("<div style='width:100%;position:relative;height:100%;padding-right:0.3em;'></div>");
  301. if(this.hidden===true) { $(th).css("display","none");}
  302. this.search = this.search === false ? false : true;
  303. if(typeof this.stype == 'undefined' ) {this.stype='text';}
  304. soptions = $.extend({},this.searchoptions || {});
  305. if(this.search){
  306. switch (this.stype)
  307. {
  308. case "select":
  309. surl = this.surl || soptions.dataUrl;
  310. if(surl) {
  311. // data returned should have already constructed html select
  312. // primitive jQuery load
  313. self = thd;
  314. $.ajax($.extend({
  315. url: surl,
  316. dataType: "html",
  317. success: function(res,status) {
  318. if(soptions.buildSelect !== undefined) {
  319. var d = soptions.buildSelect(res);
  320. if (d) { $(self).append(d); }
  321. } else {
  322. $(self).append(res);
  323. }
  324. if(soptions.defaultValue) { $("select",self).val(soptions.defaultValue); }
  325. $("select",self).attr({name:cm.index || cm.name, id: "gs_"+cm.name});
  326. if(soptions.attr) {$("select",self).attr(soptions.attr);}
  327. $("select",self).css({width: "100%"});
  328. // preserve autoserch
  329. if(soptions.dataInit !== undefined) { soptions.dataInit($("select",self)[0]); }
  330. if(soptions.dataEvents !== undefined) { bindEvents($("select",self)[0],soptions.dataEvents); }
  331. if(p.autosearch===true){
  332. $("select",self).change(function(e){
  333. triggerToolbar();
  334. return false;
  335. });
  336. }
  337. res=null;
  338. }
  339. }, $.jgrid.ajaxOptions, $t.p.ajaxSelectOptions || {} ));
  340. } else {
  341. var oSv;
  342. if(cm.searchoptions && cm.searchoptions.value) {
  343. oSv = cm.searchoptions.value;
  344. } else if(cm.editoptions && cm.editoptions.value) {
  345. oSv = cm.editoptions.value;
  346. }
  347. if (oSv) {
  348. var elem = document.createElement("select");
  349. elem.style.width = "100%";
  350. $(elem).attr({name:cm.index || cm.name, id: "gs_"+cm.name});
  351. var so, sv, ov;
  352. if(typeof oSv === "string") {
  353. so = oSv.split(";");
  354. for(var k=0; k<so.length;k++){
  355. sv = so[k].split(":");
  356. ov = document.createElement("option");
  357. ov.value = sv[0]; ov.innerHTML = sv[1];
  358. elem.appendChild(ov);
  359. }
  360. } else if(typeof oSv === "object" ) {
  361. for ( var key in oSv) {
  362. if(oSv.hasOwnProperty(key)) {
  363. ov = document.createElement("option");
  364. ov.value = key; ov.innerHTML = oSv[key];
  365. elem.appendChild(ov);
  366. }
  367. }
  368. }
  369. if(soptions.defaultValue) { $(elem).val(soptions.defaultValue); }
  370. if(soptions.attr) {$(elem).attr(soptions.attr);}
  371. if(soptions.dataInit !== undefined) { soptions.dataInit(elem); }
  372. if(soptions.dataEvents !== undefined) { bindEvents(elem, soptions.dataEvents); }
  373. $(thd).append(elem);
  374. if(p.autosearch===true){
  375. $(elem).change(function(e){
  376. triggerToolbar();
  377. return false;
  378. });
  379. }
  380. }
  381. }
  382. break;
  383. case 'text':
  384. var df = soptions.defaultValue ? soptions.defaultValue: "";
  385. $(thd).append("<input type='text' style='width:95%;padding:0px;' name='"+(cm.index || cm.name)+"' id='gs_"+cm.name+"' value='"+df+"'/>");
  386. if(soptions.attr) {$("input",thd).attr(soptions.attr);}
  387. if(soptions.dataInit !== undefined) { soptions.dataInit($("input",thd)[0]); }
  388. if(soptions.dataEvents !== undefined) { bindEvents($("input",thd)[0], soptions.dataEvents); }
  389. if(p.autosearch===true){
  390. if(p.searchOnEnter) {
  391. $("input",thd).keypress(function(e){
  392. var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
  393. if(key == 13){
  394. triggerToolbar();
  395. return false;
  396. }
  397. return this;
  398. });
  399. } else {
  400. $("input",thd).keydown(function(e){
  401. var key = e.which;
  402. switch (key) {
  403. case 13:
  404. return false;
  405. case 9 :
  406. case 16:
  407. case 37:
  408. case 38:
  409. case 39:
  410. case 40:
  411. case 27:
  412. break;
  413. default :
  414. if(timeoutHnd) { clearTimeout(timeoutHnd); }
  415. timeoutHnd = setTimeout(function(){triggerToolbar();},500);
  416. }
  417. });
  418. }
  419. }
  420. break;
  421. }
  422. }
  423. $(th).append(thd);
  424. $(tr).append(th);
  425. });
  426. $("table thead",$t.grid.hDiv).append(tr);
  427. this.ftoolbar = true;
  428. this.triggerToolbar = triggerToolbar;
  429. this.clearToolbar = clearToolbar;
  430. this.toggleToolbar = toggleToolbar;
  431. });
  432. },
  433. destroyGroupHeader : function(nullHeader)
  434. {
  435. if(typeof(nullHeader) == 'undefined') {
  436. nullHeader = true;
  437. }
  438. return this.each(function()
  439. {
  440. var $t = this, $tr, i, l, headers, $th, $resizing, grid = $t.grid,
  441. thead = $("table.ui-jqgrid-htable thead", grid.hDiv), cm = $t.p.colModel, hc;
  442. if(!grid) return;
  443. $tr = $("<tr>", {role: "rowheader"}).addClass("ui-jqgrid-labels");
  444. headers = grid.headers;
  445. for (i = 0, l = headers.length; i < l; i++) {
  446. hc = cm[i].hidden ? "none" : "";
  447. $th = $(headers[i].el)
  448. .width(headers[i].width)
  449. .css('display',hc);
  450. try {
  451. $th.removeAttr("rowSpan");
  452. } catch (rs) {
  453. //IE 6/7
  454. $th.attr("rowSpan",1);
  455. }
  456. $tr.append($th);
  457. $resizing = $th.children("span.ui-jqgrid-resize");
  458. if ($resizing.length>0) {// resizable column
  459. $resizing[0].style.height = "";
  460. }
  461. $th.children("div")[0].style.top = "";
  462. }
  463. $(thead).children('tr.ui-jqgrid-labels').remove();
  464. $(thead).prepend($tr);
  465. if(nullHeader === true) {
  466. $($t).jqGrid('setGridParam',{ 'groupHeader': null});
  467. }
  468. });
  469. },
  470. setGroupHeaders : function ( o ) {
  471. o = $.extend({
  472. useColSpanStyle : false,
  473. groupHeaders: []
  474. },o || {});
  475. return this.each(function(){
  476. this.p.groupHeader = o;
  477. var ts = this,
  478. i, cmi, skip = 0, $tr, $colHeader, th, $th, thStyle,
  479. iCol,
  480. cghi,
  481. //startColumnName,
  482. numberOfColumns,
  483. titleText,
  484. cVisibleColumns,
  485. colModel = ts.p.colModel,
  486. cml = colModel.length,
  487. ths = ts.grid.headers,
  488. $htable = $("table.ui-jqgrid-htable", ts.grid.hDiv),
  489. $trLabels = $htable.children("thead").children("tr.ui-jqgrid-labels:last").addClass("jqg-second-row-header"),
  490. $thead = $htable.children("thead"),
  491. $theadInTable,
  492. originalResizeStop,
  493. $firstHeaderRow = $htable.find(".jqg-first-row-header");
  494. if($firstHeaderRow.html() === null) {
  495. $firstHeaderRow = $('<tr>', {role: "row", "aria-hidden": "true"}).addClass("jqg-first-row-header").css("height", "auto");
  496. } else {
  497. $firstHeaderRow.empty();
  498. }
  499. var $firstRow,
  500. inColumnHeader = function (text, columnHeaders) {
  501. var i = 0, length = columnHeaders.length;
  502. for (; i < length; i++) {
  503. if (columnHeaders[i].startColumnName === text) {
  504. return i;
  505. }
  506. }
  507. return -1;
  508. };
  509. $(ts).prepend($thead);
  510. $tr = $('<tr>', {role: "rowheader"}).addClass("ui-jqgrid-labels jqg-third-row-header");
  511. for (i = 0; i < cml; i++) {
  512. th = ths[i].el;
  513. $th = $(th);
  514. cmi = colModel[i];
  515. // build the next cell for the first header row
  516. thStyle = { height: '0px', width: ths[i].width + 'px', display: (cmi.hidden ? 'none' : '')};
  517. $("<th>", {role: 'gridcell'}).css(thStyle).addClass("ui-first-th-"+ts.p.direction).appendTo($firstHeaderRow);
  518. th.style.width = ""; // remove unneeded style
  519. iCol = inColumnHeader(cmi.name, o.groupHeaders);
  520. if (iCol >= 0) {
  521. cghi = o.groupHeaders[iCol];
  522. numberOfColumns = cghi.numberOfColumns;
  523. titleText = cghi.titleText;
  524. // caclulate the number of visible columns from the next numberOfColumns columns
  525. for (cVisibleColumns = 0, iCol = 0; iCol < numberOfColumns && (i + iCol < cml); iCol++) {
  526. if (!colModel[i + iCol].hidden) {
  527. cVisibleColumns++;
  528. }
  529. }
  530. // The next numberOfColumns headers will be moved in the next row
  531. // in the current row will be placed the new column header with the titleText.
  532. // The text will be over the cVisibleColumns columns
  533. $colHeader = $('<th>').attr({role: "columnheader"})
  534. .addClass("ui-state-default ui-th-column-header ui-th-"+ts.p.direction)
  535. .css({'height':'22px', 'border-top': '0px none'})
  536. .html(titleText);
  537. if(cVisibleColumns > 0) {
  538. $colHeader.attr("colspan", String(cVisibleColumns));
  539. }
  540. if (ts.p.headertitles) {
  541. $colHeader.attr("title", $colHeader.text());
  542. }
  543. // hide if not a visible cols
  544. if( cVisibleColumns === 0) {
  545. $colHeader.hide();
  546. }
  547. $th.before($colHeader); // insert new column header before the current
  548. $tr.append(th); // move the current header in the next row
  549. // set the coumter of headers which will be moved in the next row
  550. skip = numberOfColumns - 1;
  551. } else {
  552. if (skip === 0) {
  553. if (o.useColSpanStyle) {
  554. // expand the header height to two rows
  555. $th.attr("rowspan", "2");
  556. } else {
  557. $('<th>', {role: "columnheader"})
  558. .addClass("ui-state-default ui-th-column-header ui-th-"+ts.p.direction)
  559. .css({"display": cmi.hidden ? 'none' : '', 'border-top': '0px none'})
  560. .insertBefore($th);
  561. $tr.append(th);
  562. }
  563. } else {
  564. // move the header to the next row
  565. //$th.css({"padding-top": "2px", height: "19px"});
  566. $tr.append(th);
  567. skip--;
  568. }
  569. }
  570. }
  571. $theadInTable = $(ts).children("thead");
  572. $theadInTable.prepend($firstHeaderRow);
  573. $tr.insertAfter($trLabels);
  574. $htable.append($theadInTable);
  575. if (o.useColSpanStyle) {
  576. // Increase the height of resizing span of visible headers
  577. $htable.find("span.ui-jqgrid-resize").each(function () {
  578. var $parent = $(this).parent();
  579. if ($parent.is(":visible")) {
  580. this.style.cssText = 'height: ' + $parent.height() + 'px !important; cursor: col-resize;';
  581. }
  582. });
  583. // Set position of the sortable div (the main lable)
  584. // with the column header text to the middle of the cell.
  585. // One should not do this for hidden headers.
  586. $htable.find("div.ui-jqgrid-sortable").each(function () {
  587. var $ts = $(this), $parent = $ts.parent();
  588. if ($parent.is(":visible") && $parent.is(":has(span.ui-jqgrid-resize)")) {
  589. $ts.css('top', ($parent.height() - $ts.outerHeight()) / 2 + 'px');
  590. }
  591. });
  592. }
  593. // Preserve original resizeStop event if any defined
  594. if ($.isFunction(ts.p.resizeStop)) {
  595. originalResizeStop = ts.p.resizeStop;
  596. }
  597. $firstRow = $theadInTable.find("tr.jqg-first-row-header");
  598. ts.p.resizeStop = function (nw, idx) {
  599. $firstRow.find('th').eq(idx).width(nw);
  600. if ($.isFunction(originalResizeStop)) {
  601. originalResizeStop.call(ts, nw, idx);
  602. }
  603. };
  604. });
  605. },
  606. setFrozenColumns : function () {
  607. return this.each(function() {
  608. if ( !this.grid ) {return;}
  609. var $t = this, cm = $t.p.colModel,i=0, len = cm.length, maxfrozen = -1, frozen= false;
  610. // TODO treeGrid and grouping Support
  611. if($t.p.subGrid == true || $t.p.treeGrid === true || $t.p.cellEdit == true || $t.p.sortable || $t.p.scroll || $t.p.grouping )
  612. {
  613. return;
  614. }
  615. if($t.p.rownumbers) { i++; }
  616. if($t.p.multiselect) { i++; }
  617. // get the max index of frozen col
  618. while(i<len)
  619. {
  620. // from left, no breaking frozen
  621. if(cm[i].frozen === true)
  622. {
  623. frozen = true;
  624. maxfrozen = i;
  625. } else {
  626. break;
  627. }
  628. i++;
  629. }
  630. if( maxfrozen>=0 && frozen) {
  631. var top = $t.p.caption ? $($t.grid.cDiv).outerHeight() : 0,
  632. hth = $(".ui-jqgrid-htable","#gview_"+$.jgrid.jqID($t.p.id)).height();
  633. $t.p.orgEvents = {};
  634. //headers
  635. if($t.p.toppager) {
  636. top = top + $($t.grid.topDiv).outerHeight();
  637. }
  638. if($t.p.toolbar[0] == true) {
  639. if($t.p.toolbar[1] != "bottom") {
  640. top = top + $($t.grid.uDiv).outerHeight();
  641. }
  642. }
  643. $t.grid.fhDiv = $('<div style="position:absolute;left:0px;top:'+top+'px;height:'+hth+'px;" class="frozen-div ui-state-default ui-jqgrid-hdiv"></div>');
  644. $t.grid.fbDiv = $('<div style="position:absolute;left:0px;top:'+(parseInt(top,10)+parseInt(hth,10) + 1)+'px;overflow-y:hidden" class="frozen-bdiv ui-jqgrid-bdiv"></div>');
  645. $("#gview_"+$.jgrid.jqID($t.p.id)).append($t.grid.fhDiv);
  646. var htbl = $(".ui-jqgrid-htable","#gview_"+$.jgrid.jqID($t.p.id)).clone(true);
  647. // groupheader support - only if useColSpanstyle is false
  648. if($t.p.groupHeader) {
  649. $("tr.jqg-first-row-header, tr.jqg-third-row-header", htbl).each(function(){
  650. $("th:gt("+maxfrozen+")",this).remove();
  651. });
  652. var swapfroz = -1, fdel = -1;
  653. $("tr.jqg-second-row-header th", htbl).each(function( i ){
  654. var cs= parseInt($(this).attr("colspan"),10);
  655. if(cs) {
  656. swapfroz = swapfroz+cs;
  657. fdel++;
  658. }
  659. if(swapfroz === maxfrozen) {
  660. return false;
  661. }
  662. });
  663. if(swapfroz !== maxfrozen) {
  664. fdel = maxfrozen;
  665. }
  666. $("tr.jqg-second-row-header", htbl).each(function( i ){
  667. $("th:gt("+fdel+")",this).remove();
  668. });
  669. } else {
  670. $("tr",htbl).each(function(){
  671. $("th:gt("+maxfrozen+")",this).remove();
  672. });
  673. }
  674. $(htbl).width(1);
  675. // resizing stuff
  676. $($t.grid.fhDiv).append(htbl)
  677. .mousemove(function (e) {
  678. if($t.grid.resizing){ $t.grid.dragMove(e);return false; }
  679. });
  680. if ($.isFunction($t.p.resizeStop)) {
  681. $t.p.orgEvents.resizeStop = $t.p.resizeStop;
  682. }
  683. $t.p.resizeStop = function(w, index)
  684. {
  685. var rhth = $(".ui-jqgrid-htable",$t.grid.fhDiv);
  686. $("th:eq("+index+")",rhth).width( w );
  687. var btd = $(".ui-jqgrid-btable",$t.grid.fbDiv);
  688. $("tr:first td:eq("+index+")",btd).width( w );
  689. if ($.isFunction($t.p.orgEvents.resizeStop)) {
  690. $t.p.orgEvents.resizeStop.call($t, w, index);
  691. } else {
  692. $t.p.orgEvents.resizeStop = null;
  693. }
  694. };
  695. // sorting stuff
  696. if($.isFunction( $t.p.onSortCol)) {
  697. $t.p.orgEvents.onSortCol = $t.p.onSortCol;
  698. } else {
  699. $t.p.orgEvents.onSortCol = null;
  700. }
  701. $t.p.onSortCol = function( index,idxcol,so ){
  702. var previousSelectedTh = $("tr.ui-jqgrid-labels:last th:eq("+$t.p.lastsort+")",$t.grid.fhDiv), newSelectedTh = $("tr.ui-jqgrid-labels:last th:eq("+idxcol+")",$t.grid.fhDiv);
  703. $("span.ui-grid-ico-sort",previousSelectedTh).addClass('ui-state-disabled');
  704. $(previousSelectedTh).attr("aria-selected","false");
  705. $("span.ui-icon-"+$t.p.sortorder,newSelectedTh).removeClass('ui-state-disabled');
  706. $(newSelectedTh).attr("aria-selected","true");
  707. if(!$t.p.viewsortcols[0]) {
  708. if($t.p.lastsort != idxcol) {
  709. $("span.s-ico",previousSelectedTh).hide();
  710. $("span.s-ico",newSelectedTh).show();
  711. }
  712. }
  713. if( $.isFunction($t.p.orgEvents.onSortCol) ) {
  714. $t.p.orgEvents.onSortCol.call($t,index,idxcol,so);
  715. }
  716. };
  717. // data stuff
  718. //TODO support for setRowData
  719. $("#gview_"+$.jgrid.jqID($t.p.id)).append($t.grid.fbDiv);
  720. jQuery($t.grid.bDiv).scroll(function () {
  721. jQuery($t.grid.fbDiv).scrollTop(jQuery(this).scrollTop());
  722. });
  723. if ($.isFunction($t.p._complete)) {
  724. $t.p.orgEvents.complete = $t.p._complete;
  725. } else {
  726. $t.p.orgEvents.complete = null;
  727. }
  728. if($t.p.hoverrows === true) {
  729. $("#"+$.jgrid.jqID($t.p.id)).unbind('mouseover').unbind('mouseout');
  730. }
  731. $t.p._complete = function() {
  732. $("#"+$.jgrid.jqID($t.p.id)+"_frozen").remove();
  733. jQuery($t.grid.fbDiv).height( jQuery($t.grid.bDiv).height()-16);
  734. var btbl = $("#"+$.jgrid.jqID($t.p.id)).clone(true);
  735. $("tr",btbl).each(function(){
  736. $("td:gt("+maxfrozen+")",this).remove();
  737. });
  738. $(btbl).width(1).attr("id",$.jgrid.jqID($t.p.id)+"_frozen");
  739. $($t.grid.fbDiv).append(btbl);
  740. if($t.p.hoverrows === true) {
  741. $("tr.jqgrow", btbl).hover(
  742. function(){ $(this).addClass("ui-state-hover"); $("#"+$.jgrid.jqID(this.id), "#"+$.jgrid.jqID($t.p.id)).addClass("ui-state-hover") },
  743. function(){ $(this).removeClass("ui-state-hover"); $("#"+$.jgrid.jqID(this.id), "#"+$.jgrid.jqID($t.p.id)).removeClass("ui-state-hover") }
  744. );
  745. $("tr.jqgrow", "#"+$.jgrid.jqID($t.p.id)).hover(
  746. function(){ $(this).addClass("ui-state-hover"); $("#"+$.jgrid.jqID(this.id), "#"+$.jgrid.jqID($t.p.id)+"_frozen").addClass("ui-state-hover");},
  747. function(){ $(this).removeClass("ui-state-hover"); $("#"+$.jgrid.jqID(this.id), "#"+$.jgrid.jqID($t.p.id)+"_frozen").removeClass("ui-state-hover"); }
  748. );
  749. }
  750. btbl=null;
  751. if($.isFunction($t.p.orgEvents.complete)) {
  752. $t.p.orgEvents.complete.call($t);
  753. }
  754. };
  755. $t.p.frozenColumns = true;
  756. }
  757. });
  758. },
  759. destroyFrozenColumns : function() {
  760. return this.each(function() {
  761. if ( !this.grid ) {return;}
  762. if(this.p.frozenColumns === true) {
  763. var $t = this;
  764. $($t.grid.fhDiv).remove();
  765. $($t.grid.fbDiv).remove();
  766. $t.grid.fhDiv = null; $t.grid.fbDiv=null;
  767. $t.p._complete = $t.p.orgEvents.complete;
  768. $t.p.resizeStop = $t.p.orgEvents.resizeStop;
  769. $t.p.onSortCol = $t.p.orgEvents.onSortCol;
  770. $t.p.orgEvents = null;
  771. if($t.p.hoverrows == true) {
  772. var ptr;
  773. $("#"+$.jgrid.jqID($t.p.id)).bind('mouseover',function(e) {
  774. ptr = $(e.target).closest("tr.jqgrow");
  775. if($(ptr).attr("class") !== "ui-subgrid") {
  776. $(ptr).addClass("ui-state-hover");
  777. }
  778. }).bind('mouseout',function(e) {
  779. ptr = $(e.target).closest("tr.jqgrow");
  780. $(ptr).removeClass("ui-state-hover");
  781. });
  782. }
  783. this.p.frozenColumns = false;
  784. }
  785. });
  786. }
  787. });
  788. })(jQuery);