隐藏行时如何重新排序行

时间:2013-02-19 17:07:03

标签: javascript

我想根据隐藏的字段重新排序,我将隐藏的字段区域zone_deleted设置为1,当它被隐藏时将其标记为已删除。

function insertZone() {

    var table = document.getElementById('zoneItems');

    var e = document.getElementById('zone');
    var number = e.options[e.selectedIndex].value;
    var rowcount = document.getElementById('zoneItems').rows.length;

    if(rowcount == 0){
        var x = table.insertRow(-1);

        var head1 = x.insertCell(0);
        head1.innerHTML = "Zone";

        var head2 = x.insertCell(1);
        head2.innerHTML = "Motor Reference Point";

        var head3 = x.insertCell(2);
        head3.innerHTML = " ";

    }

        for (var i=0; i < number; i++)
        {
            var x = table.insertRow(-1);
            var a = x.insertCell(0);
            //a.innerHTML = "<input type='hidden' name='zone_id' value='' /> ";

            num = i+1;

            var b = x.insertCell(1);
            b.innerHTML = "<input type='text' name='zone_description[]' size='18' value=''/><input id='zone"+num+"' type='hidden' name='zone_deleted[]' value='0' />";

            var c = x.insertCell(2);
            c.innerHTML = "<button type='button' class='button lastChild' onclick='removeZone(this)' ><img src='themes/default/images/id-ff-clear.png'></button>";
        }

        //set the row ordering
        count();

}

function removeZone(rows) {

    var _row = rows.parentElement.parentElement;

    _row.cells[1].getElementsByTagName('input')[1].value = '1';

    document.getElementById('zoneItems').rows[_row.rowIndex].style.display = 'none';

    count();


}

function count(){ //sets the row ordering

    var table = document.getElementById("zoneItems");
    var tbody = table.tBodies[0];

    for (var i = 0, row; row = tbody.rows[i]; i++) { //loop through rows

        if(i != 0){//if not the first row

           // var deleted = row.cells[1].getElementsByTagName('input')[1].value;


            for (var j = 0, col; col = row.cells[j]; j++) {//loop through cols


                if(j == 0){//insert into only the first td
                    col.innerHTML = "<span>" +i+ "</span><input type='hidden' name='zone_id' value='' />";
                }

            }
        }

    }

}

1 个答案:

答案 0 :(得分:0)

对显示的行号使用不同的变量,而不是用于索引DOM中行的索引。

function count() {
    var table = document.getElementById("zoneItems");
    var tbody = table.tBodies[0];
    var rownum = 1;

    for (var i = 1, row; row = tbody.rows[i]; i++) { //loop through rows
        var deleted = row.cells[1].getElementsByTagName('input')[1].value;
        if (deleted != "1") {
            row.cells[0].innerHTML = "<span>" +rownum+ "</span><input type='hidden' name='zone_id' value='' />";
            rownum++;
        }
    }
}

FIDDLE