【问题标题】:Add and delete multiple rows from a table从表中添加和删除多行
【发布时间】:2020-08-06 18:13:23
【问题描述】:

我的vue 应用程序中有一个动态表,可在单击按钮时添加或删除行。表格如下所示。

<table class="stripped centered">
        <thead>
          <tr>
            <th></th>
            <th>No.</th>
            <th>Part No.</th>
            <th>Description</th>
            <th>Quantity</th>
            <th>unit market price</th>
            <th>Markup percentage</th>
            <th><a @click="addRow " class="waves-effect waves-light btn blue darken-3 white-text">Add row</a></th>
          </tr>
        </thead>
        <tbody class="body-rows" v-for="(row, index) in tableRows" :key="index">
          <tr>
            <td><a @click.prevent="deleteRow(index)" class="waves-effect waves-light btn red darken-3 white-text"><i class="material-icons left">delete</i></a></td>
            <td><input class="item-no" type="number" step="0.001" placeholder="Item no"></td>
            <td><input class="part-no" type="text" placeholder="part no"></td>
            <td><input type="text" placeholder="description"></td>
            <td><input class="quantity" type="number" step="0.01" placeholder="quantity"></td>
            <td><input class="ump" type="number" step="0.01" placeholder="UMP"></td>
            <td><input class="markup" type="number" step="0.01" placeholder="markup %"></td>
            <td><a class="add-sub waves-effect waves-light btn teal darken-3 white-text"><i class="material-icons left">add</i>sub</a></td>
          </tr>
          <tr>
            <td><div class="chip">Freight and Insurance</div></td>
            <td><div class="chip">Other costs</div></td>
            <td><div class="chip">Custom Rates</div></td>
            <td><div class="chip">Excise Tax</div></td>
            <td><div class="chip">VAT</div></td>
            <td><div class="chip">Surtax</div></td>
            <td><div class="chip">Withholding Tax</div></td>
          </tr>
          <tr>
            <td><input type="number" placeholder="freight & insurance"></td>
            <td><input type="number" placeholder="other costs"></td>
            <td><input type="number" placeholder="custom rates"></td>
            <td><input type="number" placeholder="excise tax"></td>
            <td><input type="number" placeholder="vat"></td>
            <td><input type="number" placeholder="surtax"></td>
            <td><input type="number" placeholder="withholding"></td>
          </tr>
        </tbody>
      </table>

如您所见,我试图在v-for 循环的一次迭代中添加三行。在我的脚本中,我有:

data(){
    return{
          currentRows: 0,
          tableRows: [],
    }
}
methods:{
    addRow(){
      this.currentRows++
      this.tableRows.push(this.currentRows)
    },
    deleteRow(index){
      this.tableRows.splice(index, 1)
},

添加按钮起作用并添加三行。但是,当我单击该行上的删除按钮时,它会删除最后一行而不是自行删除。任何帮助,将不胜感激。谢谢

【问题讨论】:

  • 为什么不直接使用indexdeleteRow(index) 和内部deleteRowthis.tableRows.splice(index, 1)
  • @Minato 哦,这是我在直接使用index 时尝试过的,但没有奏效。他们有相同的结果。
  • 可能是indexOf 中的比较器返回-1 并且拼接总是从末尾删除row
  • @Minato 我把它改回直接使用index,它仍然从最后删除
  • tableRows的结构是什么

标签: javascript vue.js


【解决方案1】:

你的实现应该是这样的

<table class="stripped centered">
    <thead>
      <tr>
        <th></th>
        <th>No.</th>
        <th>Part No.</th>
        <th>Description</th>
        <th>Quantity</th>
        <th>unit market price</th>
        <th>Markup percentage</th>
        <th><a @click="addRow " class="waves-effect waves-light btn blue darken-3 white-text">Add row</a></th>
      </tr>
    </thead>
    <tbody class="body-rows">
      <tr v-for="(row, index) in tableRows" :key=":key="JSON.stringify(row)"">
          <td>
            <table>
               <tr>
        <td><a @click.prevent="deleteRow(index)" class="waves-effect waves-light btn red darken-3 white-text"><i class="material-icons left">delete</i></a></td>
        <td><input class="item-no" type="number" step="0.001" placeholder="Item no"></td>
        <td><input class="part-no" type="text" placeholder="part no"></td>
        <td><input type="text" placeholder="description"></td>
        <td><input class="quantity" type="number" step="0.01" placeholder="quantity"></td>
        <td><input class="ump" type="number" step="0.01" placeholder="UMP"></td>
        <td><input class="markup" type="number" step="0.01" placeholder="markup %"></td>
        <td><a class="add-sub waves-effect waves-light btn teal darken-3 white-text"><i class="material-icons left">add</i>sub</a></td>
      </tr>
      <tr>
        <td><div class="chip">Freight and Insurance</div></td>
        <td><div class="chip">Other costs</div></td>
        <td><div class="chip">Custom Rates</div></td>
        <td><div class="chip">Excise Tax</div></td>
        <td><div class="chip">VAT</div></td>
        <td><div class="chip">Surtax</div></td>
        <td><div class="chip">Withholding Tax</div></td>
      </tr>
      <tr>
        <td><input type="number" placeholder="freight & insurance"></td>
        <td><input type="number" placeholder="other costs"></td>
        <td><input type="number" placeholder="custom rates"></td>
        <td><input type="number" placeholder="excise tax"></td>
        <td><input type="number" placeholder="vat"></td>
        <td><input type="number" placeholder="surtax"></td>
        <td><input type="number" placeholder="withholding"></td>
      </tr>
            </table>
          </td>          
      </tr>
    </tbody>
    </table>

【讨论】:

  • 好吧,只迭代一行,我想在一次迭代中添加三行。
  • 该表应该有一个 以便您想要操作的部分通过迭代包裹在 中,以便在从 DOM 中删除时用作拼接的焦点
  • 我认为这就是我正在做的事情。用&lt;tbody&gt; 包裹所有三个&lt;tr&gt;s 并迭代它们。你能告诉我你在答案中的意思吗?
  • 这应该可以,但它仍在删除最后一行。我不明白它有什么问题
  • 将 :key="index" 更改为 :key="JSON.stringify(row)" 应该可以修复它。您可以使用实用方法来处理更清洁的体验。 jsfiddle.net/4bpc0m2f/1
【解决方案2】:

你应该修改你的代码如下

在 deleteRow 函数调用中也传递行 ID

   <td><a @click.prevent="deleteRow(row.id,index)" class="waves-effect waves-light btn red 
   darken-3 white-text"><i class="material-icons left">delete</i></a></td>

删除方法如下

   deleteRow(id,index){
      this.tableRows.splice(id, 1);
      this.tableRows.splice(index, 1)
   }

【讨论】:

  • 这只是从末尾删除两行
猜你喜欢
相关资源
最近更新 更多
热门标签