Add table row in jQuery

前端 未结 30 2103
既然无缘
既然无缘 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:17

    What if you had a <tbody> and a <tfoot>?

    Such as:

    <table>
        <tbody>
            <tr><td>Foo</td></tr>
        </tbody>
        <tfoot>
            <tr><td>footer information</td></tr>
        </tfoot>
    </table>
    

    Then it would insert your new row in the footer - not to the body.

    Hence the best solution is to include a <tbody> tag and use .append, rather than .after.

    $("#myTable > tbody").append("<tr><td>row content</td></tr>");
    
    0 讨论(0)
  • 2020-11-21 06:17

    Neil's answer is by far the best one. However things get messy really fast. My suggestion would be to use variables to store elements and append them to the DOM hierarchy.

    HTML

    <table id="tableID">
        <tbody>
        </tbody>
    </table>
    

    JAVASCRIPT

    // Reference to the table body
    var body = $("#tableID").find('tbody');
    
    // Create a new row element
    var row = $('<tr>');
    
    // Create a new column element
    var column = $('<td>');
    
    // Create a new image element
    var image = $('<img>');
    image.attr('src', 'img.png');
    image.text('Image cell');
    
    // Append the image to the column element
    column.append(image);
    // Append the column to the row element
    row.append(column);
    // Append the row to the table body
    body.append(row);
    
    0 讨论(0)
  • 2020-11-21 06:18

    In my opinion the fastest and clear way is

    //Try to get tbody first with jquery children. works faster!
    var tbody = $('#myTable').children('tbody');
    
    //Then if no tbody just select your table 
    var table = tbody.length ? tbody : $('#myTable');
    
    //Add row
    table.append('<tr><td>hello></td></tr>');
    

    here is demo Fiddle

    Also I can recommend a small function to make more html changes

    //Compose template string
    String.prototype.compose = (function (){
    var re = /\{{(.+?)\}}/g;
    return function (o){
            return this.replace(re, function (_, k){
                return typeof o[k] != 'undefined' ? o[k] : '';
            });
        }
    }());
    

    If you use my string composer you can do this like

    var tbody = $('#myTable').children('tbody');
    var table = tbody.length ? tbody : $('#myTable');
    var row = '<tr>'+
        '<td>{{id}}</td>'+
        '<td>{{name}}</td>'+
        '<td>{{phone}}</td>'+
    '</tr>';
    
    
    //Add row
    table.append(row.compose({
        'id': 3,
        'name': 'Lee',
        'phone': '123 456 789'
    }));
    

    Here is demo Fiddle

    0 讨论(0)
  • 2020-11-21 06:19

    My solution:

    //Adds a new table row
    $.fn.addNewRow = function (rowId) {
        $(this).find('tbody').append('<tr id="' + rowId + '"> </tr>');
    };
    

    usage:

    $('#Table').addNewRow(id1);
    
    0 讨论(0)
  • 2020-11-21 06:19

    To add a good example on the topic, here is working solution if you need to add a row at specific position.

    The extra row is added after the 5th row, or at the end of the table if there are less then 5 rows.

    var ninja_row = $('#banner_holder').find('tr');
    
    if( $('#my_table tbody tr').length > 5){
        $('#my_table tbody tr').filter(':nth-child(5)').after(ninja_row);
    }else{
        $('#my_table tr:last').after(ninja_row);
    }
    

    I put the content on a ready (hidden) container below the table ..so if you(or the designer) have to change it is not required to edit the JS.

    <table id="banner_holder" style="display:none;"> 
        <tr>
            <td colspan="3">
                <div class="wide-banner"></div>
            </td>   
        </tr> 
    </table>
    
    0 讨论(0)
  • 2020-11-21 06:19
    <table id=myTable>
        <tr><td></td></tr>
        <style="height=0px;" tfoot></tfoot>
    </table>
    

    You can cache the footer variable and reduce access to DOM (Note: may be it will be better to use a fake row instead of footer).

    var footer = $("#mytable tfoot")
    footer.before("<tr><td></td></tr>")
    
    0 讨论(0)
提交回复
热议问题