12

I would like to create table inside a div element

On my .html file, I have this

<div id='div1'> </div>

On my js file, I want to place new table with rows and with data

How can I implement this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
kratz
  • 576
  • 4
  • 9
  • 19

5 Answers5

33

Assuming you have the HTML for the table, you can simply create a jQuery object out of it and append it to the DIV. If you have the data, you'll need to iterate through it and create the cells/rows from the data 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');
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
3

There are many different ways you could go with this. One way is to do something 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 your div, parsed as HTML. Another way, if you want to add each row separately, would 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 put the HTML or text you enter inside of whatever element you selected using the dollar-sign selector ($("selector text"))

John Nicely
  • 1,006
  • 8
  • 18
1

FOR EXAMPLE YOU HAVE RECIEVED JASON DATA 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;
}
GGSoft
  • 439
  • 6
  • 15
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 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);
        }
    }       
    });
vishwakarma09
  • 254
  • 3
  • 17
-1
var newContent = $('<your html="here"/>');
var insertLocation = $('#div1');
insertLocation.append(newContent);
John Fisher
  • 22,355
  • 2
  • 39
  • 64