grid.treegrid.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /**
  2. * jqGrid extension - Tree Grid
  3. * Tony Tomov tony@trirand.com
  4. * http://trirand.com/blog/
  5. * Dual licensed under the MIT and GPL licenses:
  6. * http://www.opensource.org/licenses/mit-license.php
  7. * http://www.gnu.org/licenses/gpl.html
  8. **/
  9. /*jshint eqeqeq:false */
  10. /*global jQuery */
  11. (function($) {
  12. "use strict";
  13. $.jgrid.extend({
  14. setTreeNode : function(i, len){
  15. return this.each(function(){
  16. var $t = this;
  17. if( !$t.grid || !$t.p.treeGrid ) {return;}
  18. var expCol = $t.p.expColInd,
  19. expanded = $t.p.treeReader.expanded_field,
  20. isLeaf = $t.p.treeReader.leaf_field,
  21. level = $t.p.treeReader.level_field,
  22. icon = $t.p.treeReader.icon_field,
  23. loaded = $t.p.treeReader.loaded, lft, rgt, curLevel, ident,lftpos, twrap,
  24. ldat, lf;
  25. while(i<len) {
  26. var ind = $.jgrid.stripPref($t.p.idPrefix, $t.rows[i].id), dind = $t.p._index[ind], expan;
  27. ldat = $t.p.data[dind];
  28. //$t.rows[i].level = ldat[level];
  29. if($t.p.treeGridModel === 'nested') {
  30. if(!ldat[isLeaf]) {
  31. lft = parseInt(ldat[$t.p.treeReader.left_field],10);
  32. rgt = parseInt(ldat[$t.p.treeReader.right_field],10);
  33. // NS Model
  34. ldat[isLeaf] = (rgt === lft+1) ? 'true' : 'false';
  35. $t.rows[i].cells[$t.p._treeleafpos].innerHTML = ldat[isLeaf];
  36. }
  37. }
  38. //else {
  39. //row.parent_id = rd[$t.p.treeReader.parent_id_field];
  40. //}
  41. curLevel = parseInt(ldat[level],10);
  42. if($t.p.tree_root_level === 0) {
  43. ident = curLevel+1;
  44. lftpos = curLevel;
  45. } else {
  46. ident = curLevel;
  47. lftpos = curLevel -1;
  48. }
  49. twrap = "<div class='tree-wrap tree-wrap-"+$t.p.direction+"' style='width:"+(ident*18)+"px;'>";
  50. twrap += "<div style='"+($t.p.direction==="rtl" ? "right:" : "left:")+(lftpos*18)+"px;' class='ui-icon ";
  51. if(ldat[loaded] !== undefined) {
  52. if(ldat[loaded]==="true" || ldat[loaded]===true) {
  53. ldat[loaded] = true;
  54. } else {
  55. ldat[loaded] = false;
  56. }
  57. }
  58. if(ldat[isLeaf] === "true" || ldat[isLeaf] === true) {
  59. twrap += ((ldat[icon] !== undefined && ldat[icon] !== "") ? ldat[icon] : $t.p.treeIcons.leaf)+" tree-leaf treeclick";
  60. ldat[isLeaf] = true;
  61. lf="leaf";
  62. } else {
  63. ldat[isLeaf] = false;
  64. lf="";
  65. }
  66. ldat[expanded] = ((ldat[expanded] === "true" || ldat[expanded] === true) ? true : false) && (ldat[loaded] || ldat[loaded] === undefined);
  67. if(ldat[expanded] === false) {
  68. twrap += ((ldat[isLeaf] === true) ? "'" : $t.p.treeIcons.plus+" tree-plus treeclick'");
  69. } else {
  70. twrap += ((ldat[isLeaf] === true) ? "'" : $t.p.treeIcons.minus+" tree-minus treeclick'");
  71. }
  72. twrap += "></div></div>";
  73. $($t.rows[i].cells[expCol]).wrapInner("<span class='cell-wrapper"+lf+"'></span>").prepend(twrap);
  74. if(curLevel !== parseInt($t.p.tree_root_level,10)) {
  75. var pn = $($t).jqGrid('getNodeParent',ldat);
  76. expan = pn && pn.hasOwnProperty(expanded) ? pn[expanded] : true;
  77. if( !expan ){
  78. $($t.rows[i]).css("display","none");
  79. }
  80. }
  81. $($t.rows[i].cells[expCol])
  82. .find("div.treeclick")
  83. .bind("click",function(e){
  84. var target = e.target || e.srcElement,
  85. ind2 =$.jgrid.stripPref($t.p.idPrefix,$(target,$t.rows).closest("tr.jqgrow")[0].id),
  86. pos = $t.p._index[ind2];
  87. if(!$t.p.data[pos][isLeaf]){
  88. if($t.p.data[pos][expanded]){
  89. $($t).jqGrid("collapseRow",$t.p.data[pos]);
  90. $($t).jqGrid("collapseNode",$t.p.data[pos]);
  91. } else {
  92. $($t).jqGrid("expandRow",$t.p.data[pos]);
  93. $($t).jqGrid("expandNode",$t.p.data[pos]);
  94. }
  95. }
  96. return false;
  97. });
  98. if($t.p.ExpandColClick === true) {
  99. $($t.rows[i].cells[expCol])
  100. .find("span.cell-wrapper")
  101. .css("cursor","pointer")
  102. .bind("click",function(e) {
  103. var target = e.target || e.srcElement,
  104. ind2 =$.jgrid.stripPref($t.p.idPrefix,$(target,$t.rows).closest("tr.jqgrow")[0].id),
  105. pos = $t.p._index[ind2];
  106. if(!$t.p.data[pos][isLeaf]){
  107. if($t.p.data[pos][expanded]){
  108. $($t).jqGrid("collapseRow",$t.p.data[pos]);
  109. $($t).jqGrid("collapseNode",$t.p.data[pos]);
  110. } else {
  111. $($t).jqGrid("expandRow",$t.p.data[pos]);
  112. $($t).jqGrid("expandNode",$t.p.data[pos]);
  113. }
  114. }
  115. $($t).jqGrid("setSelection",ind2);
  116. return false;
  117. });
  118. }
  119. i++;
  120. }
  121. });
  122. },
  123. setTreeGrid : function() {
  124. return this.each(function (){
  125. var $t = this, i=0, pico, ecol = false, nm, key, tkey, dupcols=[];
  126. if(!$t.p.treeGrid) {return;}
  127. if(!$t.p.treedatatype ) {$.extend($t.p,{treedatatype: $t.p.datatype});}
  128. $t.p.subGrid = false;$t.p.altRows =false;
  129. $t.p.pgbuttons = false;$t.p.pginput = false;
  130. $t.p.gridview = true;
  131. if($t.p.rowTotal === null ) { $t.p.rowNum = 10000; }
  132. $t.p.multiselect = false;$t.p.rowList = [];
  133. $t.p.expColInd = 0;
  134. pico = 'ui-icon-triangle-1-' + ($t.p.direction==="rtl" ? 'w' : 'e');
  135. $t.p.treeIcons = $.extend({plus:pico,minus:'ui-icon-triangle-1-s',leaf:'ui-icon-radio-off'},$t.p.treeIcons || {});
  136. if($t.p.treeGridModel === 'nested') {
  137. $t.p.treeReader = $.extend({
  138. level_field: "level",
  139. left_field:"lft",
  140. right_field: "rgt",
  141. leaf_field: "isLeaf",
  142. expanded_field: "expanded",
  143. loaded: "loaded",
  144. icon_field: "icon"
  145. },$t.p.treeReader);
  146. } else if($t.p.treeGridModel === 'adjacency') {
  147. $t.p.treeReader = $.extend({
  148. level_field: "level",
  149. parent_id_field: "parent",
  150. leaf_field: "isLeaf",
  151. expanded_field: "expanded",
  152. loaded: "loaded",
  153. icon_field: "icon"
  154. },$t.p.treeReader );
  155. }
  156. for ( key in $t.p.colModel){
  157. if($t.p.colModel.hasOwnProperty(key)) {
  158. nm = $t.p.colModel[key].name;
  159. if( nm === $t.p.ExpandColumn && !ecol ) {
  160. ecol = true;
  161. $t.p.expColInd = i;
  162. }
  163. i++;
  164. //
  165. for(tkey in $t.p.treeReader) {
  166. if($t.p.treeReader.hasOwnProperty(tkey) && $t.p.treeReader[tkey] === nm) {
  167. dupcols.push(nm);
  168. }
  169. }
  170. }
  171. }
  172. $.each($t.p.treeReader,function(j,n){
  173. if(n && $.inArray(n, dupcols) === -1){
  174. if(j==='leaf_field') { $t.p._treeleafpos= i; }
  175. i++;
  176. $t.p.colNames.push(n);
  177. $t.p.colModel.push({name:n,width:1,hidden:true,sortable:false,resizable:false,hidedlg:true,editable:true,search:false});
  178. }
  179. });
  180. });
  181. },
  182. expandRow: function (record){
  183. this.each(function(){
  184. var $t = this;
  185. if(!$t.grid || !$t.p.treeGrid) {return;}
  186. var childern = $($t).jqGrid("getNodeChildren",record),
  187. //if ($($t).jqGrid("isVisibleNode",record)) {
  188. expanded = $t.p.treeReader.expanded_field;
  189. $(childern).each(function(){
  190. var id = $t.p.idPrefix + $.jgrid.getAccessor(this,$t.p.localReader.id);
  191. $($($t).jqGrid('getGridRowById', id)).css("display","");
  192. if(this[expanded]) {
  193. $($t).jqGrid("expandRow",this);
  194. }
  195. });
  196. //}
  197. });
  198. },
  199. collapseRow : function (record) {
  200. this.each(function(){
  201. var $t = this;
  202. if(!$t.grid || !$t.p.treeGrid) {return;}
  203. var childern = $($t).jqGrid("getNodeChildren",record),
  204. expanded = $t.p.treeReader.expanded_field;
  205. $(childern).each(function(){
  206. var id = $t.p.idPrefix + $.jgrid.getAccessor(this,$t.p.localReader.id);
  207. $($($t).jqGrid('getGridRowById', id)).css("display","none");
  208. if(this[expanded]){
  209. $($t).jqGrid("collapseRow",this);
  210. }
  211. });
  212. });
  213. },
  214. // NS ,adjacency models
  215. getRootNodes : function() {
  216. var result = [];
  217. this.each(function(){
  218. var $t = this;
  219. if(!$t.grid || !$t.p.treeGrid) {return;}
  220. switch ($t.p.treeGridModel) {
  221. case 'nested' :
  222. var level = $t.p.treeReader.level_field;
  223. $($t.p.data).each(function(){
  224. if(parseInt(this[level],10) === parseInt($t.p.tree_root_level,10)) {
  225. result.push(this);
  226. }
  227. });
  228. break;
  229. case 'adjacency' :
  230. var parent_id = $t.p.treeReader.parent_id_field;
  231. $($t.p.data).each(function(){
  232. if(this[parent_id] === null || String(this[parent_id]).toLowerCase() === "null") {
  233. result.push(this);
  234. }
  235. });
  236. break;
  237. }
  238. });
  239. return result;
  240. },
  241. getNodeDepth : function(rc) {
  242. var ret = null;
  243. this.each(function(){
  244. if(!this.grid || !this.p.treeGrid) {return;}
  245. var $t = this;
  246. switch ($t.p.treeGridModel) {
  247. case 'nested' :
  248. var level = $t.p.treeReader.level_field;
  249. ret = parseInt(rc[level],10) - parseInt($t.p.tree_root_level,10);
  250. break;
  251. case 'adjacency' :
  252. ret = $($t).jqGrid("getNodeAncestors",rc).length;
  253. break;
  254. }
  255. });
  256. return ret;
  257. },
  258. getNodeParent : function(rc) {
  259. var result = null;
  260. this.each(function(){
  261. var $t = this;
  262. if(!$t.grid || !$t.p.treeGrid) {return;}
  263. switch ($t.p.treeGridModel) {
  264. case 'nested' :
  265. var lftc = $t.p.treeReader.left_field,
  266. rgtc = $t.p.treeReader.right_field,
  267. levelc = $t.p.treeReader.level_field,
  268. lft = parseInt(rc[lftc],10), rgt = parseInt(rc[rgtc],10), level = parseInt(rc[levelc],10);
  269. $(this.p.data).each(function(){
  270. if(parseInt(this[levelc],10) === level-1 && parseInt(this[lftc],10) < lft && parseInt(this[rgtc],10) > rgt) {
  271. result = this;
  272. return false;
  273. }
  274. });
  275. break;
  276. case 'adjacency' :
  277. var parent_id = $t.p.treeReader.parent_id_field,
  278. dtid = $t.p.localReader.id,
  279. ind = rc[dtid], pos = $t.p._index[ind];
  280. while(pos--) {
  281. if($t.p.data[pos][dtid] === $.jgrid.stripPref($t.p.idPrefix, rc[parent_id])) {
  282. result = $t.p.data[pos];
  283. break;
  284. }
  285. }
  286. break;
  287. }
  288. });
  289. return result;
  290. },
  291. getNodeChildren : function(rc) {
  292. var result = [];
  293. this.each(function(){
  294. var $t = this;
  295. if(!$t.grid || !$t.p.treeGrid) {return;}
  296. switch ($t.p.treeGridModel) {
  297. case 'nested' :
  298. var lftc = $t.p.treeReader.left_field,
  299. rgtc = $t.p.treeReader.right_field,
  300. levelc = $t.p.treeReader.level_field,
  301. lft = parseInt(rc[lftc],10), rgt = parseInt(rc[rgtc],10), level = parseInt(rc[levelc],10);
  302. $(this.p.data).each(function(){
  303. if(parseInt(this[levelc],10) === level+1 && parseInt(this[lftc],10) > lft && parseInt(this[rgtc],10) < rgt) {
  304. result.push(this);
  305. }
  306. });
  307. break;
  308. case 'adjacency' :
  309. var parent_id = $t.p.treeReader.parent_id_field,
  310. dtid = $t.p.localReader.id;
  311. $(this.p.data).each(function(){
  312. if(this[parent_id] == $.jgrid.stripPref($t.p.idPrefix, rc[dtid])) {
  313. result.push(this);
  314. }
  315. });
  316. break;
  317. }
  318. });
  319. return result;
  320. },
  321. getFullTreeNode : function(rc) {
  322. var result = [];
  323. this.each(function(){
  324. var $t = this, len;
  325. if(!$t.grid || !$t.p.treeGrid) {return;}
  326. switch ($t.p.treeGridModel) {
  327. case 'nested' :
  328. var lftc = $t.p.treeReader.left_field,
  329. rgtc = $t.p.treeReader.right_field,
  330. levelc = $t.p.treeReader.level_field,
  331. lft = parseInt(rc[lftc],10), rgt = parseInt(rc[rgtc],10), level = parseInt(rc[levelc],10);
  332. $(this.p.data).each(function(){
  333. if(parseInt(this[levelc],10) >= level && parseInt(this[lftc],10) >= lft && parseInt(this[lftc],10) <= rgt) {
  334. result.push(this);
  335. }
  336. });
  337. break;
  338. case 'adjacency' :
  339. if(rc) {
  340. result.push(rc);
  341. var parent_id = $t.p.treeReader.parent_id_field,
  342. dtid = $t.p.localReader.id;
  343. $(this.p.data).each(function(i){
  344. len = result.length;
  345. for (i = 0; i < len; i++) {
  346. if ($.jgrid.stripPref($t.p.idPrefix, result[i][dtid]) === this[parent_id]) {
  347. result.push(this);
  348. break;
  349. }
  350. }
  351. });
  352. }
  353. break;
  354. }
  355. });
  356. return result;
  357. },
  358. // End NS, adjacency Model
  359. getNodeAncestors : function(rc) {
  360. var ancestors = [];
  361. this.each(function(){
  362. if(!this.grid || !this.p.treeGrid) {return;}
  363. var parent = $(this).jqGrid("getNodeParent",rc);
  364. while (parent) {
  365. ancestors.push(parent);
  366. parent = $(this).jqGrid("getNodeParent",parent);
  367. }
  368. });
  369. return ancestors;
  370. },
  371. isVisibleNode : function(rc) {
  372. var result = true;
  373. this.each(function(){
  374. var $t = this;
  375. if(!$t.grid || !$t.p.treeGrid) {return;}
  376. var ancestors = $($t).jqGrid("getNodeAncestors",rc),
  377. expanded = $t.p.treeReader.expanded_field;
  378. $(ancestors).each(function(){
  379. result = result && this[expanded];
  380. if(!result) {return false;}
  381. });
  382. });
  383. return result;
  384. },
  385. isNodeLoaded : function(rc) {
  386. var result;
  387. this.each(function(){
  388. var $t = this;
  389. if(!$t.grid || !$t.p.treeGrid) {return;}
  390. var isLeaf = $t.p.treeReader.leaf_field,
  391. loaded = $t.p.treeReader.loaded;
  392. if(rc !== undefined ) {
  393. if(rc[loaded] !== undefined) {
  394. result = rc[loaded];
  395. } else if( rc[isLeaf] || $($t).jqGrid("getNodeChildren",rc).length > 0){
  396. result = true;
  397. } else {
  398. result = false;
  399. }
  400. } else {
  401. result = false;
  402. }
  403. });
  404. return result;
  405. },
  406. expandNode : function(rc) {
  407. return this.each(function(){
  408. if(!this.grid || !this.p.treeGrid) {return;}
  409. var expanded = this.p.treeReader.expanded_field,
  410. parent = this.p.treeReader.parent_id_field,
  411. loaded = this.p.treeReader.loaded,
  412. level = this.p.treeReader.level_field,
  413. lft = this.p.treeReader.left_field,
  414. rgt = this.p.treeReader.right_field;
  415. if(!rc[expanded]) {
  416. var id = $.jgrid.getAccessor(rc,this.p.localReader.id);
  417. var rc1 = $("#" + this.p.idPrefix + $.jgrid.jqID(id),this.grid.bDiv)[0];
  418. var position = this.p._index[id];
  419. if( $(this).jqGrid("isNodeLoaded",this.p.data[position]) ) {
  420. rc[expanded] = true;
  421. $("div.treeclick",rc1).removeClass(this.p.treeIcons.plus+" tree-plus").addClass(this.p.treeIcons.minus+" tree-minus");
  422. } else if (!this.grid.hDiv.loading) {
  423. rc[expanded] = true;
  424. $("div.treeclick",rc1).removeClass(this.p.treeIcons.plus+" tree-plus").addClass(this.p.treeIcons.minus+" tree-minus");
  425. this.p.treeANode = rc1.rowIndex;
  426. this.p.datatype = this.p.treedatatype;
  427. if(this.p.treeGridModel === 'nested') {
  428. $(this).jqGrid("setGridParam",{postData:{nodeid:id,n_left:rc[lft],n_right:rc[rgt],n_level:rc[level]}});
  429. } else {
  430. $(this).jqGrid("setGridParam",{postData:{nodeid:id,parentid:rc[parent],n_level:rc[level]}} );
  431. }
  432. $(this).trigger("reloadGrid");
  433. rc[loaded] = true;
  434. if(this.p.treeGridModel === 'nested') {
  435. $(this).jqGrid("setGridParam",{postData:{nodeid:'',n_left:'',n_right:'',n_level:''}});
  436. } else {
  437. $(this).jqGrid("setGridParam",{postData:{nodeid:'',parentid:'',n_level:''}});
  438. }
  439. }
  440. }
  441. });
  442. },
  443. collapseNode : function(rc) {
  444. return this.each(function(){
  445. if(!this.grid || !this.p.treeGrid) {return;}
  446. var expanded = this.p.treeReader.expanded_field;
  447. if(rc[expanded]) {
  448. rc[expanded] = false;
  449. var id = $.jgrid.getAccessor(rc,this.p.localReader.id);
  450. var rc1 = $("#" + this.p.idPrefix + $.jgrid.jqID(id),this.grid.bDiv)[0];
  451. $("div.treeclick",rc1).removeClass(this.p.treeIcons.minus+" tree-minus").addClass(this.p.treeIcons.plus+" tree-plus");
  452. }
  453. });
  454. },
  455. SortTree : function( sortname, newDir, st, datefmt) {
  456. return this.each(function(){
  457. if(!this.grid || !this.p.treeGrid) {return;}
  458. var i, len,
  459. rec, records = [], $t = this, query, roots,
  460. rt = $(this).jqGrid("getRootNodes");
  461. // Sorting roots
  462. query = $.jgrid.from(rt);
  463. query.orderBy(sortname,newDir,st, datefmt);
  464. roots = query.select();
  465. // Sorting children
  466. for (i = 0, len = roots.length; i < len; i++) {
  467. rec = roots[i];
  468. records.push(rec);
  469. $(this).jqGrid("collectChildrenSortTree",records, rec, sortname, newDir,st, datefmt);
  470. }
  471. $.each(records, function(index) {
  472. var id = $.jgrid.getAccessor(this,$t.p.localReader.id);
  473. $('#'+$.jgrid.jqID($t.p.id)+ ' tbody tr:eq('+index+')').after($('tr#'+$.jgrid.jqID(id),$t.grid.bDiv));
  474. });
  475. query = null;roots=null;records=null;
  476. });
  477. },
  478. collectChildrenSortTree : function(records, rec, sortname, newDir,st, datefmt) {
  479. return this.each(function(){
  480. if(!this.grid || !this.p.treeGrid) {return;}
  481. var i, len,
  482. child, ch, query, children;
  483. ch = $(this).jqGrid("getNodeChildren",rec);
  484. query = $.jgrid.from(ch);
  485. query.orderBy(sortname, newDir, st, datefmt);
  486. children = query.select();
  487. for (i = 0, len = children.length; i < len; i++) {
  488. child = children[i];
  489. records.push(child);
  490. $(this).jqGrid("collectChildrenSortTree",records, child, sortname, newDir, st, datefmt);
  491. }
  492. });
  493. },
  494. // experimental
  495. setTreeRow : function(rowid, data) {
  496. var success=false;
  497. this.each(function(){
  498. var t = this;
  499. if(!t.grid || !t.p.treeGrid) {return;}
  500. success = $(t).jqGrid("setRowData",rowid,data);
  501. });
  502. return success;
  503. },
  504. delTreeNode : function (rowid) {
  505. return this.each(function () {
  506. var $t = this, rid = $t.p.localReader.id, i,
  507. left = $t.p.treeReader.left_field,
  508. right = $t.p.treeReader.right_field, myright, width, res, key;
  509. if(!$t.grid || !$t.p.treeGrid) {return;}
  510. var rc = $t.p._index[rowid];
  511. if (rc !== undefined) {
  512. // nested
  513. myright = parseInt($t.p.data[rc][right],10);
  514. width = myright - parseInt($t.p.data[rc][left],10) + 1;
  515. var dr = $($t).jqGrid("getFullTreeNode",$t.p.data[rc]);
  516. if(dr.length>0){
  517. for (i=0;i<dr.length;i++){
  518. $($t).jqGrid("delRowData",dr[i][rid]);
  519. }
  520. }
  521. if( $t.p.treeGridModel === "nested") {
  522. // ToDo - update grid data
  523. res = $.jgrid.from($t.p.data)
  524. .greater(left,myright,{stype:'integer'})
  525. .select();
  526. if(res.length) {
  527. for( key in res) {
  528. if(res.hasOwnProperty(key)) {
  529. res[key][left] = parseInt(res[key][left],10) - width ;
  530. }
  531. }
  532. }
  533. res = $.jgrid.from($t.p.data)
  534. .greater(right,myright,{stype:'integer'})
  535. .select();
  536. if(res.length) {
  537. for( key in res) {
  538. if(res.hasOwnProperty(key)) {
  539. res[key][right] = parseInt(res[key][right],10) - width ;
  540. }
  541. }
  542. }
  543. }
  544. }
  545. });
  546. },
  547. addChildNode : function( nodeid, parentid, data, expandData ) {
  548. //return this.each(function(){
  549. var $t = this[0];
  550. if(data) {
  551. // we suppose tha the id is autoincremet and
  552. var expanded = $t.p.treeReader.expanded_field,
  553. isLeaf = $t.p.treeReader.leaf_field,
  554. level = $t.p.treeReader.level_field,
  555. //icon = $t.p.treeReader.icon_field,
  556. parent = $t.p.treeReader.parent_id_field,
  557. left = $t.p.treeReader.left_field,
  558. right = $t.p.treeReader.right_field,
  559. loaded = $t.p.treeReader.loaded,
  560. method, parentindex, parentdata, parentlevel, i, len, max=0, rowind = parentid, leaf, maxright;
  561. if(expandData===undefined) {expandData = false;}
  562. if ( nodeid === undefined || nodeid === null ) {
  563. i = $t.p.data.length-1;
  564. if( i>= 0 ) {
  565. while(i>=0){max = Math.max(max, parseInt($t.p.data[i][$t.p.localReader.id],10)); i--;}
  566. }
  567. nodeid = max+1;
  568. }
  569. var prow = $($t).jqGrid('getInd', parentid);
  570. leaf = false;
  571. // if not a parent we assume root
  572. if ( parentid === undefined || parentid === null || parentid==="") {
  573. parentid = null;
  574. rowind = null;
  575. method = 'last';
  576. parentlevel = $t.p.tree_root_level;
  577. i = $t.p.data.length+1;
  578. } else {
  579. method = 'after';
  580. parentindex = $t.p._index[parentid];
  581. parentdata = $t.p.data[parentindex];
  582. parentid = parentdata[$t.p.localReader.id];
  583. parentlevel = parseInt(parentdata[level],10)+1;
  584. var childs = $($t).jqGrid('getFullTreeNode', parentdata);
  585. // if there are child nodes get the last index of it
  586. if(childs.length) {
  587. i = childs[childs.length-1][$t.p.localReader.id];
  588. rowind = i;
  589. i = $($t).jqGrid('getInd',rowind)+1;
  590. } else {
  591. i = $($t).jqGrid('getInd', parentid)+1;
  592. }
  593. // if the node is leaf
  594. if(parentdata[isLeaf]) {
  595. leaf = true;
  596. parentdata[expanded] = true;
  597. //var prow = $($t).jqGrid('getInd', parentid);
  598. $($t.rows[prow])
  599. .find("span.cell-wrapperleaf").removeClass("cell-wrapperleaf").addClass("cell-wrapper")
  600. .end()
  601. .find("div.tree-leaf").removeClass($t.p.treeIcons.leaf+" tree-leaf").addClass($t.p.treeIcons.minus+" tree-minus");
  602. $t.p.data[parentindex][isLeaf] = false;
  603. parentdata[loaded] = true;
  604. }
  605. }
  606. len = i+1;
  607. if( data[expanded]===undefined) {data[expanded]= false;}
  608. if( data[loaded]===undefined ) { data[loaded] = false;}
  609. data[level] = parentlevel;
  610. if( data[isLeaf]===undefined) {data[isLeaf]= true;}
  611. if( $t.p.treeGridModel === "adjacency") {
  612. data[parent] = parentid;
  613. }
  614. if( $t.p.treeGridModel === "nested") {
  615. // this method requiere more attention
  616. var query, res, key;
  617. //maxright = parseInt(maxright,10);
  618. // ToDo - update grid data
  619. if(parentid !== null) {
  620. maxright = parseInt(parentdata[right],10);
  621. query = $.jgrid.from($t.p.data);
  622. query = query.greaterOrEquals(right,maxright,{stype:'integer'});
  623. res = query.select();
  624. if(res.length) {
  625. for( key in res) {
  626. if(res.hasOwnProperty(key)) {
  627. res[key][left] = res[key][left] > maxright ? parseInt(res[key][left],10) +2 : res[key][left];
  628. res[key][right] = res[key][right] >= maxright ? parseInt(res[key][right],10) +2 : res[key][right];
  629. }
  630. }
  631. }
  632. data[left] = maxright;
  633. data[right]= maxright+1;
  634. } else {
  635. maxright = parseInt( $($t).jqGrid('getCol', right, false, 'max'), 10);
  636. res = $.jgrid.from($t.p.data)
  637. .greater(left,maxright,{stype:'integer'})
  638. .select();
  639. if(res.length) {
  640. for( key in res) {
  641. if(res.hasOwnProperty(key)) {
  642. res[key][left] = parseInt(res[key][left],10) +2 ;
  643. }
  644. }
  645. }
  646. res = $.jgrid.from($t.p.data)
  647. .greater(right,maxright,{stype:'integer'})
  648. .select();
  649. if(res.length) {
  650. for( key in res) {
  651. if(res.hasOwnProperty(key)) {
  652. res[key][right] = parseInt(res[key][right],10) +2 ;
  653. }
  654. }
  655. }
  656. data[left] = maxright+1;
  657. data[right] = maxright + 2;
  658. }
  659. }
  660. if( parentid === null || $($t).jqGrid("isNodeLoaded",parentdata) || leaf ) {
  661. $($t).jqGrid('addRowData', nodeid, data, method, rowind);
  662. $($t).jqGrid('setTreeNode', i, len);
  663. }
  664. if(parentdata && !parentdata[expanded] && expandData) {
  665. $($t.rows[prow])
  666. .find("div.treeclick")
  667. .click();
  668. }
  669. }
  670. //});
  671. }
  672. });
  673. })(jQuery);