V.B
V.B

Reputation: 1211

Creating a dynamic table with Jquery

I am creating a dynamic table with Jquery, I have dynamic number of rows and columns of the table. I want to print some text in the FIRST column of all the rows but some other control in rest of the columns. How should I find if its the first column if the row?

EDIT
Actually I am trying to write my text at the same time I am creating the table. I guess I should first create my table with blank rows and columns and then put in the text?

Upvotes: 0

Views: 347

Answers (2)

TGH
TGH

Reputation: 39248

Try this

$("#someTable tr").each(function(){
   $(this).find("td:first").text("some value");
});

One note though: I typically discourage people from creating dynamic html structures in JavaScript/JQuery. I find it to be much cleaner to do ut using some template based databinding solution like KnockoutJS or similar:http://knockoutjs.com/

Upvotes: 2

Lal krishnan S L
Lal krishnan S L

Reputation: 1745

Consider you have a div and which has id canvs

Your script page

function create_table_element(rows,cols){

    var rand_id = makeid();
    var node1 = document.createElement("div");      
    node1.setAttribute('id', rand_id);
    document.getElementById("canvas").appendChild(node1);
    row=parseInt(rows);
    col=parseInt(cols);
    var tblid=makeid();
    var inner="<table id='"+tblid+"' class='draggable drsElement new_table_insert foreditor' border='1'></table>";
    $(inner).appendTo(node1);
    var table=document.getElementById(tblid);
    var num=row*col;
    for(var i=0;i<row;i++)
    {
    var row1=table.insertRow(0);
        for(var j=0;j<col;j++)
        {
        var cell1=row1.insertCell(0);
        cell1.innerHTML="new column  &nbsp;"+num;
        num--;
        }
    }

} 

And your HTML page

<html>
  <head>
    <script>
      create_table_element(10,5);
    </script>
  </head>
  <body>
    <div id='canvas'>
   </div>
  </body>
</html>

Upvotes: 1

Related Questions