交换HTML表行时更新行ID

时间:2013-11-06 14:32:46

标签: javascript html dom

过去几周我一直在搞乱操纵HTML表格,我遇到了一个问题,我不知道如何修复。因此,表的行集合可以像数组一样迭代,但是如果你已经多次关闭了行,那么不会混合ID,浏览器也不依赖于ID作为迭代的方式在行对象?我遇到了一个问题(可能是因为缺乏理解),行最终会停止移动,或者一行在另一行上重复。我应该以某种方式每次移动时更新行的ID吗?这是迄今为止这个函数的源代码。

    function swap(rOne, rTwo, tblID) {
    tblID.rows[rOne].setAttribute('style', 'background-color:#FFFFFF');
    var tBody = tblID.children[0];
    var rowOne;
    var rowTwo;
    if (rOne > rTwo) {
        rowOne = rOne;
        rowTwo = rTwo;
    }
    else {
        rowOne = rTwo;
        rowTwo = rOne;
    }        
    var swapTempOne = tblID.rows[rowOne].cloneNode(true);
    var swapTempTwo = tblID.rows[rowTwo].cloneNode(true);        
    hiddenTable.appendChild(swapTempOne);
    hiddenTable.appendChild(swapTempTwo);           
    tblID.deleteRow(rowOne);
    var rowOneInsert = tblID.insertRow(rowOne);
    var rowOneCellZero = rowOneInsert.insertCell(0);
    var rowOneCellOne = rowOneInsert.insertCell(1);
    var rowOneCellTwo = rowOneInsert.insertCell(2);
    var rowOneCellThree = rowOneInsert.insertCell(3);
    rowOneCellZero.innerHTML = hiddenTable.rows[2].cells[0].innerHTML;
    rowOneCellOne.innerHTML = hiddenTable.rows[2].cells[1].innerHTML;
    rowOneCellTwo.innerHTML = hiddenTable.rows[2].cells[2].innerHTML;
    rowOneCellThree.innerHTML = hiddenTable.rows[2].cells[3].innerHTML;
    tblID.deleteRow(rowTwo);
    var rowTwoInsert = tblID.insertRow(rowTwo);
    var rowTwoCellZero = rowTwoInsert.insertCell(0);
    var rowTwoCellOne = rowTwoInsert.insertCell(1);
    var rowTwoCellTwo = rowTwoInsert.insertCell(2);
    var rowTwoCellThree = rowTwoInsert.insertCell(3);
    rowTwoCellZero.innerHTML = hiddenTable.rows[1].cells[0].innerHTML;
    rowTwoCellOne.innerHTML = hiddenTable.rows[1].cells[1].innerHTML;
    rowTwoCellTwo.innerHTML = hiddenTable.rows[1].cells[2].innerHTML;
    rowTwoCellThree.innerHTML = hiddenTable.rows[1].cells[3].innerHTML;      
    tblID.rows[rowOne].setAttribute('onclick', 'chkThis(event, this)');
    tblID.rows[rowTwo].setAttribute('onclick', 'chkThis(event, this)');
    for (iHiddenDelete = 2; iHiddenDelete >= 1; iHiddenDelete--) {
        hiddenTable.deleteRow(iHiddenDelete);
    }
}

编辑:为页面添加HTML以及在表格之间移动的功能,我怀疑这会导致问题。

    <body>
<form>    
<input value="0" type="text" id="cubesum" size="5"/>
<input value="0" type="text" id="wgtsum" size="5"/>
</form>
<form>
<table id="tblSource">
<thead>
    <tr>        
        <th>&nbsp;</th>
        <th>Order</th>
        <th>Cube</th>
        <th>Weight</th>
        <th>Move</th>
    </tr>
</thead>    
<tbody>

