• Home
  • Pictures
  • Portfolio
  • codez
  • comics
  • craft
  • contact
  • Rings
 
Sorting Tool (js) PDF Print E-mail
Written by alex4u2nv   
Wednesday, 14 November 2007 11:34
/**
* @author Alexander D. Mahabir
* @brief: table tools
*/
if (WebTools == undefined) var WebTools = new Object();


WebTools.Table = function (element,offset,tbody)
{
var that = this;
this.offset = (offset == null || offset==undefined)?0:offset;
this.table = element;
this.tbody = (tbody == null || tbody==undefined)?0:tbody;
this.tBody = this.table.tBodies[tbody];
this.left = null;
this.right = null;
this.tog = false;
this.classNames = new Array();
this.rows = new Array();
this.getRows(offset);
};


/**
* @Modified By Alexander D. Mahabir
* @brief: sort by Column
*/
WebTools.Table.prototype.sort = function (col,offset,tbody)
{
var index=null
var tarr=null;

this.tBody=this.table.tBodies[tbody];
this.tbody=tbody;
WebTools.Sort.compare = this.compare;
WebTools.Sort.eq = this.eq;
WebTools.Sort.lt = this.lt;
WebTools.Sort.gt = this.gt;
WebTools.Sort.gte = this.gte;
WebTools.Sort.lte = this.lte;

//tarr = WebTools.Sort.heapsort(this.rows, col);
tarr = WebTools.Sort.fastQsort(this.rows,col,offset);
this.rePop(tarr);

};

/**
* @Modified By Alexander D. Mahabir
* @brief: reversing the table
*/

WebTools.Table.prototype.reverse = function (offset)
{
var tarr =null;
var index = null;
tarr = WebTools.Sort.reverse(this.rows,offset);
this.rePop(tarr);

}
WebTools.Table.prototype.rePop=function (tarr)
{
//var b2=new bench("populating");
//this is slow. Need to find a better way to re-populate the table.
var tbody= document.createElement("tbody");
for (var i=0; i<tarr.length; i++)
{
tarr[i].className = this.classNames[i];
tbody.appendChild(tarr[i]);
//if (!confirm(this.tBody.rows[i] + "\n" + tarr[i]))exit;
}
//this.table.removeChild(this.tBody);
//this.table.insertBefore(tbody, this.tBody);
this.table.replaceChild(tbody, this.table.tBodies[this.tbody]);
//b2.stop_time();

}
WebTools.Table.prototype.getRows = function ()
{
var index=null;
this.rows = new Array();
this.classNames = new Array();
for (var i=this.offset; i< this.tBody.rows.length; i++)
{
index= i-this.offset;
this.rows[index] = this.tBody.rows[i];
this.classNames[index]=this.tBody.rows[i].className;
}
}

WebTools.Table.prototype.compare = function (a, b, col)
{
if (a.cells[col].innerHTML < b.cells[col].innerHTML)
return -1;
if (a.cells[col].innerHTML == b.cells[col].innerHTML)
return 0;
if (a.cells[col].innerHTML > b.cells[col].innerHTML)
return 1;
}

WebTools.Table.prototype.eq = function (a,b,col) {
return (a.cells[col].innerHTML == b.cells[col].innerHTML) ;
}

WebTools.Table.prototype.lt = function (a,b,col) {
return (a.cells[col].innerHTML < b.cells[col].innerHTML );
}

WebTools.Table.prototype.gt = function (a,b,col) {
return (a.cells[col].innerHTML > b.cells[col].innerHTML) ;
}

WebTools.Table.prototype.gte = function (a,b,col) {
return (a.cells[col].innerHTML >= b.cells[col].innerHTML) ;
}

WebTools.Table.prototype.lte = function (a,b,col) {
return (a.cells[col].innerHTML <= b.cells[col].innerHTML) ;
}

WebTools.Table.prototype.hideColumn = function (col,tbody)
{
for (var i=0; i< this.table.tBodies[tbody].rows.length; i++)
if (this.table.tBodies[tbody].rows[i].cells.length > col)
this.table.tBodies[tbody].rows[i].cells[col].style.display="none";
}

WebTools.Table.prototype.showColumn = function (col,tbody)
{
for (var i=0; i< this.table.tBodies[tbody].rows.length; i++)
if (this.table.tBodies[tbody].rows[i].cells.length > col)
this.table.tBodies[tbody].rows[i].cells[col].style.display="";
}
Last Updated ( Thursday, 24 January 2008 14:52 )