自己做的js(三)

来源:互联网 发布:sigmasoft软件 编辑:程序博客网 时间:2024/05/23 19:17

//表动态添加方法
function TableProperty(tname, tableobj1)
{
    var tableobjname, tbname = "", otb, itemcount, inputClassArray, inputReadOnlyArray, inputNeedArray, nowcellnum = 0;
   
    this.tableobjname = tableobj1;
   
    this.tbname = tname + "table";
   
    this.otb = $(this.tbname);
   
    this.itemcount = 1;
   
    this.inputClassArray = null;
   
    this.inputReadOnlyArray = null;
   
    this.inputNeedArray = null;
   
    this.nowcellnum = 0;
}

//格式化内容
TableProperty.prototype.FormatTable = function(inputArray1, inputArray2, inputArray3)
{
    this.inputClassArray = inputArray1;
    this.inputReadOnlyArray = inputArray2;
    this.inputNeedArray = inputArray3;
}

//添加一行
TableProperty.prototype.AddRow = function()
{
    var otr = this.otb.insertRow(this.itemcount);
    otr.id = this.tbname + "_row_" + this.itemcount;
    otr.style.backgroundColor = "white";
    this.itemcount = this.itemcount + 1;
    this.nowcellnum = 0;
}

//填写一行
TableProperty.prototype.SetTable = function()
{
    var formatintext = new FormatInputText();
    for(var i = this.nowcellnum; i<this.inputClassArray.length; i++)
    {
        var ocell = this.otb.rows(this.itemcount - 1).insertCell(i);
        var readonly = "";
        if(this.inputReadOnlyArray[i] == "true")
            readonly = "true";
        switch (this.inputClassArray[i])
        {
            case "numpoint":
                ocell.innerHTML = formatintext.getNumPoint(this.tbname + "_row_" + (this.itemcount - 1) + "_cell_" + (i + 1), "width: 97%; background-color: white;", "input2", readonly, (this.itemcount - 1).toString());
                break;
            case "common":
                ocell.innerHTML = formatintext.getCommonText(this.tbname + "_row_" + (this.itemcount - 1) + "_cell_" + (i + 1), "width: 97%; background-color: white;", "input2", readonly);
                break;
            default:
                break;
        }
        ocell.className = cellclass;
        this.nowcellnum++;
    }
}

//添加删除按钮
TableProperty.prototype.AddDelButton = function()
{
    var ocell = this.otb.rows(this.itemcount - 1).insertCell(this.nowcellnum);
    ocell.className = cellclass;
    ocell.style.textAlign = "center";
    ocell.innerHTML = "<img src='" + delImgAddress + "' onclick=/"" + this.tableobjname + ".DelRow(" + (this.itemcount - 1) + ")/"></img>";
    this.nowcellnum++;
}

//删除一行
TableProperty.prototype.DelRow = function(rownum)
{
    for(var i = rownum; i<this.itemcount - 1; i++)
    {
        for(var j = 0; j<this.inputClassArray.length; j++)
        {
            $(this.tbname + "_row_" + i + "_cell_" + (j + 1)).value = $(this.tbname + "_row_" + (i + 1) + "_cell_" + (j + 1)).value;
        }
    }
    this.otb.deleteRow(this.itemcount - 1);
    this.itemcount--;
}

//删除所有行
TableProperty.prototype.DelAllRow = function()
{
    for(var i = this.itemcount - 1; i>0; i--)
    {
        this.otb.deleteRow(i);
        this.itemcount--;
    }
    this.nowcellnum = 0;
}

//读取表的值
TableProperty.prototype.readtablecont = function()
{
    var tablecont = "";
    var rowcount = this.otb.rows.length;
    for(var i = 0; i < this.itemcount - 1; i++)
    {
        for(var j = 0; j < this.inputClassArray.length; j++)
        {
            tablecont = tablecont + Trim($(this.tbname + "_row_" + (i + 1) + "_cell_" + (j + 1)).value);
            if(j == this.inputClassArray.length - 1)
            {
                tablecont = tablecont + rowspl;
            }
            else
            {
                tablecont = tablecont + cellspl;
            }
        }
    }
    return tablecont;
}

//检查必填项
TableProperty.prototype.checkNeedInput = function()
{
    for(var i = 1; i<this.itemcount; i++)
    {
        for(var j = 0; j<this.inputClassArray.length; j++)
        {
            if(this.inputNeedArray[j] == "true")
            {
                if($(this.tbname + "_row_" + i + "_cell_" + (j + 1)).value == "") return false;
            }
        }
    }
    return true;
}

//普通增加行
TableProperty.prototype.commonAdd = function()
{
    this.AddRow();
    this.SetTable();
    this.AddDelButton();
}

//增加内容
TableProperty.prototype.addContext = function(tablecont)
{
    //alert(tablecont);
    this.rowcont = tablecont.split(rowspl);
    if(this.rowcont[0] == "") this.rownum = 0;
    else if(this.rowcont[1] == "") this.rownum = 1;
    else this.rownum = this.rowcont.length;
    for(var i = 0; i<this.rowcont.length; i++)
    {
        this.AddRow();
        this.SetTable();
        this.AddDelButton();
        var cellcont = this.rowcont[i].split(cellspl);
        for(var j = 0; j<cellcont.length; j++)
        {
            $(this.tbname + "_row_" + (i + 1) + "_cell_" + (j + 1)).value = cellcont[j];
        }
    }

原创粉丝点击