</tbody>
</table>
<button type="button" onclick="move('tblSource','tblTarget')" style="width: 58px">To Trucks (Down)</button>
<button type="button" onclick="move('tblTarget', 'tblSource')" style="width: 58px">To Orders (Up)</button>
</form>
<form>
<table id="tblTarget">
<thead>
    <tr>        
        <th>&nbsp;</th>
        <th>Order</th>
        <th>Cube</th>
        <th>Weight</th>
        <th>Move</th>
    </tr>
</thead>
<tbody>

</tbody>
</table>
</form>
<table id="hiddenTable" style="display: none"> <!--this table is hidden! -->
<thead>
    <tr>        
        <th>&nbsp;</th>
        <th>Order</th>
        <th>Cube</th>
        <th>Weight</th>
        <th>Move</th>
    </tr>
</thead>
<tbody>

</tbody>
</table>
</body>
</html>

功能在这里开始

    function move(from, to) {
    var frTbl = document.getElementById(from);
    var toTbl = document.getElementById(to);        
    chkArray.length = 0;
    cbsMove = frTbl.getElementsByTagName('input');
    for (var oChk = 0; oChk < cbsMove.length; oChk++) {
        if (cbsMove[oChk].type == 'checkbox') {
            if (cbsMove[oChk].checked == true) {
               var prntRow = cbsMove[oChk].parentNode.parentNode;
               var prntRowIdx = prntRow.rowIndex;
                chkArray.push(prntRowIdx);
                cbsMove[oChk].checked = false;
            }
        }
    }

    for (iMove = chkArray.length -1; iMove >= 0; iMove--) {
        var num = chkArray[iMove];
        var row = frTbl.rows[num];
        var cln = row.cloneNode(true);
        toTbl.appendChild(cln);
        frTbl.deleteRow(num);
    }
    sum();
}

1 个答案:

答案 0 :(得分:0)

事实证明,我在表之间移动的行克隆导致格式错误的HTML,其中行不再在表体标签内。此外,不信任浏览器跟踪按钮ID并使用按钮ID将setAttributes设置为按钮也最终导致按钮ID重叠。所以,我摆脱了节点克隆,并在表之间以手动方式移动行,并使用innerHTML在按钮内添加函数调用。经过进一步的反思,我开始了解到有些人实际上创建了处理所有按钮点击的功能而没有在按钮内调用它们,并根据ID或其他因素(如按钮的父节点)将它们路由到正确的功能。也许这是最好的。这里的主要技巧是STICK TO ONE METHOD。我操纵桌子的方式到处都是,它打破了东西。以下是那些希望做类似事情的人的工作来源。

      <!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />

    <style type="text/css">
        #selectSource {
            width: 320px;
        }
        #selectTarget {
            width: 320px;
        }
        table, th, td
        {
            border: 1px solid black;
        }
    </style>
    <title>Loader</title>

