【问题标题】:Add or Remove Row in Input Table Along With Some Available Jinja在输入表中添加或删除行以及一些可用的 Jinja
【发布时间】:2021-01-11 14:32:34
【问题描述】:

我有一个带有一些 jinja(在 value 属性内)的 HTML 表格。

我的template:

<form class="" method="POST">
{% csrf_token %}
  <table class="table table-hover my-5">
    <thead class="">
        <tr>
            <th>No</th>
            <th>Name</th>
            <th>rank</th>
            <th>gmail</th>
            <th>Delete?</th>
        </tr>
    </thead>
    <tbody class="my_dynamic_table">
      {% for i in report_tableA %}
      <tr>
        <td>i</td>
        <td><input type="text" name="name" value="{{ i.name }}"></td>
        <td><input type="text" name="rank" value="{{ i.rank }}"></td>
        <td><input type="email" name="gmail" value="{{ i.gmail }}"></td>
        <td><i class="fa fa-times-circle" style="font-size: 22px; color: red;"></i></td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
  <div class="row mx-5">
    <button class="btn btn-warning">Add row</button>
    <button type="Submit" class="btn btn-primary ml-auto">Submit</button>
  </div>
</form>

现在我的问题是如何实现Add row 按钮和Delete 图标(在最后一个td 内)的功能?这里Add row 函数将在表中添加一个新行,同样具有blank value 属性。

我之前通过以下stackoverflow link练习过。但现在问题是这里也添加了jinja。

如果您能帮我解决这个问题,我将不胜感激。

【问题讨论】:

  • 只是附加行的空值是你想要的还是别的?
  • 是的,这就是我想要的,还需要任何一行的 removeRow 选项。
  • 您在问题中添加的链接已经这样做了..它在附加时不包括value 属性,它还具有删除功能(接受的答案)。
  • 是的,但由于某种原因我不能使用它:现在i 的值应该来自神社.....还有一个问题,就像我需要一些 @ 行987654335@ 属性。但是新的appended 行不应该有value。
  • 这里可以用jquery吗?

标签: javascript python django jinja2


【解决方案1】:

您可以在单击Add row 按钮时首先使用clone tr,然后使用find("input").val("") 清空value 属性并使用find("td:eq(0)").text(length) 将新的i 值添加到第一个td。现在,当单击 remove 时,只需使用 .closest('tr') 这将获得最接近的 tr 并删除该 tr 。此外,您需要在 td 中再次调整 i 值,因此请使用 .each 循环遍历 trs 并进行更改。

演示代码:

$(".add").on("click", function() {
  //get length of tr 
  var length = $(".my_dynamic_table tr").length + 1
  console.log(length)
  //clone first tr
  var cloned = $(".my_dynamic_table tr:first").clone();
  $(cloned).find("input").val(""); //empty values of all cloned inputs
  $(cloned).find("td:eq(0)").text(length); //add `i` value 
  $(cloned).appendTo($(".my_dynamic_table")) //append to tbody
})
//onclick of remove button
$(document).on("click", ".remove", function() {
  $(this).closest("tr").remove(); //remove tr
  //loop through tr
  $(".my_dynamic_table tr").each(function(i) {
    $(this).find("td:eq(0)").text((i + 1)) //change `i` value
  })
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form class="" method="POST">
  <table class="table table-hover my-5">
    <thead class="">
      <tr>
        <th>No</th>
        <th>Name</th>
        <th>rank</th>
        <th>gmail</th>
        <th>Delete?</th>
      </tr>
    </thead>
    <tbody class="my_dynamic_table">
      <tr>
        <td>1</td>
        <td><input type="text" name="name" value="{{ i.name }}"></td>
        <td><input type="text" name="rank" value="{{ i.rank }}"></td>
        <td><input type="email" name="gmail" value="s@gmail.com"></td>
        <td><i class="fa fa-times-circle remove" style="font-size: 22px; color: red;">x</i></td>
      </tr>
    </tbody>
  </table>
  <div class="row mx-5">
    <button class="btn btn-warning add" type="button">Add row</button>
    <button type="Submit" class="btn btn-primary ml-auto">Submit</button>
  </div>
</form>

【讨论】:

  • 非常感谢,现在它运行良好!
猜你喜欢
  • 1970-01-01
  • 2017-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-07
  • 2014-05-05
  • 1970-01-01
  • 2020-11-02
相关资源
最近更新 更多