Guest User

mjsilva

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