<script>
    var chkArray = [];
    var data = [];
    var tmpArray = [];
    var iChk = 0;
    var swap;

    window.onload = function () {

        var load = document.getElementById('selectSource')

        loadFromAJAX();
    }

    function loadFromAJAX()
    {
        var xmlhttp;
        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
             xmlhttp=new XMLHttpRequest();
          }
        else
        {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                var rawData = xmlhttp.responseText;
                data = JSON.parse(rawData);

                for (iData = 0; iData < data.length; iData++) {
                    newRow = document.getElementById('tblSource').insertRow(iData + 1);
                    var dn = "dn" + (iData + 1);
                    var up = "up" + (iData + 1);
                    cel0 = newRow.insertCell(0);
                    cel1 = newRow.insertCell(1);
                    cel2 = newRow.insertCell(2);
                    cel3 = newRow.insertCell(3);
                    cel4 = newRow.insertCell(4);
                    cel0.innerHTML = "<input type='checkbox' name='chkbox'>";
                    cel1.innerHTML = data[iData].num;
                    cel2.innerHTML = data[iData].cube;
                    cel3.innerHTML = data[iData].wgt;
                    cel4.innerHTML = "<button type='button' onclick=moveUp(this)>up</button><button type='button' onclick=moveDn(this)>down</button>";
                }
            }
        }
        xmlhttp.open("POST","http://192.168.3.2/cgi-bin/rims50.cgi/json.p",true);
        xmlhttp.send();
    }    

    function moveUp(mvThisRow) {
        var mvThisRowRow = mvThisRow.parentNode.parentNode;
        var mvThisRowTbl = mvThisRowRow.parentNode.parentNode;
        var mvThisRowIndex = mvThisRowRow.rowIndex;
        var mvThisRowTblLngth = mvThisRowTbl.rows.length;
        var mvFrRow = mvThisRowTbl.rows[mvThisRowIndex];
        var mvToRow = mvThisRowIndex - 1;        
        var mvThisDn = "dn" + (mvToRow) + mvThisRowTbl;
        var mvThisUp = "up" + (mvToRow) + mvThisRowTbl;
        if (mvThisRowIndex - 1 !== 0) {
            moveToRow = mvThisRowTbl.insertRow(mvToRow);
            mvRowCel0 = moveToRow.insertCell(0);
            mvRowCel1 = moveToRow.insertCell(1);
            mvRowCel2 = moveToRow.insertCell(2);
            mvRowCel3 = moveToRow.insertCell(3);
            mvRowCel4 = moveToRow.insertCell(4);
            mvRowCel0.innerHTML = "<input type='checkbox' name='chkbox'>";
            mvRowCel1.innerHTML = mvFrRow.cells[1].innerHTML;
            mvRowCel2.innerHTML = mvFrRow.cells[2].innerHTML;
            mvRowCel3.innerHTML = mvFrRow.cells[3].innerHTML;
            mvRowCel4.innerHTML = "<button type='button' onclick=moveUp(this)>up</button><button type='button' onclick=moveDn(this)>down</button>";
            mvThisRowTbl.deleteRow(mvThisRowIndex +1);
        }
        else {
            alert("You can't move the top row 'up' try moving it 'down'.");
        }

    }

    function moveDn(mvThisRow) {
        var mvThisRowRow = mvThisRow.parentNode.parentNode;
        var mvThisRowTbl = mvThisRowRow.parentNode.parentNode;
        var mvThisRowTblLngth = mvThisRowTbl.rows.length;
        var mvThisRowIndex = mvThisRowRow.rowIndex;        
        if (mvThisRowIndex + 1 !== mvThisRowTblLngth) {
            var mvFrRow = mvThisRowTbl.rows[mvThisRowIndex];
            var mvToRow = mvThisRowIndex + 2;
            var moveToRow = mvThisRowTbl.insertRow(mvToRow);
            var dn = "dn" + (mvToRow) + mvThisRowTbl;
            var up = "up" + (mvToRow) + mvThisRowTbl;
            mvRowCel0 = moveToRow.insertCell(0);
            mvRowCel1 = moveToRow.insertCell(1);
            mvRowCel2 = moveToRow.insertCell(2);
            mvRowCel3 = moveToRow.insertCell(3);
            mvRowCel4 = moveToRow.insertCell(4);            
            mvRowCel0.innerHTML = "<input type='checkbox' name='chkbox'>";
            mvRowCel1.innerHTML = mvFrRow.cells[1].innerHTML;
            mvRowCel2.innerHTML = mvFrRow.cells[2].innerHTML;
            mvRowCel3.innerHTML = mvFrRow.cells[3].innerHTML;
            mvRowCel4.innerHTML = "<button type='button' onclick=moveUp(this)>up</button><button type='button' onclick=moveDn(this)>down</button>";
            mvThisRowTbl.deleteRow(mvThisRowIndex);
        }
        else {
            alert("You can't move the bottom row 'down' try moving it 'up'.");
        }
    }

    function sum() {
        var trgTbl = document.getElementById('tblTarget');
        var tblLength = trgTbl.rows.length;
        var sumAddCube = 0;
        var sumAddWgt = 0;
        document.getElementById("cubesum").setAttribute("value", sumAddCube);
        document.getElementById("wgtsum").setAttribute("value", sumAddWgt);

            for (iSum = 1; iSum < tblLength; iSum++) {
                celCubeNum = trgTbl.rows[iSum].cells[2].innerHTML;
                celWgtNum = trgTbl.rows[iSum].cells[3].innerHTML;
                sumAddCube = parseInt(sumAddCube) + parseInt(celCubeNum);
                sumAddWgt = parseInt(sumAddWgt) + parseInt(celWgtNum);
            }
        document.getElementById("cubesum").setAttribute("value", sumAddCube);
        document.getElementById("wgtsum").setAttribute("value", sumAddWgt);
    }

    function move(from, to) {
        var frTbl = document.getElementById(from);
        var toTbl = document.getElementById(to);        
        chkArray.length = 0;
        cbsMove = frTbl.getElementsByTagName('input');
        for (var oChk = 0; oChk < cbsMove.length; oChk++) {
            if (cbsMove[oChk].type == 'checkbox') {
                if (cbsMove[oChk].checked == true) {
                   var prntRow = cbsMove[oChk].parentNode.parentNode;
                   var prntRowIdx = prntRow.rowIndex;
                    chkArray.push(prntRowIdx);
                    cbsMove[oChk].checked = false;
                }
            }
        }

        for (iMove = chkArray.length -1; iMove >= 0; iMove--) {
            var num = chkArray[iMove];
            var row = frTbl.rows[num];
            var toRow = toTbl.rows.length
            moveRow = toTbl.insertRow(toRow);
            var dn = "dn" + (toRow) + toTbl;
            var up = "up" + (toRow) + toTbl;
            mvCel0 = moveRow.insertCell(0);
            mvCel1 = moveRow.insertCell(1);
            mvCel2 = moveRow.insertCell(2);
            mvCel3 = moveRow.insertCell(3);
            mvCel4 = moveRow.insertCell(4);
            mvCel0.innerHTML = "<input type='checkbox' name='chkbox'>";
            mvCel1.innerHTML = row.cells[1].innerHTML;
            mvCel2.innerHTML = row.cells[2].innerHTML;
            mvCel3.innerHTML = row.cells[3].innerHTML;
            mvCel4.innerHTML = "<button type='button' onclick=moveUp(this)>up</button><button type='button' onclick=moveDn(this)>down</button>";
            frTbl.deleteRow(num);            
        }
        sum();
    }

