2006-08-03

Dom  Javascript 在本机浏览器中对的表格数据进行排序

来源: CSDN 作者:lzid2008 评论 0 条
 
*/
function sortTableInRange(tableId,sortCol,compareType,startRow,endRow,startCol,endCol){


try{
var table = document.getElementById(tableId);
// get all row object of the table
var objRows = table.getElementsByTagName("tr");
//alert(objRows.length);

endRow = (endRow < objRows.length ? endRow : objRows.length);

var sortRows = endRow - startRow 1;
//alert("sortRows " sortRows);
if (sortRows < 2) //only one line,don't sort
return ;


endCol = (endCol < objRows[1].getElementsByTagName("td").length ? endCol :
objRows[1].getElementsByTagName("td").length);


// column number of sort
//var cols = objRows[1].childNodes.length;
var cols = endCol - startCol 1;



// define a array to store table cell and sort them
var tabData = new BiArray(sortRows,cols);


var ari = 0;
// retrived table cell data save to array
for(i = startRow - 1; i < endRow; i ){
//retrived all <td> cell
var cells = objRows[i].getElementsByTagName("td");

var arj = 0;
for(var j = startCol - 1; j < endCol; j ){
tabData.setElement(ari,arj,cells.item(j).innerHTML);
arj ;

}
ari ;
}

if (asce){
if (compareType == "n" || compareType == 'N')
tabData.sortLineByNumber(sortCol- startCol);
else
tabData.sortLine(sortCol - startCol);
asce = false;
}else{
if (compareType == "n" || compareType == 'N')
tabData.sortLineByNumberRev(sortCol - startCol);
else
tabData.sortLineRev(sortCol - startCol);
asce = true;
}
ari = 0;
//update table data with array
for(i = startRow -1; i < endRow; i ){
//retrived all <td> cell
var cells = objRows[i].getElementsByTagName("td");

arj = 0;
for(var j = startCol - 1; j < endCol; j ){
cells.item(j).innerHTML = tabData.getElement(ari,arj);
arj ;
}
ari ;
}

}catch(e){
alert(e);
}
}

/**
对表格除第一行外的数据行排序,是sortYableInRange(tableId,sortCol,compareType,2,tabRows,1,tabCols)
的特例。
tableId 要排序的表格的id,值格式为 <table id="tb1" >
用于排序的列号,从1开始计数
compareType 排序时比较方式,s-按字符串比较,n-按数值比较
*/
function sortTable(tableId,sortCol,compareType){


try{
var table = document.getElementById(tableId);
// get all row object of the table
var objRows = table.getElementsByTagName("tr");
//alert(objRows.length);


共6页: 上一页 [1] [2] [3] 4 [5] [6] 下一页

(本文仅表明作者个人观点,不代表本站及其管理员立场.) 推荐 收藏 投稿 打印 返回 关闭
上一篇:取消网页各种禁止功能的解决方案  
下一篇:Lightbox JS v2.0
    评论加载中…

网站首页  -  网站地图 -   站长论坛  -  网站投稿  -    -  网站管理
Copyright © 2008 芜湖站长站 All Rights Reserved 皖ICP备07500611号