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

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

How to make dynamic table using Javascript?

问题

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

这是我已经做过的部分:

<table id="myTable">
    <tr id="tr">
        <th>学生姓名</th>
        <th>平均分</th>
    </tr>
    {% for student in teacherStudents %}
    <tr id="tr2">
        <td id="td">{{ student.Students_Enrollment_Records.Student_Users }}</td>
    </tr>
    {% endfor %}
</table>
<button type="button" value="" class="save" onclick="doTheInsert()" title="插入新单元格" id="save">&plus;&nbsp;插入新单元格</button>

<button type="button" value="" class="save" onclick="undoTheInsert()" title="撤销最近的操作" id="unsave">&times;&nbsp;撤销最近的操作</button>

<script>
    var count = 0;

    function doTheInsert() {
        let header = $.find("tr[id='tr']")[0];
        header.append("<th data-id='header'><input type='date'></th>");
        let rows = $.find("tr[id='tr2']");
        for (let i = 0; i < rows.length; i++) {
            rows[i].append("<td data-id='row'><input type='text'/></td>");
        }
    }
</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

&lt;table id=&quot;myTable&quot;&gt;
    &lt;tr id=&quot;tr&quot;&gt;
        &lt;th&gt;Students Name&lt;/th&gt;
        &lt;th&gt;Average&lt;/th&gt;
    &lt;/tr&gt;
    {% for student in teacherStudents %}
    &lt;tr id=&quot;tr2&quot;&gt;
        &lt;td id=&quot;td&quot;&gt;{{ student.Students_Enrollment_Records.Student_Users }}&lt;/td&gt;
    &lt;/tr&gt;
    {% endfor %}
&lt;/table&gt;
&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;

&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;

&lt;script&gt;
    var count = 0;

    function doTheInsert() {
        let header = $.find(&quot;tr[id=&#39;tr&#39;]&quot;)[0];
        header.append(&quot;&lt;th data-id=&#39;header&#39;&gt;&lt;input type=&#39;date&#39;&gt;&lt;/th&gt;&quot;);
        let rows = $.find(&quot;tr[id=&#39;tr2&#39;]&quot;);
        for (let i = 0; i &lt; rows.length; i++) {
            rows[i].append(&quot;&lt;td data-id=&#39;row&#39;&gt;&lt;input type=&#39;text&#39;/&gt;&lt;/td&gt;&quot;);
        }
    }
&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:

function doTheInsert()
{
    let header=$(&quot;tr#tr&quot;);  // same as $.find(&quot;tr[id=&#39;tr2&#39;]&quot;)
    header.append(&quot;&lt;th data-id=&#39;header&#39;&gt;&lt;input type=&#39;date&#39;&gt;&lt;/th&gt;&quot;);

    let rows=$(&quot;tr#tr2&quot;);
    rows.append(&quot;&lt;td data-id=&#39;row&#39;&gt;&lt;input type=&#39;text&#39;/&gt;&lt;/td&gt;&quot;);
}
英文:

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:

function doTheInsert()
    {
        let header=$(&quot;tr#tr&quot;);  // same as $.find(&quot;tr[id=&#39;tr2&#39;]&quot;)
        header.append(&quot;&lt;th data-id=&#39;header&#39;&gt;&lt;input type=&#39;date&#39;&gt;&lt;/th&gt;&quot;);

        let rows=$(&quot;tr#tr2&quot;);
        rows.append(&quot;&lt;td data-id=&#39;row&#39;&gt;&lt;input type=&#39;text&#39;/&gt;&lt;/td&gt;&quot;);
    }

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:

确定