Add table row in jQuery

前端 未结 30 2098
既然无缘
既然无缘 2020-11-21 05:40

What is the best method in jQuery to add an additional row to a table as the last row?

Is this acceptable?

$(\'#myTable\').append(\'

        
相关标签:
30条回答
  • 2020-11-21 06:03

    The approach you suggest is not guaranteed to give you the result you're looking for - what if you had a tbody for example:

    <table id="myTable">
      <tbody>
        <tr>...</tr>
        <tr>...</tr>
      </tbody>
    </table>
    

    You would end up with the following:

    <table id="myTable">
      <tbody>
        <tr>...</tr>
        <tr>...</tr>
      </tbody>
      <tr>...</tr>
    </table>
    

    I would therefore recommend this approach instead:

    $('#myTable tr:last').after('<tr>...</tr><tr>...</tr>');
    

    You can include anything within the after() method as long as it's valid HTML, including multiple rows as per the example above.

    Update: Revisiting this answer following recent activity with this question. eyelidlessness makes a good comment that there will always be a tbody in the DOM; this is true, but only if there is at least one row. If you have no rows, there will be no tbody unless you have specified one yourself.

    DaRKoN_ suggests appending to the tbody rather than adding content after the last tr. This gets around the issue of having no rows, but still isn't bulletproof as you could theoretically have multiple tbody elements and the row would get added to each of them.

    Weighing everything up, I'm not sure there is a single one-line solution that accounts for every single possible scenario. You will need to make sure the jQuery code tallies with your markup.

    I think the safest solution is probably to ensure your table always includes at least one tbody in your markup, even if it has no rows. On this basis, you can use the following which will work however many rows you have (and also account for multiple tbody elements):

    $('#myTable > tbody:last-child').append('<tr>...</tr><tr>...</tr>');
    
    0 讨论(0)
  • 2020-11-21 06:04

    I recommend

    $('#myTable > tbody:first').append('<tr>...</tr><tr>...</tr>'); 
    

    as opposed to

    $('#myTable > tbody:last').append('<tr>...</tr><tr>...</tr>'); 
    

    The first and last keywords work on the first or last tag to be started, not closed. Therefore, this plays nicer with nested tables, if you don't want the nested table to be changed, but instead add to the overall table. At least, this is what I found.

    <table id=myTable>
      <tbody id=first>
        <tr><td>
          <table id=myNestedTable>
            <tbody id=last>
            </tbody>
          </table>
        </td></tr>
      </tbody>
    </table>
    
    0 讨论(0)
  • 2020-11-21 06:05

    So things have changed ever since @Luke Bennett answered this question. Here is an update.

    jQuery since version 1.4(?) automatically detects if the element you are trying to insert (using any of the append(), prepend(), before(), or after() methods) is a <tr> and inserts it into the first <tbody> in your table or wraps it into a new <tbody> if one doesn't exist.

    So yes your example code is acceptable and will work fine with jQuery 1.4+. ;)

    $('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
    
    0 讨论(0)
  • 2020-11-21 06:06
        // Create a row and append to table
        var row = $('<tr />', {})
            .appendTo("#table_id");
    
        // Add columns to the row. <td> properties can be given in the JSON
        $('<td />', {
            'text': 'column1'
        }).appendTo(row);
    
        $('<td />', {
            'text': 'column2',
            'style': 'min-width:100px;'
        }).appendTo(row);
    
    0 讨论(0)
  • 2020-11-21 06:07

    This can be done easily using the "last()" function of jQuery.

    $("#tableId").last().append("<tr><td>New row</td></tr>");
    
    0 讨论(0)
  • 2020-11-21 06:09

    I'm using this way when there is not any row in the table, as well as, each row is quite complicated.

    style.css:

    ...
    #templateRow {
      display:none;
    }
    ...
    

    xxx.html

    ...
    <tr id="templateRow"> ... </tr>
    ...
    
    $("#templateRow").clone().removeAttr("id").appendTo( $("#templateRow").parent() );
    
    ...
    
    0 讨论(0)
提交回复
热议问题