JS @ DOM: dynamically add row display information to the table to achieve scrolling. js @ dom table

Source: Internet
Author: User

JS @ DOM: dynamically add row display information to the table to achieve scrolling. js @ dom table

Recently, I learned about D3 and used tables most of the time, and dynamically added and deleted tables in js. Here I will summarize the operations on tables in js:

First, you must know that js does not provide column-related operations. Therefore, column-related operations can only be implemented through row operations, such as obtaining the number of columns, you can only obtain the number of columns by getting the number of cells in the row. (this is also a defect. If a cell is set with the colspan attribute, an error occurs ).

Table. rows. length // obtain the number of rows in the table. rows [0]. cells. length // obtain the number of cells in the first row of the table. insertRow (I) // insert row (I + 1). The number of rows (parameter) starts from scratch tr. insertCell (I) // Insert the cell (I + 1). The parameter starts from scratch.

There are also some uncommon methods. See the tutorial in w3school: html dom Table object;

Thanks to the simple example provided by the showbo Moderator: display information to the row in the table, with a maximum of 10 rows displayed. If there are more than 10 rows, delete the top row (Scroll display information ):

<Input type = "button" onclick = "addRow () "value =" Add new rows "/> <br/> <table id =" tb "border =" 1 "> </table> <script> var total = 0; // for testing, the reference function addRow () {var tb = document is deleted by the actual application. getElementById ('tb'), l = tb. rows. length; var tr = tb. insertRow (l); var td = tr. insertCell (0); td. innerHTML = 'xxxxxx' + total; // displayed content. If multiple cells exist, continue tr. insertCell (1), note that the parameter is from 0 if (l> = 10) {// more than 10 rows, delete the first row tr = tb. rows [0]; tr. parentNode. removeChild (tr);} total ++;} </script>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.