Tuesday, October 09, 2012

Working with table using jQuery | Narendra


To add a new row at the end of the table, use the following code,
  1. $(‘#table_id >  tbody:last’).append(‘
    ….
    ’);  
Here, table_id is an id of your table.
You can also append an html to the table like,
  1. var tr_html = “
    Hello  World
    ”;  
  2.   
  3. $(‘#table_id > tbody:last’).append(tr_html);  
Remove all rows from table,
  1. $('#myTable').find('tr').remove();  
You can also delete a particular row from the table using the id of row.
  1. $(‘#row_id’).remove();  
To get last row of the table,
  1. $(‘#table_id tr:last’);  
Get total rows in table,
Use a selector that will select all the rows and take the length.
  1. var rowCount = $('#table_id tr').length;  
If you use or in your table, you’ll have to use the following syntax or you’ll get a incorrect value:
  1. var rowCount = $('#myTable >tbody >tr').length;  


More than 3 requests, I'll translate this to Chinese.
超过3个请求,我就会把这篇文章翻译成中文。

No comments: