客户端生成 HTML 表

发布于 2024-07-17 09:06:25 字数 707 浏览 14 评论 0原文

(ASP.NET Web 应用程序)我想创建一个允许用户构建 HTML 表的页面。 将向用户提供以下控件:用于定义表中行数的文本框、用于定义表中列数的文本框以及形状的项目符号列表(例如正方形、八边形等) )将显示在每个单元格内。

当用户在每个控件中输入值时,我想根据现有值构建并显示表格。 我想在客户端处理 HTML 表代码的生成。

我有什么选择? 我遇到了 数量 文章我相信这可能有用,但我无法将所有内容组合在一起并收集适用的方法; 我对客户端脚本编写经验很少,但是如果它能带来干净、高效的解决方案,我不会回避学习曲线(程序员会怎样?)。

您可以提供任何信息链接,我们将不胜感激。

(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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

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

评论(3

人事已非 2024-07-24 09:06:25

具有几个嵌套循环的 JQuery 应该相当容易地做到这一点。 您可以使用字符串生成器或其他东西来优化它,但基础知识非常清楚。 如果您正在做更复杂的事情,您可能最好研究 JQuery 或 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)) {
              return;
          }
          var shape = $('#shape').get(0).value;
          drawTable(rows, cols, shape);
       });
     });

     function drawTable(rows, cols, shape) {
        $('#results').empty().append("<table></table>");
        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>";
           table.append(htmlRow);
        }
     }         
 </script>

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

 <div id="results">
 </div>

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)) {
              return;
          }
          var shape = $('#shape').get(0).value;
          drawTable(rows, cols, shape);
       });
     });

     function drawTable(rows, cols, shape) {
        $('#results').empty().append("<table></table>");
        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>";
           table.append(htmlRow);
        }
     }         
 </script>

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

 <div id="results">
 </div>
勿忘初心 2024-07-24 09:06:25

我看起来你想做一些非常具体的事情,所以你必须进行自定义构建。 我想说看看 JQuery (http://jquery.com/),这是最好的方法之一编写自定义 JavaScript,而无需花费数小时重新发明轮子。 谷歌上有很多很好的教程。

编辑:有一些简单的方法可以添加元素(适合您的案例的表行)并设置其属性。 如果需要的话,您还可以使用 AJAX 调用来保存所有内容。

希望有帮助

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 (http://jquery.com/), 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

潦草背影 2024-07-24 09:06:25

如果您确实想在客户端执行此操作,则可以针对 HTML DOM 进行编程来构建表。 创建表对象并添加行和列应该不是什么大问题。 您可以使用文本框的值作为 for 循环中的控制变量。 有关要使用的 DOM 对象,请参阅W3Schools 教程。 在客户端添加形状应该像在 td 元素内添加图像对象(或 img 标签)一样简单。

我自己没有做过,但根据你的描述,这听起来像是可以在客户端用 JavaScript 完成的事情,而不需要付出巨大的努力。 也就是说,我肯定会考虑在服务器上执行此操作,除非您确实需要在客户端上执行它。

编辑:
顺便说一句,我对 jQuery 不太了解。 使用该库可能有一种更简单的方法。 但纯JS方式应该不会太毛茸茸的。

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.

Edit:
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文