[ACCEPTED]-How to create a new table with rows using jquery and wrap it inside div-html-table

Accepted answer
Score: 33

Assuming you have the HTML for the table, you 5 can simply create a jQuery object out of 4 it and append it to the DIV. If you have 3 the data, you'll need to iterate through 2 it and create the cells/rows from the data 1 and add them independently.

$('<table><tr><td>.....</td></tr></table>').appendTo( '#div1' );

or

var data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [7, 8, 9 ] ];

var html = '<table><thead><tr>...</tr></thead><tbody>';
for (var i = 0, len = data.length; i < len; ++i) {
    html += '<tr>';
    for (var j = 0, rowLen = data[i].length; j < rowLen; ++j ) {
        html += '<td>' + data[i][j] + '</td>';
    }
    html += "</tr>";
}
html += '</tbody><tfoot><tr>....</tr></tfoot></table>';

$(html).appendTo('#div1');
Score: 3

There are many different ways you could 8 go with this. One way is to do something 7 like this:

// $(document).ready() makes sure that nothing happens until the page
// is fully loaded. It's important because the div may not have loaded
// yet if you put code outside of this
$(document).ready( function() {
    $("#div1").append(
        "<table><tr><td>My column 1, row 1</td>" +
        "<td>My column 2, row 2</td></tr>" +
        "<tr><td>My column 1, row 2</td>" +
        "<td>My column 2, row 2</td></tr></table>");
});

This will put the full table into 6 your div, parsed as HTML. Another way, if 5 you want to add each row separately, would 4 be:

$(document).ready(function() {
    $("#div1").append("<table id=\"my_table1\"></table>");
    $("#my_table1").append("<tr><td>Row 1</td></tr>");
    ... insert more rows here ...
    $("#my_table1").append("<tr><td>Row ...</td></tr>");
});

It's important to understand that .append() will 3 put the HTML or text you enter inside of whatever 2 element you selected using the dollar-sign 1 selector ($("selector text"))

Score: 1

FOR EXAMPLE YOU HAVE RECIEVED JASON DATA 1 FROM SERVER.

        var obj = JSON.parse(msg);
        var tableString ="<table id='tbla'>";
        tableString +="<tr><th>Name<th>City<th>Birthday</tr>";


        for (var i=0; i<obj.length; i++){
        tableString +=gg_stringformat("<tr><td>{0}<td>{1}<td>{2}</tr>",obj[i].name, obj[i].city, obj[i].birthday);
        }
        tableString +="</table>";
        $('#divb').html(tableString);

HERE IS THE CODE FOR gg_stringformat

function gg_stringformat() {
var argcount = arguments.length,
    string,
    i;

if (!argcount) {
    return "";
}
if (argcount === 1) {
    return arguments[0];
}
string = arguments[0];
for (i = 1; i < argcount; i++) {
    string = string.replace(new RegExp('\\{' + (i - 1) + '}', 'gi'), arguments[i]);
}
return string;
}
Score: 0

HTML having this structure:

    <html>
    <head>
    <body>
    <div id="container">
    <div id="row0" class="row">
    <div id="col0" class="column">
    <div id="col1" class="column">
    <div id="col2" class="column">
    </div>
    <div id="row1" class="row">
    <div id="col0" class="column"></div>
    <div id="col1" class="column"></div>
    <div id="col2" class="column"></div>
    </div>
    <div id="row2" class="row">
    <div id="col0" class="column"></div>
    <div id="col1" class="column"></div>
    <div id="col2" class="column"></div>
    </div>
    </div>
    </body>
    </html>

This is the jquery 1 code

    $(document).ready(function(){
    var row,col,rowid,colid;

    for(i=0;i<=2;i++){
        row='<div id=\"row'+i+'\" class=\"row\"></div>';
        $("#container").append(row);
        for(j=0;j<=2;j++){
            col='<div id=\"col'+j+'\" class=\"column\"></div>';
            $("#row"+i).append(col);
            $("#col"+j).append(flipper);
        }
    }       
    });

More Related questions