/    Sign up×
Community /Pin to ProfileBookmark

Append to and Remove table rows

I found the following code on the W3schools tutorial page.
It inserts and deletes at the top row position of a created table.

[code=php]
<html>
<head>
<script type=”text/javascript”>
// From: http://www.w3schools.com/htmldom/met_table_insertrow.asp

function insRow() {
var x=document.getElementById(‘myTable’).insertRow(0);
var y=x.insertCell(0);
var z=x.insertCell(1);
y.innerHTML=”NEW CELL1″;
z.innerHTML=”NEW CELL2″;
}
function delRow() {
document.getElementById(‘myTable’).deleteRow(0)
}

// function addRow() {} // append row to end of table
// function delAllRows() {} // delete all rows to end of table

</script>
</head>

<body>
<table id=”myTable” border=”1″>
<tr>
<td>Row1 cell1</td>
<td>Row1 cell2</td>
</tr>
<tr>
<td>Row2 cell1</td>
<td>Row2 cell2</td>
</tr>
<tr>
<td>Row3 cell1</td>
<td>Row3 cell2</td>
</tr>
</table>
<br />
<input type=”button” onclick=”insRow()” value=”Insert row”>
<input type=”button” onclick=”delRow()” value=”Delete row”>

<!– want to add the following
<input type=”button” onclick=”addRow()” value=”Append row”>
<input type=”button” onclick=”delAllRows()” value=”Remove all rows”>
–>

</body>
</html>
[/code]

I have two questions concerning this code: ?

  • 1.

    How do you determine the last row position in the table?
    There is something called a ‘rowIndex’ but how is it used to determine the last row (first row appears to be referenced as ‘0’)

  • 2.

    I’m assuming that if I can determine the last row position, I would be able to delete all rows of a table, but stop when the ‘rowIndex’ reaches a minimum of ‘0’ or ‘-1’ (don’t know which it might be). I don’t want to induce an error by deleting more rows than are currently displayed in the table.

  • Has anyone thought about this and determined a solution?
    All help is much appreciated.

    to post a comment
    JavaScript

    3 Comments(s)

    Copy linkTweet thisAlerts:
    @patrx-7Aug 06.2008 — 
  • 1. How do you determine the last row position in the table?
    [CODE]// number of rows in the table:
    var numRows = document.getElementById('myTable').rows.length;

    // index of last row:
    var idxLastRow = document.getElementById('myTable').rows.length - 1;[/CODE]


  • 2. I'm assuming that if I can determine the last row position, I would be able to delete all rows of a table...
    [CODE]// delete all rows in the table (continuously delete first row until all rows gone):
    var objTable = document.getElementById('myTable');
    while (objTable.length > 0)
    objTable.deleteRow(0);[/CODE]


  • The syntax might not be perfect (I just wrote this up without looking/testing), but the ideas are good.



    Hope this helps.



    Patrick Carlo-Hickman

    http://AllegroConsultants.com/progresservices
    Copy linkTweet thisAlerts:
    @JMRKERauthorAug 06.2008 — Thank you 'patrx-7', that was just the information I needed.

    Your 'off-th-cuff' code was good except for a minor error with the delete all rows.

    I changed the information insertion to allow variable information to be placed

    into the cells of the rows by splitting a string on the ',' characters.

    I might try next to pass array information in the function, but it works OK as is.

    Here are the functions and test HTML code I came up with if any are interested.

    I have not seen any errors yet in either FF, MSIE, or Safari.

    [code=php]
    <html>
    <head>
    <script type="text/javascript">
    // Orig.from: http://www.w3schools.com/htmldom/met_table_insertrow.asp
    // Help from: http://www.webdeveloper.com/forum/showthread.php?p=917975#post917975

    function tblSize(TBL) { // number of rows in the table:
    return document.getElementById(TBL).rows.length;
    }

    function insRow(TBL,RowPosition,RowContents) {
    var numRows = tblSize(TBL);
    if (numRows < RowPosition) { RowPosition = numRows; } // check for valid insertion

    var x=document.getElementById(TBL).insertRow(RowPosition);
    var tmp = RowContents.split(',');
    for (var i=0; i<tmp.length; i++) {
    var y=x.insertCell(i);
    y.innerHTML = tmp[i];
    }
    }

    function delRow(TBL,RowPosition) {
    var numRows = tblSize(TBL);
    if (RowPosition < numRows) { // check for valid deletion
    document.getElementById(TBL).deleteRow(RowPosition);
    }
    }

    function addRow(TBL,RowContents) { // append row to end of table
    var idxLastRow = tblSize(TBL);
    insRow(TBL,idxLastRow,idxLastRow+','+RowContents);
    }

    function delLastRow(TBL) {
    var numRows = tblSize(TBL);
    delRow(TBL,numRows-1);
    }

    function delAllRows(TBL) {
    // delete all rows in the table (continuously delete first row until all rows gone):
    var objTable = document.getElementById(TBL);
    while (objTable.rows.length > 0) { objTable.deleteRow(0); }
    }

    </script>
    </head>

    <body>
    <input type="button" onclick="insRow('myTable',0,'0,1,2')" value="Insert row">
    <input type="button" onclick="delRow('myTable',0)" value="Delete row 0">

    <input type="button" onclick="insRow('myTable',3,'r3c0,r3c1,r3c2')" value="Insert at row 3">
    <input type="button" onclick="delRow('myTable',3)" value="Delete at row 3">

    <input type="button" onclick="addRow('myTable','ARC1,ARC2')" value="Append row">
    <input type="button" onclick="delLastRow('myTable')" value="Delete last row">

    <br>

    <input type="button" onclick="delAllRows('myTable')" value="Remove all
    rows">
    <p>

    <table id="myTable" border="1">
    <tr>
    <td>Row1 cell1</td>
    <td>Row1 cell2</td>
    <td>Row1 cell3</td>
    </tr>
    <tr>
    <td>Row2 cell1</td>
    <td>Row2 cell2</td>

    <td>Row2 cell3</td>
    </tr>
    <tr>
    <td>Row3 cell1</td>
    <td>Row3 cell2</td>
    <td>Row3 cell3</td>
    </tr>
    </table>

    </body>
    </html>
    [/code]
    Copy linkTweet thisAlerts:
    @patrx-7Aug 06.2008 — Not a problem. I knew I'd forget some little thing, so I thought the disclaimer was appropriate. Glad I could help!

    Patrick
    ×

    Success!

    Help @JMRKER spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.29,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...