如何使用JavaScript创建动态表格?

huangapple go评论128阅读模式
英文:

How to make dynamic table using Javascript?

问题

我正在尝试创建一个动态的HTML表格,但我还没有成功。

这是我已经做过的部分:

  1. <table id="myTable">
  2. <tr id="tr">
  3. <th>学生姓名</th>
  4. <th>平均分</th>
  5. </tr>
  6. {% for student in teacherStudents %}
  7. <tr id="tr2">
  8. <td id="td">{{ student.Students_Enrollment_Records.Student_Users }}</td>
  9. </tr>
  10. {% endfor %}
  11. </table>
  12. <button type="button" value="" class="save" onclick="doTheInsert()" title="插入新单元格" id="save">&plus;&nbsp;插入新单元格</button>
  13. <button type="button" value="" class="save" onclick="undoTheInsert()" title="撤销最近的操作" id="unsave">&times;&nbsp;撤销最近的操作</button>
  14. <script>
  15. var count = 0;
  16. function doTheInsert() {
  17. let header = $.find("tr[id='tr']")[0];
  18. header.append("<th data-id='header'><input type='date'></th>");
  19. let rows = $.find("tr[id='tr2']");
  20. for (let i = 0; i < rows.length; i++) {
  21. rows[i].append("<td data-id='row'><input type='text'/></td>");
  22. }
  23. }
  24. </script>

我想要的是,如果老师点击按钮,它将在日期下面添加文本框,根据学生人数添加多少个文本框。

例如,如果我点击两次“插入新单元格”按钮,我想要的结果如下(我将制作静态结果以澄清我想要的内容):

如何使用JavaScript创建动态表格?

但是我得到的结果如下:

如何使用JavaScript创建动态表格?

更新:
当我尝试了Sir dirkgroten的答案后,我得到了以下结果:

日期将出现在“学生姓名”和“平均分”之间:

如何使用JavaScript创建动态表格?

英文:

I am trying to create a dynamic html table and i've not been able to do it yet.

i will give what ive done

如何使用JavaScript创建动态表格?

and this is the code

  1. &lt;table id=&quot;myTable&quot;&gt;
  2. &lt;tr id=&quot;tr&quot;&gt;
  3. &lt;th&gt;Students Name&lt;/th&gt;
  4. &lt;th&gt;Average&lt;/th&gt;
  5. &lt;/tr&gt;
  6. {% for student in teacherStudents %}
  7. &lt;tr id=&quot;tr2&quot;&gt;
  8. &lt;td id=&quot;td&quot;&gt;{{ student.Students_Enrollment_Records.Student_Users }}&lt;/td&gt;
  9. &lt;/tr&gt;
  10. {% endfor %}
  11. &lt;/table&gt;
  12. &lt;button type=&quot;button&quot; value=&quot;&quot; class=&quot;save&quot; onclick=&quot;doTheInsert()&quot; title=&quot;Insert New Cell&quot; id=&quot;save&quot;&gt;&amp;plus;&amp;nbsp;Insert New Cell&lt;/button&gt;
  13. &lt;button type=&quot;button&quot; value=&quot;&quot; class=&quot;save&quot; onclick=&quot;undoTheInsert()&quot; title=&quot;Undo Recent Action&quot; id=&quot;unsave&quot;&gt;&amp;times;&amp;nbsp;Undo Recent Action&lt;/button&gt;
  14. &lt;script&gt;
  15. var count = 0;
  16. function doTheInsert() {
  17. let header = $.find(&quot;tr[id=&#39;tr&#39;]&quot;)[0];
  18. header.append(&quot;&lt;th data-id=&#39;header&#39;&gt;&lt;input type=&#39;date&#39;&gt;&lt;/th&gt;&quot;);
  19. let rows = $.find(&quot;tr[id=&#39;tr2&#39;]&quot;);
  20. for (let i = 0; i &lt; rows.length; i++) {
  21. rows[i].append(&quot;&lt;td data-id=&#39;row&#39;&gt;&lt;input type=&#39;text&#39;/&gt;&lt;/td&gt;&quot;);
  22. }
  23. }
  24. &lt;/script&gt;

what i want is if the teacher click the button it will add textbox under the date, depending on the number of students how many textboxes to add

for example i click twice the button Insert New Cell

this is the result I want, (i will make static result to clarify what i want)

如何使用JavaScript创建动态表格?

what result ive got

如何使用JavaScript创建动态表格?

UPDATE:
when I tried the answer of Sir dirkgroten I got this

the date will appear between the Students Name and Average

如何使用JavaScript创建动态表格?

答案1

得分: 0

Your .append() is applied to a javascript DOM node instead of jQuery element because you index into the jQuery elements.

For example, let header = $(&quot;tr#tr&quot;) gives you all &lt;tr id=&quot;tr&quot;&gt; elements. When you do header[0] this isn't a jQuery element anymore but the JavaScript node. So append() is not the same function you expect:

  • In JavaScript, ParentNode.append(&lt;string&gt;) appends the actual string, as explained here.
  • In jQuery, elem.append(&lt;string&gt;) appends the HTML if the string is an htmlString, as explained here.

Your code should be like this:

  1. function doTheInsert()
  2. {
  3. let header=$(&quot;tr#tr&quot;); // same as $.find(&quot;tr[id=&#39;tr2&#39;]&quot;)
  4. header.append(&quot;&lt;th data-id=&#39;header&#39;&gt;&lt;input type=&#39;date&#39;&gt;&lt;/th&gt;&quot;);
  5. let rows=$(&quot;tr#tr2&quot;);
  6. rows.append(&quot;&lt;td data-id=&#39;row&#39;&gt;&lt;input type=&#39;text&#39;/&gt;&lt;/td&gt;&quot;);
  7. }
英文:

Your .append() is applied to a javascript DOM node instead of jQuery element, because you index into the jQuery elements.

For example, let header = $(&quot;tr#tr&quot;) gives you all &lt;tr id=&quot;tr&quot;&gt; elements. When you do header[0] this isn't a jQuery element anymore but the javascript node. So append() is not the same function you expect:

  • In javascript, ParentNode.append(&lt;string&gt;) appends the actual string, as explained here.
  • In jQuery, elem.append(&lt;string&gt;) appends the HTML, if the string is an htmlString, as explained here.

Your code should be like this:

  1. function doTheInsert()
  2. {
  3. let header=$(&quot;tr#tr&quot;); // same as $.find(&quot;tr[id=&#39;tr2&#39;]&quot;)
  4. header.append(&quot;&lt;th data-id=&#39;header&#39;&gt;&lt;input type=&#39;date&#39;&gt;&lt;/th&gt;&quot;);
  5. let rows=$(&quot;tr#tr2&quot;);
  6. rows.append(&quot;&lt;td data-id=&#39;row&#39;&gt;&lt;input type=&#39;text&#39;/&gt;&lt;/td&gt;&quot;);
  7. }

huangapple
  • 本文由 发表于 2020年1月3日 23:44:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定