最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Generate Html table using for loop - Stack Overflow

programmeradmin8浏览0评论

Please help with below mentioned scenario->

I am having wanna display values from 1 to 30 in a table such a way that nos 1,2,3 should e in one tag and like wise 4,5,6 in other tr tag ans so on till 30 value. I wanna use table for displaying values in a element of a table. wherein each value like "1" should display in one , no "2" should display in different <TD> and so on.

I mean to say that value "1" should be displayed in single <TD> of <Table> tag ,value "2" should be displayed in another <td> tag and so on ,Also after three subsequent <TD>s one <Tr> should be used. Output should be as folllows ->

1 2 3 
4 5 6
7 8 9

and so on!

Early response would be much appreciated. Thanks.

I have tried Code as given below,

<script type="text/javascript">

    document.write("        <table width='673' align='center' cellpadding='2' cellspacing='1'>");
    document.write("            <tr>");
    document.write("    <td valign = 'top'>");
    document.write("                </td>");

    document.write("            </tr>");

    var cnt = 0;
    for (var idx = 1; idx <= 30; idx++) 
    {
        cnt = cnt + 1;
        document.write("            <tr>");
            document.write("    <td valign = 'top'>");
            document.write("        <table width='100px' align='center' cellpadding='2' cellspacing='1'>");
            document.write("            <tr>");
            document.write("                <td align='center'>");
            document.write("                   " + idx + "");
            document.write("                </td>");
            document.write("            </tr>");
            document.write("            <tr>");
            document.write("                <td class='label'>");
            document.write("                    <span> name part : " + idx + "</span>");
            document.write("                </td>");
            document.write("            </tr>");
            document.write("            </table>");
            document.write("                </td>");
            if (cnt = 3) 
            {
                document.write("            </tr>");
            }
            if (cnt = 3) {
                cnt = 0;
            }

        }

    document.write("            </table>");
</script>

Please help with below mentioned scenario->

I am having wanna display values from 1 to 30 in a table such a way that nos 1,2,3 should e in one tag and like wise 4,5,6 in other tr tag ans so on till 30 value. I wanna use table for displaying values in a element of a table. wherein each value like "1" should display in one , no "2" should display in different <TD> and so on.

I mean to say that value "1" should be displayed in single <TD> of <Table> tag ,value "2" should be displayed in another <td> tag and so on ,Also after three subsequent <TD>s one <Tr> should be used. Output should be as folllows ->

1 2 3 
4 5 6
7 8 9

and so on!

Early response would be much appreciated. Thanks.

I have tried Code as given below,

<script type="text/javascript">

    document.write("        <table width='673' align='center' cellpadding='2' cellspacing='1'>");
    document.write("            <tr>");
    document.write("    <td valign = 'top'>");
    document.write("                </td>");

    document.write("            </tr>");

    var cnt = 0;
    for (var idx = 1; idx <= 30; idx++) 
    {
        cnt = cnt + 1;
        document.write("            <tr>");
            document.write("    <td valign = 'top'>");
            document.write("        <table width='100px' align='center' cellpadding='2' cellspacing='1'>");
            document.write("            <tr>");
            document.write("                <td align='center'>");
            document.write("                   " + idx + "");
            document.write("                </td>");
            document.write("            </tr>");
            document.write("            <tr>");
            document.write("                <td class='label'>");
            document.write("                    <span> name part : " + idx + "</span>");
            document.write("                </td>");
            document.write("            </tr>");
            document.write("            </table>");
            document.write("                </td>");
            if (cnt = 3) 
            {
                document.write("            </tr>");
            }
            if (cnt = 3) {
                cnt = 0;
            }

        }

    document.write("            </table>");
</script>
Share Improve this question edited Mar 17, 2012 at 17:15 hero123 asked Mar 17, 2012 at 17:07 hero123hero123 511 gold badge1 silver badge6 bronze badges 4
  • Didn't you just post the same question at stackoverflow./questions/9751799/… ? – j08691 Commented Mar 17, 2012 at 17:10
  • Ya @ j08691 but this time ,I have clarified my query along with detailed explanation of the question! Since my early query was closed without being answered , I have posted new Q. – hero123 Commented Mar 17, 2012 at 17:11
  • I am confused. At least format the coding part of your question properly. – Kaf Commented Mar 17, 2012 at 17:15
  • @ Kaf, what else needs to be provided I have used javascript that will be embeded in head section of my Html page,Hope it is now clarified to u? – hero123 Commented Mar 17, 2012 at 17:17
Add a ment  | 

3 Answers 3

Reset to default 10

You can try something like this:

var mytable = "<table cellpadding=\"0\" cellspacing=\"0\"><tbody><tr>";

for (var i = 1; i < 31; i++) {
  if (i % 3 == 1 && i != 1) {
    mytable += "</tr><tr>";
  }
  mytable += "<td>[" + i + "]</td>";
}

mytable += "</tr></tbody></table>";

document.write(mytable);

Here is a jsFiddle demo

This is untested psuedo-code, but may help you get started:

var x, row;
for(x=1;x<10;x=x+row)
{
    document.write('<tr>');
    for(row=0;row<2;row++)
    {
        document.write('<td>' + (x + row));
        // Whatever else you want to output
    }
}

Edit: this answer was given before OP edited his question to add additional info.

Use the modulo operator % to check for 3rd numbers and set the tags accordingly.

http://en.wikibooks/wiki/C_Sharp_Programming/Operators

发布评论

评论列表(0)

  1. 暂无评论