</script>
</head>
<body>
    <form>    
<input value="0" type="text" id="cubesum" size="5"/>
<input value="0" type="text" id="wgtsum" size="5"/>
</form>
<form>
<table id="tblSource">
<thead>
    <tr>        
        <th>&nbsp;</th>
        <th>Order</th>
        <th>Cube</th>
        <th>Weight</th>
        <th>Move</th>
    </tr>
</thead>    
<tbody>

</tbody>
</table>
<button type="button" onclick="move('tblSource','tblTarget')" style="width: 58px">To Trucks (Down)</button>
<button type="button" onclick="move('tblTarget', 'tblSource')" style="width: 58px">To Orders (Up)</button>
</form>
<form>
<table id="tblTarget">
<thead>
    <tr>        
        <th>&nbsp;</th>
        <th>Order</th>
        <th>Cube</th>
        <th>Weight</th>
        <th>Move</th>
    </tr>
</thead>
<tbody>

</tbody>
</table>
</form>
<table id="hiddenTable" style="display: none"> <!--this table is hidden! -->
<thead>
    <tr>        
        <th>&nbsp;</th>
        <th>Order</th>
        <th>Cube</th>
        <th>Weight</th>
        <th>Move</th>
    </tr>
</thead>
<tbody>

</tbody>
</table>
</body>
</html>