简体   繁体   中英

jQuery append rows to a table

I have a jQuery DataTable with options for inserting new rows.
I've set a limit for table rows to 8.
If the table already has 8 rows it displays an error message.
The problem is that if i have 7 rows it doesen't add the eighth one.
How to fix this ?

var addImage = function(){
    var row_number = $s('.table-slides tbody tr').length;
    var oTable     = $s('#dataT').DataTable();
    var max_rows   = 8;

    $s('body').on('click', '#slide-add', function(){

        if(row_number < max_rows){
            oTable.row.add(
                [
                '<input type="text" name="nume[]" />',
                '<input type="file" name="image[]" id="image" />',
                '<input type="text" name="link[]" />',
                '<a title="Vizualizeaza slide" href="javascript:void(0);"><i class="fa fa-eye"></i></a>',
                '<a href="javascript:void(0);"><i class="fa fa-times"</i></a>'
                ]
                ).draw();
            row_number++;

        }else{
            alert('Error max limit exceeded');
        }
    });
};

Solved.
There was an emty row in my DataTable that displays a message for empty table.

var addImage = function(){
    var row_number = $s('.table-slides tbody tr').length;
    var oTable     = $s('#dataT').DataTable();
    var max_rows   = 8;
    var empty_row  = $s('.dataTables_empty').length;

    if(empty_row){
        row_number = 0;
    }

    $s('body').on('click', '#slide-add', function(){

        if(row_number === max_rows){
            alert('Error max limit exceeded');
        }else{
            oTable.row.add(
                [
                '<input type="text" name="nume[]" />',
                '<input type="file" name="image[]" id="image" />',
                '<input type="text" name="link[]" />',
                '<a title="Vizualizeaza slide" href="javascript:void(0);"><i class="fa fa-eye"></i></a>',
                '<a href="javascript:void(0);"><i class="fa fa-times"</i></a>'
                ]
                ).draw();
            row_number++;
        }



    });
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM