0

Im trying to do something similar to this answer: How to add multiple rows to a jQuery DataTable from a html string

The only difference is: I need to add multiple values that i will get from a text area. The first part works perfectly.

function Gen(){
var data = require('../../cases/config.json');
var tableHeaders;
var cantidad = 0;

$.each(data.Ge[0].Data, function(i, val){
    cantidad += 1
    tableHeaders += "<th>" + val + "</th>";
});
// Header 
$("#tabGen").empty();
$("#tabGen").append('<thead><tr>' + tableHeaders + '</tr></thead>');

var t = $('#tabGen').DataTable({
    "scrollY": 200,
    "scrollX": true,
    rowReorder: true,
    autoFill: true,
    select:true,
    stateSave: true
});

This second part when doing the t.row.add($(info)).draw(); it doesn't appear all the data that i need to show. So when I check out the other answer I saw that I can add a single row from an html string. So I don't know how can I add multiple values inside of the table.

 $('#excel').on( 'click', function () {
    var inf = $('textarea[name=excel_data]').val();
    var rows = inf.split("\n");

    for(var y = 0; y < rows.length; y++) {
    var cells = rows[y].split("\t");

        for(var x in cells) {
            var info = '<td><input type="text" id="inputTextAg'+x+'" name="inputTextAg'+x+'" value="'+cells[x]+'" draggable="true" "></td>'
            t.rows.add($(info)).draw();
        }
    }
 });
}

Thanks!

1 Answer 1

1

Try this:

$('#excel').on( 'click', function () {
    var inf = $('textarea[name=excel_data]').val();
    var rows = inf.split("\n");

    for(var y = 0; y < rows.length; y++) {
        var cells = rows[y].split("\t");

        var info = '<tr>';
        for(var x in cells) {
            info += '<td><input type="text" id="inputTextAg'+x+'" name="inputTextAg'+x+'" value="'+cells[x]+'" draggable="true" "></td>';
        }
        info += '</tr>';
        t.rows.add($(info)).draw();
    }
  });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.