您现在的位置: 365建站网 > 365文章 > 操作组合表头(js)

操作组合表头(js)

文章来源:365jz.com     点击数:376    更新时间:2009-11-09 22:40   参与评论

/**
 *--------------------------------------------------------
 * 表头列(cells,texts,rows,cols)
 * (tableObj,smartObj)
 *---------------------------------------------------------
 */
function TableHeader(){
    this.cells = new Array();
    this.texts = new Array();
}
TableHeader.prototype.setObject=function(tableObj){
    this.tableObj = tableObj;
}
TableHeader.prototype.toUI=function(){
    this.rows = this.cells.length;
    this.tableObj = document.createElement("TABLE");
    this.tableObj.className = "grid"; /////////////////////////////////
    var bottom_cells = new Array();
    for (var i=0; i<this.rows; i++){
        var row = this.tableObj.insertRow(-1);
        for (var j=0,cols=this.cells[i].length/2; j<cols; j++){
            var th = row.insertCell(-1);
            var rspan = this.cells[i][2*j];
            var cspan = this.cells[i][2*j+1];
            th.setAttribute("rowspan", rspan);
            th.setAttribute("colspan", cspan);
            th.align = "center";
            th.className = "grid-head resizableTdClass";
            th.innerHTML = "<span>"+this.texts[i][j]+"</span><span class='sort_symbol'>&nbsp;</span>";
            var preCols = 0;
            for (var k=0; k<j; k++){
                preCols += row.cells[k].colspan;
            }
            /*在同一行的顺序*/
            th.setAttribute("columnIndex", preCols);
            /*th.innerHTML += preCols;  debug*/
            if (rspan+i >= this.rows){
                bottom_cells.push(th);
            }
        }
        if (i > 0){
            var index = 0;
            row = this.tableObj.rows[i-1];
            var columns = row.cells;
            for (var j=0,len=columns.length; j<len; j++){
                var rowspan = columns[j].rowspan;
                if (row.rowIndex+rowspan > i){
                    index += columns[j].colspan;
                }else{
                    break;
                }
            }
            if (index > 0){
                columns = this.tableObj.rows[i].cells;
                for (var j=0,len=columns.length; j<len; j++){
                    columns[j].setAttribute("columnIndex", parseInt(columns[j].getAttribute("columnIndex"))+index);
                    /*columns[j].innerHTML += (columns[j].getAttribute("columnIndex"));  debug*/
                }
            }
        }
    }
    /*按colno排序*/
    bottom_cells.sort(function(obj1,obj2){
            return parseInt(obj1.getAttribute("columnIndex"))-parseInt(obj2.getAttribute("columnIndex"));
        });
    for (var i=0; i<this.cols; i++){
        bottom_cells[i].style.cursor = "default";
        bottom_cells[i].setAttribute("colno", i); /*columns的下标*/
        //bottom_cells[i].setAttribute("onclick", this.smartObj.loader+".clickHeader(event)");
    }

    this.tableObj.setAttribute("rownums", this.rows);
    this.tableObj.setAttribute("columns", this.cols);
    return this.tableObj;
}
TableHeader.prototype.setAttribute=function(name, value){
    this.tableObj.setAttribute(name, value);
};
/*new Array("(2,1,title)(1,2,title)","(1,1,title)(1,1,title)") */
TableHeader.prototype.buildRender=function(takeHolder){
    var arrays = eval(takeHolder);
    this.rows = arrays.length;
    this.cols = 0;
    for (var i=0,len=arrays.length; i<len; i++){
        var cells = arrays[i].split(")");
        var row_colspan = new Array();
        var title = new Array();
        for (var j=0,size=cells.length-1; j<size; j++){
            var cell_str = cells[j].substring(1, cells[j].length);
            cell_str = cell_str.split(",");
            if (cell_str.length == 1){
                row_colspan.push(1);
                row_colspan.push(1);
                title.push(cell_str[0]);
            }else if (cell_str.length == 3){
                row_colspan.push(parseInt(cell_str[0],10));
                row_colspan.push(parseInt(cell_str[1],10));
                title.push(cell_str[2]);
            }
            if (i == 0){/*row0 colspan*/
                this.cols += parseInt(cell_str[1]);
            }
        }
        this.cells.push(row_colspan);
        this.texts.push(title);
    }
};
/**
 * 得到某个实际位置的单元格
 */
TableHeader.prototype.getCell=function(rowno, colno){
    var cells = this.tableObj.rows[rowno].cells;
    for (var j=0,len=cells.length; j<len; j++){
        index = parseInt(cells[j].getAttribute("columnIndex"));
        if (index == colno){
            return cells[j];
        }else if (!isNaN(cells[j].colspan) && cells[j].colspan>1){
            if (index+cells[j].colspan > colno){
                return cells[j];
            }
        }
    }
    return null;
}
/**
 * 删除一列
 */
TableHeader.prototype.deleteColumn=function(colno){
    var rownum = this.tableObj.rows.length;
    var cells,index,len;
    for (var i=0; i<rownum; i++){
        cells = this.tableObj.rows[i].cells;
        len = cells.length;
        for (var j=0; j<len; j++){
            index = parseInt(cells[j].getAttribute("columnIndex"));
            if (index == colno){
                if (cells[j].colspan && cells[j].colspan>1){
                    cells[j].colspan --;
                }else{
                    this.tableObj.rows[i].deleteCell[j];
                }
                break;
            }else if (index<colno && j<len-1){
                if (parseInt(cells[j].getAttribute("columnIndex")) > colno){
                    cells[j].colspan --;
                    break;
                }
            }
        }alert(j);
        for (var k=j+1; k<len; k++){
            cells[k].setAttribute("columnIndex", parseInt(cells[k].getAttribute("columnIndex"))-1);
        }
    }
}
/**
 * 删除一行
 */
TableHeader.prototype.deleteRow=function(rowno){
    var rownum = this.tableObj.rows.length;
    if (rowno < 0){
        rowno = 0;
    }else if (rowno > rownum-1){
        rowno = rownum - 1;
    }
    this.tableObj.deleteRow(rowno);
    for (var i=rowno-1; i>=0; i--){
        var cells = this.tableObj.rows[i].cells;
        var rowIndex = this.tableObj.rows[i].rowIndex;
        for (var j=0,len=cells.length; j<len; j++){
            if (cells[j].rowspan && cells[j].rowspan+rowIndex>rowno){
                cells[j].rowspan --;
            }
        }
    }
}
/**
 * 在(endRow+1,colno)处添加一“小计”行
 */
TableHeader.prototype.subTotalRow=function(caption,colno,colspan, startRow,endRow, cssClass){
    var formula = "";
    for (var i=startRow; i<=endRow; i++){
        formula += ("$"+i)+"+";
    }
    formula = formula.substring(0, formula.length-1);
    this.addStatRow(caption,formula, endRow+1,colno,colspan, cssClass);
};
TableHeader.prototype.addStatRow=function(caption,formula, rowno,colno,colspan, cssClass){
    var items = formula.split2("+-*/%()");
    items.sort(function(str1,str2){return str1.length-str2.length;});
    var copied = formula;

    var row = this.tableObj.insertRow(rowno);
    var td;
    /*改变前面单元格的rowspan*/
    for (var i=rowno-1; i>=0; i--){
        td = this.getCell(i,colno-1);
        if (td != null){
            if (td.rowspan+this.tableObj.rows[i].rowIndex > rowno-1){
                td.rowspan += 1;
            }
        }
    }

    td = row.insertCell(-1);
    td.innerText = caption;
    td.setAttribute("colspan", colspan);
    td.setAttribute("columnIndex", colno);
    if (cssClass)   td.className = cssClass;
    td.style.textAlign = "center";

    var start = colno + colspan;
    var cells = this.tableObj.rows[rowno-1].cells;
    td = cells[cells.length-1];
    var loop = parseInt(td.getAttribute("columnIndex")) + 1;  /*总列数*/
    if (td.colspan != undefined && td.colspan>1){
        loop = loop + td.colspan - 1;
    }
    for (var j=start; j<loop; j++){
        td = row.insertCell(-1);
        copied = formula;
        for (var i=0,len=items.length; i<len; i++){
            rowno = parseInt(items[i].substring(1,items[i].length));
            copied = copied.replace(items[i], parseFloat(this.getCell(rowno,j).innerText));
        }
        td.innerText = eval(copied);
        td.setAttribute("columnIndex", j);
        if (cssClass)   td.className = cssClass;
    }
};

