Building a table dynamically with JS?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Craig

    #1

    Building a table dynamically with JS?

    Quick question.

    I have HTML tables that get created on the fly. The size of
    rows/columns depends on the report the user is looking at.

    What I want to do is take more control over the look/feel of the
    tables.

    Is this possible?
  • Martin Honnen

    #2
    Re: Building a table dynamically with JS?



    Craig wrote:
    [color=blue]
    > Quick question.
    >
    > I have HTML tables that get created on the fly. The size of
    > rows/columns depends on the report the user is looking at.
    >
    > What I want to do is take more control over the look/feel of the
    > tables.
    >
    > Is this possible?[/color]

    Well, whether the table is static HTML or dynamically created with
    script you can use both the HTML table attributes like border,
    cellspacing, cellpadding as well as CSS to control the look of the table:

    var table = document.create Element('table' );
    table.border = 1;
    table.cellPaddi ng = 10;
    table.cellSpaci ng = 2;
    var tbody = document.create Element('tbody' );
    for (var i = 1; i < 10; i++) {
    var row = document.create Element('tr');
    for (var j = 1; j < 10; j++) {
    var cell = document.create Element('td');
    cell.appendChil d(document.crea teTextNode(i + ', ' + j));
    row.appendChild (cell);
    }
    tbody.appendChi ld(row);
    }
    table.appendChi ld(tbody);
    document.body.a ppendChild(tabl e)
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Craig McDermott

      #3
      Re: Building a table dynamically with JS?

      Thanks for the quick reply.

      I'm a bit of a novice at JS.

      So what is this table actually doing, is this for 10 rows/columns?

      Can you explain more so I understand.

      I don't suppose I can pay you to knock this together as it will probably
      take me weeks to work it out!

      Regards

      Craig



      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Martin Honnen

        #4
        Re: Building a table dynamically with JS?



        Craig McDermott wrote:

        [color=blue]
        > So what is this table actually doing, is this for 10 rows/columns?[/color]

        A loop from i = 1; i < 10 yields 9 rows but of course it doesn't matter,
        it is just an example to fill the table with some data, what is imported
        in the example code is that you can create a table element object with
        var table = document.create Element('table' );
        and then set
        table.border
        table.cellPaddi ng
        table.cellSpaci ng

        --

        Martin Honnen
        http://JavaScript.FAQTs.com/

        Comment

        Working...