【问题标题】:How to add new line within copying existing line in Vuejs - Laravel project如何在 Vuejs 中复制现有行中添加新行 - Laravel 项目
【发布时间】:2020-07-19 13:03:22
【问题描述】:

这是我的代码。 单击“添加新行”按钮时,添加一个空行即可。 但是,我需要在新表行中添加复制现有行数据。 请帮我。

  

  addMarkerLine(){
        this.form.markeratios.push({
              size:null,
              ratio:0,
         })
      },
    <form action="">
     <table>
     <thead>
     <tr>
        <th>SL.</th>
        <th>Size</th>
        <th>Ratio</th>
        <th>Action</th>
    </tr>`enter code here`
    </thead>
    <tbody>
     <tr v-for="(markeratio , index) in form.markeratios ">
        <td >{{index + 1}}</td>
        <td ><input type="text" v-model="markeratio.size"></td>
        <td ><input type="text" v-model="markeratio.ratio"></td>
        <td><button>Copy This Line</button></td>
    </tr>
    </tbody>
    <tfoot>
   <tr>
      <td><span @click="addMarkerLine">Add New Line</span></td>
   </tr>
   </tfoot>
 </table>
</form>

【问题讨论】:

  • 您能否更详细地解释您想要什么或澄清您的问题。
  • 这里我使用 push 方法来添加新行,这很好。但我需要复制我现有的行并在添加空白行的新行中过去。

标签: vuejs2 laravel-5.7


【解决方案1】:

如果你想将数据复制到下一行,你只需要定义一个函数copyRow,它接受一个object数据,当你的Copy This Line按钮被点击时,将当前对象传递给copyRow功能。在该函数中,只需将数据推送到form.markeratios

<template>
  ...
  <tr v-for="(markeratio , index) in form.markeratios ">
    <td >{{index + 1}}</td>
    <td ><input type="text" v-model="markeratio.size"></td>
    <td ><input type="text" v-model="markeratio.ratio"></td>
    <td><button @click="copyRow(markeratio)">Copy This Line</button></td>
  </tr>
  ...
</template>

<script>
export default {
  ...
  data: {
    return {
      form: {
        markeratios: []
      }
    }
  },
  methods: {
    copyRow(data) {
      this.form.markeratios.push(data);
    }
  }
}
</script>

【讨论】:

  • 感谢上述解决方案,但面临的问题是,当我单击复制按钮时,行正在复制,当更改或插入一些数据时,所有复制的行数据都在发生变化。你能给出一个解决方案吗?
  • 我刚刚为你做了一个小提琴。 jsfiddle.net/9sLdmbz5 。看来我的一切都很好。最好也分享您的代码,以便我们一起调试。
猜你喜欢
  • 1970-01-01
  • 2018-09-15
  • 2020-04-19
  • 2020-01-16
  • 2021-12-12
  • 1970-01-01
  • 2011-06-04
  • 2020-04-26
  • 1970-01-01
相关资源
最近更新 更多