Swaprows

From Federal Burro of Information
Jump to navigationJump to search

<html>
    <head>

    <script>
    Node.prototype.swapNode = function (node) {
        var nextSibling = this.nextSibling;
        var parentNode = this.parentNode;
        node.parentNode.replaceChild(this, node);
        parentNode.insertBefore(node, nextSibling);
    }
    Node.prototype.swapCardinal = function (node) {
        var srcCardinal = this.cells[2].childNodes[0].value;
        var dstCardinal = node.cells[2].childNodes[0].value;
        this.cells[2].childNodes[0].value = dstCardinal ;
        node.cells[2].childNodes[0].value = srcCardinal ;
    }

    function nextRow(obj) {
        var row = obj.parentNode.parentNode;
        var tbody = row.parentNode;
        var nextrow = tbody.rows[row.sectionRowIndex];
        if ( nextrow ) {
            alert ( nextrow.id + " " + nextrow.rowIndex ) ;
        }
    }

    function MoveDown(obj) {
        var row = obj.parentNode.parentNode;
        var tbody = row.parentNode;
        var nextrow = tbody.rows[row.sectionRowIndex+1];
        if ( nextrow ) {
            row.swapNode(nextrow);
            row.swapCardinal(nextrow);
        }
    }
    function MoveUp(obj) {
        var row = obj.parentNode.parentNode;
        var tbody = row.parentNode;
        var previousrow = tbody.rows[row.sectionRowIndex-1];
        if ( previousrow ) {
            row.swapNode(previousrow);
            row.swapCardinal(previousrow);
        }
    }
    </script>
</head>

<body>

<form name="myform" method="post" action="" enctype="application/x-www-form-urlencoded">

<table border="1" name"mydata" id="mytable">
<thead>
    <tr>
        <td>Name</td>
        <td>Max Age</td>
        <td>Importance</td>
    </tr>
</thead>
<tbody>
<tr id="row1">
    <td>David</td>
    <td><input type=text size=4 name="DavidMaxAge"></td>
    <td><input type=text size=4 name="DavidCardinal"></td>
    <td>
        <input type=button onclick="MoveDown(this)" value="Down">
        <input type=button onclick="MoveUp(this)" value="Up">
    </td>
</tr>
<tr id="row2">
    <td>Luc</td>
    <td><input type=text size=4 name="LucMaxAge"></td>
    <td><input type=text size=4 name="LucCardinal"></td>
    <td>
        <input type=button onclick="MoveDown(this)" value="Down">
        <input type=button onclick="MoveUp(this)" value="Up">
    </td>
</tr>
<tr id="row3">
    <td>James</td>
    <td><input type=text size=4 name="JamesMaxAge"></td>
    <td><input type=text size=4 name="JamesCardinal"></td>
    <td>
        <input type=button onclick="MoveDown(this)" value="Down">
        <input type=button onclick="MoveUp(this)" value="Up">
    </td>
</tr>

</table>
</form>
</body>