flexigrid.js 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  1. /*
  2. * Flexigrid for jQuery - v1.1
  3. *
  4. * Copyright (c) 2008 Paulo P. Marinas (code.google.com/p/flexigrid/)
  5. * Dual licensed under the MIT or GPL Version 2 licenses.
  6. * http://jquery.org/license
  7. *
  8. */
  9. (function ($) {
  10. /*
  11. * jQuery 1.9 support. browser object has been removed in 1.9
  12. */
  13. var browser = $.browser
  14. if (!browser) {
  15. function uaMatch( ua ) {
  16. ua = ua.toLowerCase();
  17. var match = /(chrome)[ \/]([\w.]+)/.exec( ua ) ||
  18. /(webkit)[ \/]([\w.]+)/.exec( ua ) ||
  19. /(opera)(?:.*version|)[ \/]([\w.]+)/.exec( ua ) ||
  20. /(msie) ([\w.]+)/.exec( ua ) ||
  21. ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec( ua ) ||
  22. [];
  23. return {
  24. browser: match[ 1 ] || "",
  25. version: match[ 2 ] || "0"
  26. };
  27. };
  28. var matched = uaMatch( navigator.userAgent );
  29. browser = {};
  30. if ( matched.browser ) {
  31. browser[ matched.browser ] = true;
  32. browser.version = matched.version;
  33. }
  34. // Chrome is Webkit, but Webkit is also Safari.
  35. if ( browser.chrome ) {
  36. browser.webkit = true;
  37. } else if ( browser.webkit ) {
  38. browser.safari = true;
  39. }
  40. }
  41. /*!
  42. * START code from jQuery UI
  43. *
  44. * Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
  45. * Dual licensed under the MIT or GPL Version 2 licenses.
  46. * http://jquery.org/license
  47. *
  48. * http://docs.jquery.com/UI
  49. */
  50. if(typeof $.support.selectstart != 'function') {
  51. $.support.selectstart = "onselectstart" in document.createElement("div");
  52. }
  53. if(typeof $.fn.disableSelection != 'function') {
  54. $.fn.disableSelection = function() {
  55. return this.bind( ( $.support.selectstart ? "selectstart" : "mousedown" ) +
  56. ".ui-disableSelection", function( event ) {
  57. event.preventDefault();
  58. });
  59. };
  60. }
  61. /* END code from jQuery UI */
  62. $.addFlex = function (t, p) {
  63. if (t.grid) return false; //return if already exist
  64. p = $.extend({ //apply default properties
  65. height: 200, //default height
  66. width: 'auto', //auto width
  67. striped: true, //apply odd even stripes
  68. novstripe: false,
  69. minwidth: 30, //min width of columns
  70. minheight: 80, //min height of columns
  71. resizable: true, //allow table resizing
  72. url: false, //URL if using data from AJAX
  73. method: 'POST', //data sending method
  74. dataType: 'xml', //type of data for AJAX, either xml or json
  75. errormsg: 'Connection Error',
  76. usepager: false,
  77. nowrap: true,
  78. page: 1, //current page
  79. total: 1, //total pages
  80. useRp: true, //use the results per page select box
  81. rp: 15, //results per page
  82. rpOptions: [10, 15, 20, 30, 50], //allowed per-page values
  83. title: false,
  84. idProperty: 'id',
  85. pagestat: 'Displaying {from} to {to} of {total} items',
  86. pagetext: 'Page',
  87. outof: 'of',
  88. findtext: 'Find',
  89. params: [], //allow optional parameters to be passed around
  90. procmsg: 'Processing, please wait ...',
  91. query: '',
  92. qtype: '',
  93. nomsg: 'No items',
  94. minColToggle: 1, //minimum allowed column to be hidden
  95. showToggleBtn: true, //show or hide column toggle popup
  96. hideOnSubmit: true,
  97. autoload: true,
  98. blockOpacity: 0.5,
  99. preProcess: false,
  100. addTitleToCell: false, // add a title attr to cells with truncated contents
  101. dblClickResize: false, //auto resize column by double clicking
  102. onDragCol: false,
  103. onToggleCol: false,
  104. onChangeSort: false,
  105. onDoubleClick: false,
  106. onSuccess: false,
  107. onError: false,
  108. onSubmit: false, //using a custom populate function
  109. __mw: { //extendable middleware function holding object
  110. datacol: function(p, col, val) { //middleware for formatting data columns
  111. var _col = (typeof p.datacol[col] == 'function') ? p.datacol[col](val) : val; //format column using function
  112. if(typeof p.datacol['*'] == 'function') { //if wildcard function exists
  113. return p.datacol['*'](_col); //run wildcard function
  114. } else {
  115. return _col; //return column without wildcard
  116. }
  117. }
  118. },
  119. getGridClass: function(g) { //get the grid class, always returns g
  120. return g;
  121. },
  122. datacol: {}, //datacol middleware object 'colkey': function(colval) {}
  123. colResize: true, //from: http://stackoverflow.com/a/10615589
  124. colMove: true
  125. }, p);
  126. $(t).show() //show if hidden
  127. .attr({
  128. cellPadding: 0,
  129. cellSpacing: 0,
  130. border: 0
  131. }) //remove padding and spacing
  132. .removeAttr('width'); //remove width properties
  133. //create grid class
  134. var g = {
  135. hset: {},
  136. rePosDrag: function () {
  137. var cdleft = 0 - this.hDiv.scrollLeft;
  138. if (this.hDiv.scrollLeft > 0) cdleft -= Math.floor(p.cgwidth / 2);
  139. $(g.cDrag).css({
  140. top: g.hDiv.offsetTop + 1
  141. });
  142. var cdpad = this.cdpad;
  143. var cdcounter=0;
  144. $('div', g.cDrag).hide();
  145. $('thead tr:first th:visible', this.hDiv).each(function () {
  146. var n = $('thead tr:first th:visible', g.hDiv).index(this);
  147. var cdpos = parseInt($('div', this).width());
  148. if (cdleft == 0) cdleft -= Math.floor(p.cgwidth / 2);
  149. cdpos = cdpos + cdleft + cdpad;
  150. if (isNaN(cdpos)) {
  151. cdpos = 0;
  152. }
  153. $('div:eq(' + n + ')', g.cDrag).css({
  154. 'left': (!(browser.mozilla) ? cdpos - cdcounter : cdpos) + 'px'
  155. }).show();
  156. cdleft = cdpos;
  157. cdcounter++;
  158. });
  159. },
  160. fixHeight: function (newH) {
  161. newH = false;
  162. if (!newH) newH = $(g.bDiv).height();
  163. var hdHeight = $(this.hDiv).height();
  164. $('div', this.cDrag).each(
  165. function () {
  166. $(this).height(newH + hdHeight);
  167. }
  168. );
  169. var nd = parseInt($(g.nDiv).height(), 10);
  170. if (nd > newH) $(g.nDiv).height(newH).width(200);
  171. else $(g.nDiv).height('auto').width('auto');
  172. $(g.block).css({
  173. height: newH,
  174. marginBottom: (newH * -1)
  175. });
  176. var hrH = g.bDiv.offsetTop + newH;
  177. if (p.height != 'auto' && p.resizable) hrH = g.vDiv.offsetTop;
  178. $(g.rDiv).css({
  179. height: hrH
  180. });
  181. },
  182. dragStart: function (dragtype, e, obj) { //default drag function start
  183. if (dragtype == 'colresize' && p.colResize === true) {//column resize
  184. $(g.nDiv).hide();
  185. $(g.nBtn).hide();
  186. var n = $('div', this.cDrag).index(obj);
  187. var ow = $('th:visible div:eq(' + n + ')', this.hDiv).width();
  188. $(obj).addClass('dragging').siblings().hide();
  189. $(obj).prev().addClass('dragging').show();
  190. this.colresize = {
  191. startX: e.pageX,
  192. ol: parseInt(obj.style.left, 10),
  193. ow: ow,
  194. n: n
  195. };
  196. $('body').css('cursor', 'col-resize');
  197. } else if (dragtype == 'vresize') {//table resize
  198. var hgo = false;
  199. $('body').css('cursor', 'row-resize');
  200. if (obj) {
  201. hgo = true;
  202. $('body').css('cursor', 'col-resize');
  203. }
  204. this.vresize = {
  205. h: p.height,
  206. sy: e.pageY,
  207. w: p.width,
  208. sx: e.pageX,
  209. hgo: hgo
  210. };
  211. } else if (dragtype == 'colMove') {//column header drag
  212. $(e.target).disableSelection(); //disable selecting the column header
  213. if((p.colMove === true)) {
  214. $(g.nDiv).hide();
  215. $(g.nBtn).hide();
  216. this.hset = $(this.hDiv).offset();
  217. this.hset.right = this.hset.left + $('table', this.hDiv).width();
  218. this.hset.bottom = this.hset.top + $('table', this.hDiv).height();
  219. this.dcol = obj;
  220. this.dcoln = $('th', this.hDiv).index(obj);
  221. this.colCopy = document.createElement("div");
  222. this.colCopy.className = "colCopy";
  223. this.colCopy.innerHTML = obj.innerHTML;
  224. if (browser.msie) {
  225. this.colCopy.className = "colCopy ie";
  226. }
  227. $(this.colCopy).css({
  228. position: 'absolute',
  229. 'float': 'left',
  230. display: 'none',
  231. textAlign: obj.align
  232. });
  233. $('body').append(this.colCopy);
  234. $(this.cDrag).hide();
  235. }
  236. }
  237. $('body').noSelect();
  238. },
  239. dragMove: function (e) {
  240. if (this.colresize) {//column resize
  241. var n = this.colresize.n;
  242. var diff = e.pageX - this.colresize.startX;
  243. var nleft = this.colresize.ol + diff;
  244. var nw = this.colresize.ow + diff;
  245. if (nw > p.minwidth) {
  246. $('div:eq(' + n + ')', this.cDrag).css('left', nleft);
  247. this.colresize.nw = nw;
  248. }
  249. } else if (this.vresize) {//table resize
  250. var v = this.vresize;
  251. var y = e.pageY;
  252. var diff = y - v.sy;
  253. if (!p.defwidth) p.defwidth = p.width;
  254. if (p.width != 'auto' && !p.nohresize && v.hgo) {
  255. var x = e.pageX;
  256. var xdiff = x - v.sx;
  257. var newW = v.w + xdiff;
  258. if (newW > p.defwidth) {
  259. this.gDiv.style.width = newW + 'px';
  260. p.width = newW;
  261. }
  262. }
  263. var newH = v.h + diff;
  264. if ((newH > p.minheight || p.height < p.minheight) && !v.hgo) {
  265. this.bDiv.style.height = newH + 'px';
  266. p.height = newH;
  267. this.fixHeight(newH);
  268. }
  269. v = null;
  270. } else if (this.colCopy) {
  271. $(this.dcol).addClass('thMove').removeClass('thOver');
  272. if (e.pageX > this.hset.right || e.pageX < this.hset.left || e.pageY > this.hset.bottom || e.pageY < this.hset.top) {
  273. //this.dragEnd();
  274. $('body').css('cursor', 'move');
  275. } else {
  276. $('body').css('cursor', 'pointer');
  277. }
  278. $(this.colCopy).css({
  279. top: e.pageY + 10,
  280. left: e.pageX + 20,
  281. display: 'block'
  282. });
  283. }
  284. },
  285. dragEnd: function () {
  286. if (this.colresize) {
  287. var n = this.colresize.n;
  288. var nw = this.colresize.nw;
  289. $('th:visible div:eq(' + n + ')', this.hDiv).css('width', nw);
  290. $('tr', this.bDiv).each(
  291. function () {
  292. var $tdDiv = $('td:visible div:eq(' + n + ')', this);
  293. $tdDiv.css('width', nw);
  294. g.addTitleToCell($tdDiv);
  295. }
  296. );
  297. this.hDiv.scrollLeft = this.bDiv.scrollLeft;
  298. $('div:eq(' + n + ')', this.cDrag).siblings().show();
  299. $('.dragging', this.cDrag).removeClass('dragging');
  300. this.rePosDrag();
  301. this.fixHeight();
  302. this.colresize = false;
  303. if ($.cookies) {
  304. var name = p.colModel[n].name; // Store the widths in the cookies
  305. $.cookie('flexiwidths/'+name, nw);
  306. }
  307. } else if (this.vresize) {
  308. this.vresize = false;
  309. } else if (this.colCopy) {
  310. $(this.colCopy).remove();
  311. if (this.dcolt !== null) {
  312. if (this.dcoln > this.dcolt) $('th:eq(' + this.dcolt + ')', this.hDiv).before(this.dcol);
  313. else $('th:eq(' + this.dcolt + ')', this.hDiv).after(this.dcol);
  314. this.switchCol(this.dcoln, this.dcolt);
  315. $(this.cdropleft).remove();
  316. $(this.cdropright).remove();
  317. this.rePosDrag();
  318. if (p.onDragCol) {
  319. p.onDragCol(this.dcoln, this.dcolt);
  320. }
  321. }
  322. this.dcol = null;
  323. this.hset = null;
  324. this.dcoln = null;
  325. this.dcolt = null;
  326. this.colCopy = null;
  327. $('.thMove', this.hDiv).removeClass('thMove');
  328. $(this.cDrag).show();
  329. }
  330. $('body').css('cursor', 'default');
  331. $('body').noSelect(false);
  332. },
  333. toggleCol: function (cid, visible) {
  334. var ncol = $("th[axis='col" + cid + "']", this.hDiv)[0];
  335. var n = $('thead th', g.hDiv).index(ncol);
  336. var cb = $('input[value=' + cid + ']', g.nDiv)[0];
  337. if (visible == null) {
  338. visible = ncol.hidden;
  339. }
  340. if ($('input:checked', g.nDiv).length < p.minColToggle && !visible) {
  341. return false;
  342. }
  343. if (visible) {
  344. ncol.hidden = false;
  345. $(ncol).show();
  346. cb.checked = true;
  347. } else {
  348. ncol.hidden = true;
  349. $(ncol).hide();
  350. cb.checked = false;
  351. }
  352. $('tbody tr', t).each(
  353. function () {
  354. if (visible) {
  355. $('td:eq(' + n + ')', this).show();
  356. } else {
  357. $('td:eq(' + n + ')', this).hide();
  358. }
  359. }
  360. );
  361. this.rePosDrag();
  362. if (p.onToggleCol) {
  363. p.onToggleCol(cid, visible);
  364. }
  365. return visible;
  366. },
  367. switchCol: function (cdrag, cdrop) { //switch columns
  368. $('tbody tr', t).each(
  369. function () {
  370. if (cdrag > cdrop) $('td:eq(' + cdrop + ')', this).before($('td:eq(' + cdrag + ')', this));
  371. else $('td:eq(' + cdrop + ')', this).after($('td:eq(' + cdrag + ')', this));
  372. }
  373. );
  374. //switch order in nDiv
  375. if (cdrag > cdrop) {
  376. $('tr:eq(' + cdrop + ')', this.nDiv).before($('tr:eq(' + cdrag + ')', this.nDiv));
  377. } else {
  378. $('tr:eq(' + cdrop + ')', this.nDiv).after($('tr:eq(' + cdrag + ')', this.nDiv));
  379. }
  380. if (browser.msie && browser.version < 7.0) {
  381. $('tr:eq(' + cdrop + ') input', this.nDiv)[0].checked = true;
  382. }
  383. this.hDiv.scrollLeft = this.bDiv.scrollLeft;
  384. },
  385. scroll: function () {
  386. this.hDiv.scrollLeft = this.bDiv.scrollLeft;
  387. this.rePosDrag();
  388. },
  389. addData: function (data) { //parse data
  390. if (p.dataType == 'json') {
  391. data = $.extend({rows: [], page: 0, total: 0}, data);
  392. }
  393. if (p.preProcess) {
  394. data = p.preProcess(data);
  395. }
  396. $('.pReload', this.pDiv).removeClass('loading');
  397. this.loading = false;
  398. if (!data) {
  399. $('.pPageStat', this.pDiv).html(p.errormsg);
  400. if (p.onSuccess) p.onSuccess(this);
  401. return false;
  402. }
  403. if (p.dataType == 'xml') {
  404. p.total = +$('rows total', data).text();
  405. } else {
  406. p.total = data.total;
  407. }
  408. if (p.total === 0) {
  409. $('tr, a, td, div', t).unbind();
  410. $(t).empty();
  411. p.pages = 1;
  412. p.page = 1;
  413. this.buildpager();
  414. $('.pPageStat', this.pDiv).html(p.nomsg);
  415. if (p.onSuccess) p.onSuccess(this);
  416. return false;
  417. }
  418. p.pages = Math.ceil(p.total / p.rp);
  419. if (p.dataType == 'xml') {
  420. p.page = +$('rows page', data).text();
  421. } else {
  422. p.page = data.page;
  423. }
  424. this.buildpager();
  425. //build new body
  426. var tbody = document.createElement('tbody');
  427. if (p.dataType == 'json') {
  428. $.each(data.rows, function (i, row) {
  429. var tr = document.createElement('tr');
  430. if (row.name) tr.name = row.name;
  431. if (row.color) {
  432. $(tr).css('background',row.color);
  433. } else {
  434. if (i % 2 && p.striped) tr.className = 'erow';
  435. }
  436. if (row[p.idProperty]) {
  437. tr.id = 'row' + row[p.idProperty];
  438. }
  439. $('thead tr:first th', g.hDiv).each( //add cell
  440. function () {
  441. var td = document.createElement('td');
  442. var idx = $(this).attr('axis').substr(3);
  443. td.align = this.align;
  444. // If each row is the object itself (no 'cell' key)
  445. if (typeof row.cell == 'undefined') {
  446. td.innerHTML = row[p.colModel[idx].name];
  447. } else {
  448. // If the json elements aren't named (which is typical), use numeric order
  449. var iHTML = '';
  450. if (typeof row.cell[idx] != "undefined") {
  451. iHTML = (row.cell[idx] !== null) ? row.cell[idx] : ''; //null-check for Opera-browser
  452. } else {
  453. iHTML = row.cell[p.colModel[idx].name];
  454. }
  455. td.innerHTML = p.__mw.datacol(p, $(this).attr('abbr'), iHTML); //use middleware datacol to format cols
  456. }
  457. // If the content has a <BGCOLOR=nnnnnn> option, decode it.
  458. var offs = td.innerHTML.indexOf( '<BGCOLOR=' );
  459. if( offs >0 ) {
  460. $(td).css('background', text.substr(offs+7,7) );
  461. }
  462. $(td).attr('abbr', $(this).attr('abbr'));
  463. $(tr).append(td);
  464. td = null;
  465. }
  466. );
  467. if ($('thead', this.gDiv).length < 1) {//handle if grid has no headers
  468. for (idx = 0; idx < row.cell.length; idx++) {
  469. var td = document.createElement('td');
  470. // If the json elements aren't named (which is typical), use numeric order
  471. if (typeof row.cell[idx] != "undefined") {
  472. td.innerHTML = (row.cell[idx] != null) ? row.cell[idx] : '';//null-check for Opera-browser
  473. } else {
  474. td.innerHTML = row.cell[p.colModel[idx].name];
  475. }
  476. $(tr).append(td);
  477. td = null;
  478. }
  479. }
  480. $(tbody).append(tr);
  481. tr = null;
  482. });
  483. } else if (p.dataType == 'xml') {
  484. var i = 1;
  485. $("rows row", data).each(function () {
  486. i++;
  487. var tr = document.createElement('tr');
  488. if ($(this).attr('name')) tr.name = $(this).attr('name');
  489. if ($(this).attr('color')) {
  490. $(tr).css('background',$(this).attr('id'));
  491. } else {
  492. if (i % 2 && p.striped) tr.className = 'erow';
  493. }
  494. var nid = $(this).attr('id');
  495. if (nid) {
  496. tr.id = 'row' + nid;
  497. }
  498. nid = null;
  499. var robj = this;
  500. $('thead tr:first th', g.hDiv).each(function () {
  501. var td = document.createElement('td');
  502. var idx = $(this).attr('axis').substr(3);
  503. td.align = this.align;
  504. var text = $("cell:eq(" + idx + ")", robj).text();
  505. var offs = text.indexOf( '<BGCOLOR=' );
  506. if( offs >0 ) {
  507. $(td).css('background', text.substr(offs+7,7) );
  508. }
  509. td.innerHTML = p.__mw.datacol(p, $(this).attr('abbr'), text); //use middleware datacol to format cols
  510. $(td).attr('abbr', $(this).attr('abbr'));
  511. $(tr).append(td);
  512. td = null;
  513. });
  514. if ($('thead', this.gDiv).length < 1) {//handle if grid has no headers
  515. $('cell', this).each(function () {
  516. var td = document.createElement('td');
  517. td.innerHTML = $(this).text();
  518. $(tr).append(td);
  519. td = null;
  520. });
  521. }
  522. $(tbody).append(tr);
  523. tr = null;
  524. robj = null;
  525. });
  526. }
  527. $('tr', t).unbind();
  528. $(t).empty();
  529. $(t).append(tbody);
  530. this.addCellProp();
  531. this.addRowProp();
  532. this.rePosDrag();
  533. tbody = null;
  534. data = null;
  535. i = null;
  536. if (p.onSuccess) {
  537. p.onSuccess(this);
  538. }
  539. if (p.hideOnSubmit) {
  540. $(g.block).remove();
  541. }
  542. this.hDiv.scrollLeft = this.bDiv.scrollLeft;
  543. if (browser.opera) {
  544. $(t).css('visibility', 'visible');
  545. }
  546. },
  547. changeSort: function (th) { //change sortorder
  548. if (this.loading) {
  549. return true;
  550. }
  551. $(g.nDiv).hide();
  552. $(g.nBtn).hide();
  553. if (p.sortname == $(th).attr('abbr')) {
  554. if (p.sortorder == 'asc') {
  555. p.sortorder = 'desc';
  556. } else {
  557. p.sortorder = 'asc';
  558. }
  559. }
  560. $(th).addClass('sorted').siblings().removeClass('sorted');
  561. $('.sdesc', this.hDiv).removeClass('sdesc');
  562. $('.sasc', this.hDiv).removeClass('sasc');
  563. $('div', th).addClass('s' + p.sortorder);
  564. p.sortname = $(th).attr('abbr');
  565. if (p.onChangeSort) {
  566. p.onChangeSort(p.sortname, p.sortorder);
  567. } else {
  568. this.populate();
  569. }
  570. },
  571. buildpager: function () { //rebuild pager based on new properties
  572. $('.pcontrol input', this.pDiv).val(p.page);
  573. $('.pcontrol span', this.pDiv).html(p.pages);
  574. var r1 = (p.page - 1) * p.rp + 1;
  575. var r2 = r1 + p.rp - 1;
  576. if (p.total < r2) {
  577. r2 = p.total;
  578. }
  579. var stat = p.pagestat;
  580. stat = stat.replace(/{from}/, r1);
  581. stat = stat.replace(/{to}/, r2);
  582. stat = stat.replace(/{total}/, p.total);
  583. $('.pPageStat', this.pDiv).html(stat);
  584. },
  585. populate: function () { //get latest data
  586. if (this.loading) {
  587. return true;
  588. }
  589. if (p.onSubmit) {
  590. var gh = p.onSubmit();
  591. if (!gh) {
  592. return false;
  593. }
  594. }
  595. this.loading = true;
  596. if (!p.url) {
  597. return false;
  598. }
  599. $('.pPageStat', this.pDiv).html(p.procmsg);
  600. $('.pReload', this.pDiv).addClass('loading');
  601. $(g.block).css({
  602. top: g.bDiv.offsetTop
  603. });
  604. if (p.hideOnSubmit) {
  605. $(this.gDiv).prepend(g.block);
  606. }
  607. if (browser.opera) {
  608. $(t).css('visibility', 'hidden');
  609. }
  610. if (!p.newp) {
  611. p.newp = 1;
  612. }
  613. if (p.page > p.pages) {
  614. p.page = p.pages;
  615. }
  616. var param = [{
  617. name: 'page',
  618. value: p.newp
  619. }, {
  620. name: 'rp',
  621. value: p.rp
  622. }, {
  623. name: 'sortname',
  624. value: p.sortname
  625. }, {
  626. name: 'sortorder',
  627. value: p.sortorder
  628. }, {
  629. name: 'query',
  630. value: p.query
  631. }, {
  632. name: 'qtype',
  633. value: p.qtype
  634. }];
  635. if (p.params.length) {
  636. for (var pi = 0; pi < p.params.length; pi++) {
  637. param[param.length] = p.params[pi];
  638. }
  639. }
  640. $.ajax({
  641. type: p.method,
  642. url: p.url,
  643. data: param,
  644. dataType: p.dataType,
  645. success: function (data) {
  646. g.addData(data);
  647. },
  648. error: function (XMLHttpRequest, textStatus, errorThrown) {
  649. try {
  650. if (p.onError) p.onError(XMLHttpRequest, textStatus, errorThrown);
  651. } catch (e) {}
  652. }
  653. });
  654. },
  655. doSearch: function () {
  656. p.query = $('input[name=q]', g.sDiv).val();
  657. p.qtype = $('select[name=qtype]', g.sDiv).val();
  658. p.newp = 1;
  659. this.populate();
  660. },
  661. changePage: function (ctype) { //change page
  662. if (this.loading) {
  663. return true;
  664. }
  665. switch (ctype) {
  666. case 'first':
  667. p.newp = 1;
  668. break;
  669. case 'prev':
  670. if (p.page > 1) {
  671. p.newp = parseInt(p.page, 10) - 1;
  672. }
  673. break;
  674. case 'next':
  675. if (p.page < p.pages) {
  676. p.newp = parseInt(p.page, 10) + 1;
  677. }
  678. break;
  679. case 'last':
  680. p.newp = p.pages;
  681. break;
  682. case 'input':
  683. var nv = parseInt($('.pcontrol input', this.pDiv).val(), 10);
  684. if (isNaN(nv)) {
  685. nv = 1;
  686. }
  687. if (nv < 1) {
  688. nv = 1;
  689. } else if (nv > p.pages) {
  690. nv = p.pages;
  691. }
  692. $('.pcontrol input', this.pDiv).val(nv);
  693. p.newp = nv;
  694. break;
  695. }
  696. if (p.newp == p.page) {
  697. return false;
  698. }
  699. if (p.onChangePage) {
  700. p.onChangePage(p.newp);
  701. } else {
  702. this.populate();
  703. }
  704. },
  705. addCellProp: function () {
  706. $('tbody tr td', g.bDiv).each(function () {
  707. var tdDiv = document.createElement('div');
  708. var n = $('td', $(this).parent()).index(this);
  709. var pth = $('th:eq(' + n + ')', g.hDiv).get(0);
  710. if (pth != null) {
  711. if (p.sortname == $(pth).attr('abbr') && p.sortname) {
  712. this.className = 'sorted';
  713. }
  714. $(tdDiv).css({
  715. textAlign: pth.align,
  716. width: $('div:first', pth)[0].style.width
  717. });
  718. if (pth.hidden) {
  719. $(this).css('display', 'none');
  720. }
  721. }
  722. if (p.nowrap == false) {
  723. $(tdDiv).css('white-space', 'normal');
  724. }
  725. if (this.innerHTML == '') {
  726. this.innerHTML = '&nbsp;';
  727. }
  728. tdDiv.innerHTML = this.innerHTML;
  729. var prnt = $(this).parent()[0];
  730. var pid = false;
  731. if (prnt.id) {
  732. pid = prnt.id.substr(3);
  733. }
  734. if (pth != null) {
  735. if (pth.process) pth.process(tdDiv, pid);
  736. }
  737. $(this).empty().append(tdDiv).removeAttr('width'); //wrap content
  738. g.addTitleToCell(tdDiv);
  739. });
  740. },
  741. getCellDim: function (obj) {// get cell prop for editable event
  742. var ht = parseInt($(obj).height(), 10);
  743. var pht = parseInt($(obj).parent().height(), 10);
  744. var wt = parseInt(obj.style.width, 10);
  745. var pwt = parseInt($(obj).parent().width(), 10);
  746. var top = obj.offsetParent.offsetTop;
  747. var left = obj.offsetParent.offsetLeft;
  748. var pdl = parseInt($(obj).css('paddingLeft'), 10);
  749. var pdt = parseInt($(obj).css('paddingTop'), 10);
  750. return {
  751. ht: ht,
  752. wt: wt,
  753. top: top,
  754. left: left,
  755. pdl: pdl,
  756. pdt: pdt,
  757. pht: pht,
  758. pwt: pwt
  759. };
  760. },
  761. addRowProp: function () {
  762. $('tbody tr', g.bDiv).on('click', function (e) {
  763. var obj = (e.target || e.srcElement);
  764. if (obj.href || obj.type) return true;
  765. if (e.ctrlKey || e.metaKey) {
  766. // mousedown already took care of this case
  767. return;
  768. }
  769. $(this).toggleClass('trSelected');
  770. if (p.singleSelect && ! g.multisel) {
  771. $(this).siblings().removeClass('trSelected');
  772. }
  773. }).on('mousedown', function (e) {
  774. if (e.shiftKey) {
  775. $(this).toggleClass('trSelected');
  776. g.multisel = true;
  777. this.focus();
  778. $(g.gDiv).noSelect();
  779. }
  780. if (e.ctrlKey || e.metaKey) {
  781. $(this).toggleClass('trSelected');
  782. g.multisel = true;
  783. this.focus();
  784. }
  785. }).on('mouseup', function (e) {
  786. if (g.multisel && ! (e.ctrlKey || e.metaKey)) {
  787. g.multisel = false;
  788. $(g.gDiv).noSelect(false);
  789. }
  790. }).on('dblclick', function () {
  791. if (p.onDoubleClick) {
  792. p.onDoubleClick(this, g, p);
  793. }
  794. }).hover(function (e) {
  795. if (g.multisel && e.shiftKey) {
  796. $(this).toggleClass('trSelected');
  797. }
  798. }, function () {});
  799. if (browser.msie && browser.version < 7.0) {
  800. $(this).hover(function () {
  801. $(this).addClass('trOver');
  802. }, function () {
  803. $(this).removeClass('trOver');
  804. });
  805. }
  806. },
  807. combo_flag: true,
  808. combo_resetIndex: function(selObj)
  809. {
  810. if(this.combo_flag) {
  811. selObj.selectedIndex = 0;
  812. }
  813. this.combo_flag = true;
  814. },
  815. combo_doSelectAction: function(selObj)
  816. {
  817. eval( selObj.options[selObj.selectedIndex].value );
  818. selObj.selectedIndex = 0;
  819. this.combo_flag = false;
  820. },
  821. //Add title attribute to div if cell contents is truncated
  822. addTitleToCell: function(tdDiv) {
  823. if(p.addTitleToCell) {
  824. var $span = $('<span />').css('display', 'none'),
  825. $div = (tdDiv instanceof jQuery) ? tdDiv : $(tdDiv),
  826. div_w = $div.outerWidth(),
  827. span_w = 0;
  828. $('body').children(':first').before($span);
  829. $span.html($div.html());
  830. $span.css('font-size', '' + $div.css('font-size'));
  831. $span.css('padding-left', '' + $div.css('padding-left'));
  832. span_w = $span.innerWidth();
  833. $span.remove();
  834. if(span_w > div_w) {
  835. $div.attr('title', $div.text());
  836. } else {
  837. $div.removeAttr('title');
  838. }
  839. }
  840. },
  841. autoResizeColumn: function (obj) {
  842. if(!p.dblClickResize) {
  843. return;
  844. }
  845. var n = $('div', this.cDrag).index(obj),
  846. $th = $('th:visible div:eq(' + n + ')', this.hDiv),
  847. ol = parseInt(obj.style.left, 10),
  848. ow = $th.width(),
  849. nw = 0,
  850. nl = 0,
  851. $span = $('<span />');
  852. $('body').children(':first').before($span);
  853. $span.html($th.html());
  854. $span.css('font-size', '' + $th.css('font-size'));
  855. $span.css('padding-left', '' + $th.css('padding-left'));
  856. $span.css('padding-right', '' + $th.css('padding-right'));
  857. nw = $span.width();
  858. $('tr', this.bDiv).each(function () {
  859. var $tdDiv = $('td:visible div:eq(' + n + ')', this),
  860. spanW = 0;
  861. $span.html($tdDiv.html());
  862. $span.css('font-size', '' + $tdDiv.css('font-size'));
  863. $span.css('padding-left', '' + $tdDiv.css('padding-left'));
  864. $span.css('padding-right', '' + $tdDiv.css('padding-right'));
  865. spanW = $span.width();
  866. nw = (spanW > nw) ? spanW : nw;
  867. });
  868. $span.remove();
  869. nw = (p.minWidth > nw) ? p.minWidth : nw;
  870. nl = ol + (nw - ow);
  871. $('div:eq(' + n + ')', this.cDrag).css('left', nl);
  872. this.colresize = {
  873. nw: nw,
  874. n: n
  875. };
  876. g.dragEnd();
  877. },
  878. pager: 0
  879. };
  880. g = p.getGridClass(g); //get the grid class
  881. if (p.colModel) { //create model if any
  882. thead = document.createElement('thead');
  883. var tr = document.createElement('tr');
  884. for (var i = 0; i < p.colModel.length; i++) {
  885. var cm = p.colModel[i];
  886. var th = document.createElement('th');
  887. $(th).attr('axis', 'col' + i);
  888. if( cm ) { // only use cm if its defined
  889. if ($.cookies) {
  890. var cookie_width = 'flexiwidths/'+cm.name; // Re-Store the widths in the cookies
  891. if( $.cookie(cookie_width) != undefined ) {
  892. cm.width = $.cookie(cookie_width);
  893. }
  894. }
  895. if( cm.display != undefined ) {
  896. th.innerHTML = cm.display;
  897. }
  898. if (cm.name && cm.sortable) {
  899. $(th).attr('abbr', cm.name);
  900. }
  901. if (cm.align) {
  902. th.align = cm.align;
  903. }
  904. if (cm.width) {
  905. $(th).attr('width', cm.width);
  906. }
  907. if ($(cm).attr('hide')) {
  908. th.hidden = true;
  909. }
  910. if (cm.process) {
  911. th.process = cm.process;
  912. }
  913. } else {
  914. th.innerHTML = "";
  915. $(th).attr('width',30);
  916. }
  917. $(tr).append(th);
  918. }
  919. $(thead).append(tr);
  920. $(t).prepend(thead);
  921. } // end if p.colmodel
  922. //init divs
  923. g.gDiv = document.createElement('div'); //create global container
  924. g.mDiv = document.createElement('div'); //create title container
  925. g.hDiv = document.createElement('div'); //create header container
  926. g.bDiv = document.createElement('div'); //create body container
  927. g.vDiv = document.createElement('div'); //create grip
  928. g.rDiv = document.createElement('div'); //create horizontal resizer
  929. g.cDrag = document.createElement('div'); //create column drag
  930. g.block = document.createElement('div'); //creat blocker
  931. g.nDiv = document.createElement('div'); //create column show/hide popup
  932. g.nBtn = document.createElement('div'); //create column show/hide button
  933. g.iDiv = document.createElement('div'); //create editable layer
  934. g.tDiv = document.createElement('div'); //create toolbar
  935. g.sDiv = document.createElement('div');
  936. g.pDiv = document.createElement('div'); //create pager container
  937. if(p.colResize === false) { //don't display column drag if we are not using it
  938. $(g.cDrag).css('display', 'none');
  939. }
  940. if (!p.usepager) {
  941. g.pDiv.style.display = 'none';
  942. }
  943. g.hTable = document.createElement('table');
  944. if (t.id != '') {// add ID from source element - mod by Piotr Labudda piotrl86@gmail.com
  945. g.gDiv.id = t.id;
  946. }
  947. g.gDiv.className = 'flexigrid';
  948. if (p.width != 'auto') {
  949. g.gDiv.style.width = p.width + isNaN(p.width) ? '' : 'px';
  950. }
  951. //add conditional classes
  952. if (browser.msie) {
  953. $(g.gDiv).addClass('ie');
  954. }
  955. if (p.novstripe) {
  956. $(g.gDiv).addClass('novstripe');
  957. }
  958. $(t).before(g.gDiv);
  959. $(g.gDiv).append(t);
  960. //set toolbar
  961. if (p.buttons) {
  962. g.tDiv.className = 'tDiv';
  963. var tDiv2 = document.createElement('div');
  964. tDiv2.className = 'tDiv2';
  965. for (var i = 0; i < p.buttons.length; i++) {
  966. var btn = p.buttons[i];
  967. if (!btn.separator) {
  968. var btnDiv = document.createElement('div');
  969. btnDiv.className = 'fbutton';
  970. btnDiv.innerHTML = ("<div><span>") + (btn.hidename ? "&nbsp;" : btn.name) + ("</span></div>");
  971. if (btn.bclass) $('span', btnDiv).addClass(btn.bclass).css({
  972. paddingLeft: 20
  973. });
  974. if (btn.bimage) // if bimage defined, use its string as an image url for this buttons style (RS)
  975. $('span',btnDiv).css( 'background', 'url('+btn.bimage+') no-repeat center left' );
  976. $('span',btnDiv).css( 'paddingLeft', 20 );
  977. if (btn.tooltip) // add title if exists (RS)
  978. $('span',btnDiv)[0].title = btn.tooltip;
  979. btnDiv.onpress = btn.onpress;
  980. btnDiv.name = btn.name;
  981. if (btn.id) {
  982. btnDiv.id = btn.id;
  983. }
  984. if (btn.onpress) {
  985. $(btnDiv).click(function () {
  986. this.onpress(this.id || this.name, g.gDiv);
  987. });
  988. }
  989. $(tDiv2).append(btnDiv);
  990. if (browser.msie && browser.version < 7.0) {
  991. $(btnDiv).hover(function () {
  992. $(this).addClass('fbOver');
  993. }, function () {
  994. $(this).removeClass('fbOver');
  995. });
  996. }
  997. } else {
  998. $(tDiv2).append("<div class='btnseparator'></div>");
  999. }
  1000. }
  1001. $(g.tDiv).append(tDiv2);
  1002. $(g.tDiv).append("<div style='clear:both'></div>");
  1003. $(g.gDiv).prepend(g.tDiv);
  1004. }
  1005. g.hDiv.className = 'hDiv';
  1006. // Define a combo button set with custom action'ed calls when clicked.
  1007. if( p.combobuttons && $(g.tDiv2) )
  1008. {
  1009. var btnDiv = document.createElement('div');
  1010. btnDiv.className = 'fbutton';
  1011. var tSelect = document.createElement('select');
  1012. $(tSelect).change( function () { g.combo_doSelectAction( tSelect ) } );
  1013. $(tSelect).click( function () { g.combo_resetIndex( tSelect) } );
  1014. tSelect.className = 'cselect';
  1015. $(btnDiv).append(tSelect);
  1016. for (i=0;i<p.combobuttons.length;i++)
  1017. {
  1018. var btn = p.combobuttons[i];
  1019. if (!btn.separator)
  1020. {
  1021. var btnOpt = document.createElement('option');
  1022. btnOpt.innerHTML = btn.name;
  1023. if (btn.bclass)
  1024. $(btnOpt)
  1025. .addClass(btn.bclass)
  1026. .css({paddingLeft:20})
  1027. ;
  1028. if (btn.bimage) // if bimage defined, use its string as an image url for this buttons style (RS)
  1029. $(btnOpt).css( 'background', 'url('+btn.bimage+') no-repeat center left' );
  1030. $(btnOpt).css( 'paddingLeft', 20 );
  1031. if (btn.tooltip) // add title if exists (RS)
  1032. $(btnOpt)[0].title = btn.tooltip;
  1033. if (btn.onpress)
  1034. {
  1035. btnOpt.value = btn.onpress;
  1036. }
  1037. $(tSelect).append(btnOpt);
  1038. }
  1039. }
  1040. $('.tDiv2').append(btnDiv);
  1041. }
  1042. $(t).before(g.hDiv);
  1043. g.hTable.cellPadding = 0;
  1044. g.hTable.cellSpacing = 0;
  1045. $(g.hDiv).append('<div class="hDivBox"></div>');
  1046. $('div', g.hDiv).append(g.hTable);
  1047. var thead = $("thead:first", t).get(0);
  1048. if (thead) $(g.hTable).append(thead);
  1049. thead = null;
  1050. if (!p.colmodel) var ci = 0;
  1051. $('thead tr:first th', g.hDiv).each(function () {
  1052. var thdiv = document.createElement('div');
  1053. if ($(this).attr('abbr')) {
  1054. $(this).click(function (e) {
  1055. if (!$(this).hasClass('thOver')) return false;
  1056. var obj = (e.target || e.srcElement);
  1057. if (obj.href || obj.type) return true;
  1058. g.changeSort(this);
  1059. });
  1060. if ($(this).attr('abbr') == p.sortname) {
  1061. this.className = 'sorted';
  1062. thdiv.className = 's' + p.sortorder;
  1063. }
  1064. }
  1065. if (this.hidden) {
  1066. $(this).hide();
  1067. }
  1068. if (!p.colmodel) {
  1069. $(this).attr('axis', 'col' + ci++);
  1070. }
  1071. // if there isn't a default width, then the column headers don't match
  1072. // i'm sure there is a better way, but this at least stops it failing
  1073. if (this.width == '') {
  1074. this.width = 100;
  1075. }
  1076. $(thdiv).css({
  1077. textAlign: this.align,
  1078. width: this.width + 'px'
  1079. });
  1080. thdiv.innerHTML = this.innerHTML;
  1081. $(this).empty().append(thdiv).removeAttr('width').mousedown(function (e) {
  1082. g.dragStart('colMove', e, this);
  1083. }).hover(function () {
  1084. if (!g.colresize && !$(this).hasClass('thMove') && !g.colCopy) {
  1085. $(this).addClass('thOver');
  1086. }
  1087. if ($(this).attr('abbr') != p.sortname && !g.colCopy && !g.colresize && $(this).attr('abbr')) {
  1088. $('div', this).addClass('s' + p.sortorder);
  1089. } else if ($(this).attr('abbr') == p.sortname && !g.colCopy && !g.colresize && $(this).attr('abbr')) {
  1090. var no = (p.sortorder == 'asc') ? 'desc' : 'asc';
  1091. $('div', this).removeClass('s' + p.sortorder).addClass('s' + no);
  1092. }
  1093. if (g.colCopy) {
  1094. var n = $('th', g.hDiv).index(this);
  1095. if (n == g.dcoln) {
  1096. return false;
  1097. }
  1098. if (n < g.dcoln) {
  1099. $(this).append(g.cdropleft);
  1100. } else {
  1101. $(this).append(g.cdropright);
  1102. }
  1103. g.dcolt = n;
  1104. } else if (!g.colresize) {
  1105. var nv = $('th:visible', g.hDiv).index(this);
  1106. var onl = parseInt($('div:eq(' + nv + ')', g.cDrag).css('left'), 10);
  1107. var nw = jQuery(g.nBtn).outerWidth();
  1108. var nl = onl - nw + Math.floor(p.cgwidth / 2);
  1109. $(g.nDiv).hide();
  1110. $(g.nBtn).hide();
  1111. $(g.nBtn).css({
  1112. 'left': nl,
  1113. top: g.hDiv.offsetTop
  1114. }).show();
  1115. var ndw = parseInt($(g.nDiv).width(), 10);
  1116. $(g.nDiv).css({
  1117. top: g.bDiv.offsetTop
  1118. });
  1119. if ((nl + ndw) > $(g.gDiv).width()) {
  1120. $(g.nDiv).css('left', onl - ndw + 1);
  1121. } else {
  1122. $(g.nDiv).css('left', nl);
  1123. }
  1124. if ($(this).hasClass('sorted')) {
  1125. $(g.nBtn).addClass('srtd');
  1126. } else {
  1127. $(g.nBtn).removeClass('srtd');
  1128. }
  1129. }
  1130. }, function () {
  1131. $(this).removeClass('thOver');
  1132. if ($(this).attr('abbr') != p.sortname) {
  1133. $('div', this).removeClass('s' + p.sortorder);
  1134. } else if ($(this).attr('abbr') == p.sortname) {
  1135. var no = (p.sortorder == 'asc') ? 'desc' : 'asc';
  1136. $('div', this).addClass('s' + p.sortorder).removeClass('s' + no);
  1137. }
  1138. if (g.colCopy) {
  1139. $(g.cdropleft).remove();
  1140. $(g.cdropright).remove();
  1141. g.dcolt = null;
  1142. }
  1143. }); //wrap content
  1144. });
  1145. //set bDiv
  1146. g.bDiv.className = 'bDiv';
  1147. $(t).before(g.bDiv);
  1148. $(g.bDiv).css({
  1149. height: (p.height == 'auto') ? 'auto' : p.height + "px"
  1150. }).scroll(function (e) {
  1151. g.scroll()
  1152. }).append(t);
  1153. if (p.height == 'auto') {
  1154. $('table', g.bDiv).addClass('autoht');
  1155. }
  1156. //add td & row properties
  1157. g.addCellProp();
  1158. g.addRowProp();
  1159. //set cDrag only if we are using it
  1160. if (p.colResize === true) {
  1161. var cdcol = $('thead tr:first th:first', g.hDiv).get(0);
  1162. if(cdcol !== null) {
  1163. g.cDrag.className = 'cDrag';
  1164. g.cdpad = 0;
  1165. g.cdpad += (isNaN(parseInt($('div', cdcol).css('borderLeftWidth'), 10)) ? 0 : parseInt($('div', cdcol).css('borderLeftWidth'), 10));
  1166. g.cdpad += (isNaN(parseInt($('div', cdcol).css('borderRightWidth'), 10)) ? 0 : parseInt($('div', cdcol).css('borderRightWidth'), 10));
  1167. g.cdpad += (isNaN(parseInt($('div', cdcol).css('paddingLeft'), 10)) ? 0 : parseInt($('div', cdcol).css('paddingLeft'), 10));
  1168. g.cdpad += (isNaN(parseInt($('div', cdcol).css('paddingRight'), 10)) ? 0 : parseInt($('div', cdcol).css('paddingRight'), 10));
  1169. g.cdpad += (isNaN(parseInt($(cdcol).css('borderLeftWidth'), 10)) ? 0 : parseInt($(cdcol).css('borderLeftWidth'), 10));
  1170. g.cdpad += (isNaN(parseInt($(cdcol).css('borderRightWidth'), 10)) ? 0 : parseInt($(cdcol).css('borderRightWidth'), 10));
  1171. g.cdpad += (isNaN(parseInt($(cdcol).css('paddingLeft'), 10)) ? 0 : parseInt($(cdcol).css('paddingLeft'), 10));
  1172. g.cdpad += (isNaN(parseInt($(cdcol).css('paddingRight'), 10)) ? 0 : parseInt($(cdcol).css('paddingRight'), 10));
  1173. $(g.bDiv).before(g.cDrag);
  1174. var cdheight = $(g.bDiv).height();
  1175. var hdheight = $(g.hDiv).height();
  1176. $(g.cDrag).css({
  1177. top: -hdheight + 'px'
  1178. });
  1179. $('thead tr:first th', g.hDiv).each(function() {
  1180. var cgDiv = document.createElement('div');
  1181. $(g.cDrag).append(cgDiv);
  1182. if (!p.cgwidth) {
  1183. p.cgwidth = $(cgDiv).width();
  1184. }
  1185. $(cgDiv).css({
  1186. height: cdheight + hdheight
  1187. }).mousedown(function(e) {
  1188. g.dragStart('colresize', e, this);
  1189. }).dblclick(function(e) {
  1190. g.autoResizeColumn(this);
  1191. });
  1192. if (browser.msie && browser.version < 7.0) {
  1193. g.fixHeight($(g.gDiv).height());
  1194. $(cgDiv).hover(function() {
  1195. g.fixHeight();
  1196. $(this).addClass('dragging');
  1197. }, function() {
  1198. if(!g.colresize) {
  1199. $(this).removeClass('dragging');
  1200. }
  1201. });
  1202. }
  1203. });
  1204. }
  1205. }
  1206. //add strip
  1207. if (p.striped) {
  1208. $('tbody tr:odd', g.bDiv).addClass('erow');
  1209. }
  1210. if (p.resizable && p.height != 'auto') {
  1211. g.vDiv.className = 'vGrip';
  1212. $(g.vDiv).mousedown(function (e) {
  1213. g.dragStart('vresize', e);
  1214. }).html('<span></span>');
  1215. $(g.bDiv).after(g.vDiv);
  1216. }
  1217. if (p.resizable && p.width != 'auto' && !p.nohresize) {
  1218. g.rDiv.className = 'hGrip';
  1219. $(g.rDiv).mousedown(function (e) {
  1220. g.dragStart('vresize', e, true);
  1221. }).html('<span></span>').css('height', $(g.gDiv).height());
  1222. if (browser.msie && browser.version < 7.0) {
  1223. $(g.rDiv).hover(function () {
  1224. $(this).addClass('hgOver');
  1225. }, function () {
  1226. $(this).removeClass('hgOver');
  1227. });
  1228. }
  1229. $(g.gDiv).append(g.rDiv);
  1230. }
  1231. // add pager
  1232. if (p.usepager) {
  1233. g.pDiv.className = 'pDiv';
  1234. g.pDiv.innerHTML = '<div class="pDiv2"></div>';
  1235. $(g.bDiv).after(g.pDiv);
  1236. var html = ' <div class="pGroup"> <div class="pFirst pButton"><span></span></div><div class="pPrev pButton"><span></span></div> </div> <div class="btnseparator"></div> <div class="pGroup"><span class="pcontrol">' + p.pagetext + ' <input type="text" size="4" value="1" /> ' + p.outof + ' <span> 1 </span></span></div> <div class="btnseparator"></div> <div class="pGroup"> <div class="pNext pButton"><span></span></div><div class="pLast pButton"><span></span></div> </div> <div class="btnseparator"></div> <div class="pGroup"> <div class="pReload pButton"><span></span></div> </div> <div class="btnseparator"></div> <div class="pGroup"><span class="pPageStat"></span></div>';
  1237. $('div', g.pDiv).html(html);
  1238. $('.pReload', g.pDiv).click(function () {
  1239. g.populate();
  1240. });
  1241. $('.pFirst', g.pDiv).click(function () {
  1242. g.changePage('first');
  1243. });
  1244. $('.pPrev', g.pDiv).click(function () {
  1245. g.changePage('prev');
  1246. });
  1247. $('.pNext', g.pDiv).click(function () {
  1248. g.changePage('next');
  1249. });
  1250. $('.pLast', g.pDiv).click(function () {
  1251. g.changePage('last');
  1252. });
  1253. $('.pcontrol input', g.pDiv).keydown(function (e) {
  1254. if (e.keyCode == 13) {
  1255. g.changePage('input');
  1256. }
  1257. });
  1258. if (browser.msie && browser.version < 7) $('.pButton', g.pDiv).hover(function () {
  1259. $(this).addClass('pBtnOver');
  1260. }, function () {
  1261. $(this).removeClass('pBtnOver');
  1262. });
  1263. if (p.useRp) {
  1264. var opt = '',
  1265. sel = '';
  1266. for (var nx = 0; nx < p.rpOptions.length; nx++) {
  1267. if (p.rp == p.rpOptions[nx]) sel = 'selected="selected"';
  1268. else sel = '';
  1269. opt += "<option value='" + p.rpOptions[nx] + "' " + sel + " >" + p.rpOptions[nx] + "&nbsp;&nbsp;</option>";
  1270. }
  1271. $('.pDiv2', g.pDiv).prepend("<div class='pGroup'><select name='rp'>" + opt + "</select></div> <div class='btnseparator'></div>");
  1272. $('select', g.pDiv).change(function () {
  1273. if (p.onRpChange) {
  1274. p.onRpChange(+this.value);
  1275. } else {
  1276. p.newp = 1;
  1277. p.rp = +this.value;
  1278. g.populate();
  1279. }
  1280. });
  1281. }
  1282. //add search button
  1283. if (p.searchitems) {
  1284. $('.pDiv2', g.pDiv).prepend("<div class='pGroup'> <div class='pSearch pButton'><span></span></div> </div> <div class='btnseparator'></div>");
  1285. $('.pSearch', g.pDiv).click(function () {
  1286. $(g.sDiv).slideToggle('fast', function () {
  1287. $('.sDiv:visible input:first', g.gDiv).trigger('focus');
  1288. });
  1289. });
  1290. //add search box
  1291. g.sDiv.className = 'sDiv';
  1292. var sitems = p.searchitems;
  1293. var sopt = '', sel = '';
  1294. for (var s = 0; s < sitems.length; s++) {
  1295. if (p.qtype === '' && sitems[s].isdefault === true) {
  1296. p.qtype = sitems[s].name;
  1297. sel = 'selected="selected"';
  1298. } else {
  1299. sel = '';
  1300. }
  1301. sopt += "<option value='" + sitems[s].name + "' " + sel + " >" + sitems[s].display + "&nbsp;&nbsp;</option>";
  1302. }
  1303. if (p.qtype === '') {
  1304. p.qtype = sitems[0].name;
  1305. }
  1306. $(g.sDiv).append("<div class='sDiv2'>" + p.findtext +
  1307. " <input type='text' value='" + p.query +"' size='30' name='q' class='qsbox' /> "+
  1308. " <select name='qtype'>" + sopt + "</select></div>");
  1309. //Split into separate selectors because of bug in jQuery 1.3.2
  1310. $('input[name=q]', g.sDiv).keydown(function (e) {
  1311. if (e.keyCode == 13) {
  1312. g.doSearch();
  1313. }
  1314. });
  1315. $('select[name=qtype]', g.sDiv).keydown(function (e) {
  1316. if (e.keyCode == 13) {
  1317. g.doSearch();
  1318. }
  1319. });
  1320. $('input[value=Clear]', g.sDiv).click(function () {
  1321. $('input[name=q]', g.sDiv).val('');
  1322. p.query = '';
  1323. g.doSearch();
  1324. });
  1325. $(g.bDiv).after(g.sDiv);
  1326. }
  1327. }
  1328. $(g.pDiv, g.sDiv).append("<div style='clear:both'></div>");
  1329. // add title
  1330. if (p.title) {
  1331. g.mDiv.className = 'mDiv';
  1332. g.mDiv.innerHTML = '<div class="ftitle">' + p.title + '</div>';
  1333. $(g.gDiv).prepend(g.mDiv);
  1334. if (p.showTableToggleBtn) {
  1335. $(g.mDiv).append('<div class="ptogtitle" title="Minimize/Maximize Table"><span></span></div>');
  1336. $('div.ptogtitle', g.mDiv).click(function () {
  1337. $(g.gDiv).toggleClass('hideBody');
  1338. $(this).toggleClass('vsble');
  1339. });
  1340. }
  1341. }
  1342. //setup cdrops
  1343. g.cdropleft = document.createElement('span');
  1344. g.cdropleft.className = 'cdropleft';
  1345. g.cdropright = document.createElement('span');
  1346. g.cdropright.className = 'cdropright';
  1347. //add block
  1348. g.block.className = 'gBlock';
  1349. var gh = $(g.bDiv).height();
  1350. var gtop = g.bDiv.offsetTop;
  1351. $(g.block).css({
  1352. width: g.bDiv.style.width,
  1353. height: gh,
  1354. background: 'white',
  1355. position: 'relative',
  1356. marginBottom: (gh * -1),
  1357. zIndex: 1,
  1358. top: gtop,
  1359. left: '0px'
  1360. });
  1361. $(g.block).fadeTo(0, p.blockOpacity);
  1362. // add column control
  1363. if ($('th', g.hDiv).length) {
  1364. g.nDiv.className = 'nDiv';
  1365. g.nDiv.innerHTML = "<table cellpadding='0' cellspacing='0'><tbody></tbody></table>";
  1366. $(g.nDiv).css({
  1367. marginBottom: (gh * -1),
  1368. display: 'none',
  1369. top: gtop
  1370. }).noSelect();
  1371. var cn = 0;
  1372. $('th div', g.hDiv).each(function () {
  1373. var kcol = $("th[axis='col" + cn + "']", g.hDiv)[0];
  1374. var chk = 'checked="checked"';
  1375. if (kcol.style.display == 'none') {
  1376. chk = '';
  1377. }
  1378. $('tbody', g.nDiv).append('<tr><td class="ndcol1"><input type="checkbox" ' + chk + ' class="togCol" value="' + cn + '" /></td><td class="ndcol2">' + this.innerHTML + '</td></tr>');
  1379. cn++;
  1380. });
  1381. if (browser.msie && browser.version < 7.0) $('tr', g.nDiv).hover(function () {
  1382. $(this).addClass('ndcolover');
  1383. }, function () {
  1384. $(this).removeClass('ndcolover');
  1385. });
  1386. $('td.ndcol2', g.nDiv).click(function () {
  1387. if ($('input:checked', g.nDiv).length <= p.minColToggle && $(this).prev().find('input')[0].checked) return false;
  1388. return g.toggleCol($(this).prev().find('input').val());
  1389. });
  1390. $('input.togCol', g.nDiv).click(function () {
  1391. if ($('input:checked', g.nDiv).length < p.minColToggle && this.checked === false) return false;
  1392. $(this).parent().next().trigger('click');
  1393. });
  1394. $(g.gDiv).prepend(g.nDiv);
  1395. $(g.nBtn).addClass('nBtn')
  1396. .html('<div></div>')
  1397. .attr('title', 'Hide/Show Columns')
  1398. .click(function () {
  1399. $(g.nDiv).toggle();
  1400. return true;
  1401. }
  1402. );
  1403. if (p.showToggleBtn) {
  1404. $(g.gDiv).prepend(g.nBtn);
  1405. }
  1406. }
  1407. // add date edit layer
  1408. $(g.iDiv).addClass('iDiv').css({
  1409. display: 'none'
  1410. });
  1411. $(g.bDiv).append(g.iDiv);
  1412. // add flexigrid events
  1413. $(g.bDiv).hover(function () {
  1414. $(g.nDiv).hide();
  1415. $(g.nBtn).hide();
  1416. }, function () {
  1417. if (g.multisel) {
  1418. g.multisel = false;
  1419. }
  1420. });
  1421. $(g.gDiv).hover(function () {}, function () {
  1422. $(g.nDiv).hide();
  1423. $(g.nBtn).hide();
  1424. });
  1425. //add document events
  1426. $(document).mousemove(function (e) {
  1427. g.dragMove(e);
  1428. }).mouseup(function (e) {
  1429. g.dragEnd();
  1430. }).hover(function () {}, function () {
  1431. g.dragEnd();
  1432. });
  1433. //browser adjustments
  1434. if (browser.msie && browser.version < 7.0) {
  1435. $('.hDiv,.bDiv,.mDiv,.pDiv,.vGrip,.tDiv, .sDiv', g.gDiv).css({
  1436. width: '100%'
  1437. });
  1438. $(g.gDiv).addClass('ie6');
  1439. if (p.width != 'auto') {
  1440. $(g.gDiv).addClass('ie6fullwidthbug');
  1441. }
  1442. }
  1443. g.rePosDrag();
  1444. g.fixHeight();
  1445. //make grid functions accessible
  1446. t.p = p;
  1447. t.grid = g;
  1448. // load data
  1449. if (p.url && p.autoload) {
  1450. g.populate();
  1451. }
  1452. return t;
  1453. };
  1454. var docloaded = false;
  1455. $(document).ready(function () {
  1456. docloaded = true;
  1457. });
  1458. $.fn.flexigrid = function (p) {
  1459. return this.each(function () {
  1460. if (!docloaded) {
  1461. $(this).hide();
  1462. var t = this;
  1463. $(document).ready(function () {
  1464. $.addFlex(t, p);
  1465. });
  1466. } else {
  1467. $.addFlex(this, p);
  1468. }
  1469. });
  1470. }; //end flexigrid
  1471. $.fn.flexReload = function (p) { // function to reload grid
  1472. return this.each(function () {
  1473. if (this.grid && this.p.url) this.grid.populate();
  1474. });
  1475. }; //end flexReload
  1476. $.fn.flexOptions = function (p) { //function to update general options
  1477. return this.each(function () {
  1478. if (this.grid) $.extend(this.p, p);
  1479. });
  1480. }; //end flexOptions
  1481. $.fn.flexToggleCol = function (cid, visible) { // function to reload grid
  1482. return this.each(function () {
  1483. if (this.grid) this.grid.toggleCol(cid, visible);
  1484. });
  1485. }; //end flexToggleCol
  1486. $.fn.flexAddData = function (data) { // function to add data to grid
  1487. return this.each(function () {
  1488. if (this.grid) this.grid.addData(data);
  1489. });
  1490. };
  1491. $.fn.noSelect = function (p) { //no select plugin by me :-)
  1492. var prevent = (p === null) ? true : p;
  1493. if (prevent) {
  1494. return this.each(function () {
  1495. if (browser.msie || browser.safari) $(this).bind('selectstart', function () {
  1496. return false;
  1497. });
  1498. else if (browser.mozilla) {
  1499. $(this).css('MozUserSelect', 'none');
  1500. $('body').trigger('focus');
  1501. } else if (browser.opera) $(this).bind('mousedown', function () {
  1502. return false;
  1503. });
  1504. else $(this).attr('unselectable', 'on');
  1505. });
  1506. } else {
  1507. return this.each(function () {
  1508. if (browser.msie || browser.safari) $(this).unbind('selectstart');
  1509. else if (browser.mozilla) $(this).css('MozUserSelect', 'inherit');
  1510. else if (browser.opera) $(this).unbind('mousedown');
  1511. else $(this).removeAttr('unselectable', 'on');
  1512. });
  1513. }
  1514. }; //end noSelect
  1515. $.fn.flexSearch = function(p) { // function to search grid
  1516. return this.each( function() { if (this.grid&&this.p.searchitems) this.grid.doSearch(); });
  1517. }; //end flexSearch
  1518. })(jQuery);