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

javascript - Create table by clicking buttons - Stack Overflow

programmeradmin2浏览0评论

Here's what I want to do. Hopefully it's not too hard.

I need to create a table with a div inside each td which is created by clicking buttons... for example


Please select the number of rows in the table

Please select the number of columns in the table..

Result:


So if you clicked on 4 and 4 it would create a table 4 X 4. If you clicked 3 X 1, you would create a table 3 X 1, etc...

Any help would be greatly appreciated!!

Here's a jfiddle of something I'm trying to get working. I'm still looking over all your ments!

/

I know I need to add in the Javascript how to get the element by id.

Here's what I want to do. Hopefully it's not too hard.

I need to create a table with a div inside each td which is created by clicking buttons... for example


Please select the number of rows in the table

Please select the number of columns in the table..

Result:


So if you clicked on 4 and 4 it would create a table 4 X 4. If you clicked 3 X 1, you would create a table 3 X 1, etc...

Any help would be greatly appreciated!!

Here's a jfiddle of something I'm trying to get working. I'm still looking over all your ments!

http://jsfiddle/irocmon/7WD8v/

I know I need to add in the Javascript how to get the element by id.

Share Improve this question edited Jul 16, 2017 at 19:29 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 21, 2011 at 15:26 IrocmonIrocmon 911 silver badge7 bronze badges 3
  • 2 Don't expect anyone to do ALL of your work for you. Which parts of this are you having trouble with? – Diodeus - James MacFarlane Commented Sep 21, 2011 at 15:28
  • Where are you stuck? The basic vector of attack would be to bind event handlers to the buttons and when both options have a value selected, generate the table. Or to add a "Create" button and handle table creation when that's clicked based on selected values. (This would actually make a good interview question for front end engineeers) – Erik Commented Sep 21, 2011 at 15:29
  • I didn't want someone to do all the work for me. I have everything working except for this part. Right now I'm just using html for the table, but i need to be able to have about 30 different table sizes. I've tried adding and deleting columns and switching between multiple tables and i seem to run into a problem every time. This is the best way i can think of to do it. I just wasn't sure where to start. It looks like i have a lot here to help me get started though... Thanks! – Irocmon Commented Sep 21, 2011 at 16:15
Add a ment  | 

4 Answers 4

Reset to default 2

I would use 2 forms, 1 for the top row of numbers and one for the second row of numbers, where each number is a predefined value of the user input.

Assign the submit button to each of the numbers using javascript for each form and from there grab the results with javascript and perform the code/script that is required to plete the task in mind.

I would remend using jquery for this.

Have fun...

you should be able to achieve this with some pretty simple if statements or a switch

if you have 2 variables rows & columns

//loop for number of rows
for "x" number of rows{
 document.write("<tr>");
       if(columns > 0)
       {
        switch statement to output column
        1: document.write("<td></td>");
        2: document.write("<td></td><td></td>");
       }
 document.write("</tr>");
}

the syntax is very very psuedo here, this code wont work but it might get you started, what are you actually wanting to do with the table once you have it?

Using javascript, have 2 local variables: width and height. Within each DIV, have an onclick function that assigns that value to the proper variable, then checks to see if both variables have been assigned (this way they can click on either height or width first). If both are, use these variables within a for loop to generate HTML code within javascript:

var HTML = '<table>';

for(var i = 0; i < height; i++)

{ HTML += '<tr>';

for(var j = 0; j < width; j++)

{ HTML += '<td>...</td>';}

HTML += '</tr>';}

document.getElementById('where_you_want_the_table').innerHTML = HTML;

This is tested and work of note it doesn't handle if they keep trying to build the tables over and over it will keep adding cols and rows but I will let you handle that. :)

    <html>
    <head>
    <script type="text/javascript" src="https://ajax.googleapis./ajax/libs/jquery/1.6.4/jquery.min.js"></script>
    <script type="text/javascript">
        var Rows = 0;
        var ColString = "";
        var TableBuilder;

        $(document).ready(function () {
            $("#Rows input").click(function () { Rows = $(this).val(); });
            $("#Cols input").click(buildCols);
            $("#Submit").click(CreateTable);
        });

        function buildCols() {
            for (i = 0; i < $(this).val(); i++) {
                ColString = ColString + "<td></td>";
            }
            return ColString;
        }
        function CreateTable() {
            if (Rows == 0 || ColString == "") {
                $("#PleaseSelect").removeClass("displayNone");
            }
            else {
                for (i = 0; i < Rows; i++) {
                    TableBuilder = TableBuilder + "<tr>" + ColString + "</tr>";
                }
                $("#table tbody").html(TableBuilder);
            }
        }

    </script>
    <style type="text/css">
      .displayNone { display: none; }
    </style>
    </head>
    <body>

        <table id="table" border="1">
             <tbody>
             </tbody>
         </table>

    <br><br>
    How many Rows?
    <div id="Rows">
        <input type="button" value="1">
        <input type="button" value="2">
        <input type="button" value="3">
        <input type="button" value="4">
    </div>
    <br />
    How Many Columns?
    <div id="Cols">
        <input type="button" value="1" >
        <input type="button" value="2">
        <input type="button" value="3">
        <input type="button" value="4">
    </div>
    <br />
    <div id="PleaseSelect" class="displayNone">Please select both a column number and a row number.</div>
    <input type="button" id="Submit" value="Build Table" />

    </body>
    </html>
发布评论

评论列表(0)

  1. 暂无评论