Generate HTML Table Client Side

Generate HTML Table Client Side

甜柠檬 发布于 2021-11-24 字数 1394 浏览 854 回复 3 原文

(ASP.NET Web Application) I'd like to create a page which allows the user to build an HTML table. The user will be provided with the following controls: a textbox used to define the amount of rows in the table, a textbox used to define the amount of columns in the table, and a bulleted list of shapes (ex. Square, Octagon, etc) which will be displayed within each cell.

As the user enters a value within each control, I'd like to build and display the table based upon the existing values. I'd like to handle the generating of the HTML table's code on the client-side.

What are my options? I've come across a number of articles that I believe could be useful, but I'm unable to piece everything together and gather an applicable approach; I have little experience with client-side scripting, however I will not shy away from a learning curve (what programmer would?) if it will result in a clean, efficient solution.

Any informative links you can provide would be greatly appreciated.

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。



需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。


怼怹恏 2022-06-07 3 楼

You can program against the HTML DOM to build the table if you really want to do it on the client side. Creating a table object and adding rows and columns shouldn't be that big of a deal. You would use the values of the text boxes as control variables in your for loops. See the W3Schools tutorial for the DOM objects to use. Adding the shapes client-side should be as simple as adding image objects (or img tags) inside the td elements.

I haven't done it myself, but based on what you're describing it sounds like something that could be done in JavaScript on the client side without a huge amount of effort. That said, I would definitely consider doing it on the server unless you really need it on the client.

BTW, I don't know much about jQuery. There may be a simpler way to do it with that library. But the pure JS way shouldn't be too hairy.

纵山崖 2022-06-07 2 楼

I looks like you want to do something really specific, so you will have to do a custom build. I'd say look into JQuery (, it's one of the best way to write custom javascript without having to spend hours re-inventing the wheel. There are a lot of good tutorials on Google.

edit: There are simple way to add elements (tables rows for your case) and set their properties. Also you can use AJAX calls to save all that if this is needed.

Hope that helps

短暂陪伴 2022-06-07 1 楼

JQuery with a couple of nested loops should do this fairly easily. You can optimize it with a string builder or something, but the basics are pretty clear. If you're doing something more complicated, you'd probably be better off looking into one of the templating engines for JQuery or MS AJAX:

 <script type="text/javascript">
     $(function() {
       $('INPUT, SELECT').change(function() {
          var rows = parseInt($('#rows').get(0).value);
          var cols = parseInt($('#cols').get(0).value);
          if (isNaN(rows) || isNaN(cols)) {
          var shape = $('#shape').get(0).value;
          drawTable(rows, cols, shape);

     function drawTable(rows, cols, shape) {
        var table = $('#results > TABLE');

        for (var row = 0; row < rows; row++) {
           var htmlRow;
           htmlRow = '<tr>';
           for (var col = 0; col < cols; col++) {
              htmlRow += '<td><img src="' + shape + '.gif" alt="' + shape + '" /></td>';
           htmlRow += "</tr>";

 Columns: <input id="cols" type="text" /> <br />
 Rows: <input id="rows" type="text" /> <br />
     <select id="shape">
        <option value="square">Square</option>
        <option value="circle">Circle</option>

 <div id="results">