/**
 * 在第(rowno,endCol+1)行出插入一“小计”列
 */
TableHeader.prototype.subTotalColumn=function(caption,rowno,rowspan, startCol,endCol, cssClass){
    var formula = "";
    for (var i=startCol; i<=endCol; i++){
        formula += ("$"+i)+"+";
    }
    formula = formula.substring(0, formula.length-1);
    this.addStatColumn(caption,formula, rowno,endCol+1,rowspan, cssClass);
};
TableHeader.prototype.addStatColumn=function(caption,formula, rowno,colno,rowspan, cssClass){
    var items = formula.split2("+-*/%()");
    items.sort(function(str1,str2){return str1.length-str2.length;});
    var copied = formula;

    var td, cells, index;
    var endCol = colno-1;
    if (rowno > 0){
        /*改变前面行单元格的colspan*/
        for (var i=rowno-1; i>=0; i--){
            td = this.getCell(i, endCol);
            if (td != null){
                td.colspan += 1;
            }
        }
    }
    index = this.getCell(rowno,endCol).cellIndex + 1;
    td = this.tableObj.rows[rowno].insertCell(index);
    td.innerText = caption;
    td.rowspan = rowspan;
    if (cssClass != undefined)      td.className = cssClass;
    td.style.textAlign = "center";

    var startRow = rowno + rowspan;
    var rownum = this.tableObj.rows.length;
    for (var i=startRow; i<rownum; i++){
        copied = formula;
        for (var j=0,len=items.length; j<len; j++){
            colno = parseInt(items[j].substring(1,items[j].length));
            copied = copied.replace(items[j], parseFloat(this.getCell(i,colno).innerText));
        }
        index = this.getCell(i,endCol).cellIndex + 1;
        td = this.tableObj.rows[i].insertCell(index);
        td.innerText = eval(copied);
        if (cssClass != undefined)      td.className = cssClass;
    }
    for (var i=0; i<rownum; i++){
        td = this.getCell(i,endCol);
        if (td != null){
            var cells = this.tableObj.rows[i].cells;
            for (var j=td.cellIndex+1,len=cells.length; j<len; j++){
                index = cells[j].getAttribute("columnIndex");
                if (index == undefined)     index = endCol;
                cells[j].setAttribute("columnIndex", parseInt(index)+1);
            }
        }
    }
};
/**
 * 保留固定小数位
 * @param startRow,endRow:行范围
 * @param endColumn,endColumn:列范围
 * @param bits:小数位
 * @param scientiflag:显示科学记数法
 */
TableHeader.prototype.formatNumbit=function(startRow,endRow,startColumn,endColumn,bits, scientiflag){
    var td, value;
    var fact = Math.pow(10,bits);
    for (var i=startRow; i<=endRow; i++){
        for (var j=startColumn; j<=endColumn; j++){
            td = this.getCell(i, j);;
            if (!isNaN(td.innerText)){
                value = formatNumbit(td.innerText, bits);
                if (scientiflag){
                    value = dispScientific(value);
                }
                td.innerText = value;
            }
        }
    }
};

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (376人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号