【问题标题】:How to set auto increment +1 from last row numeric value如何从最后一行数值设置自动增量+1
【发布时间】:2020-11-10 10:21:23
【问题描述】:

这是我的代码示例。 第一行“end_no = 10” 我需要从“end_no”添加+1到第二行“start_no”。第二行“start_no”应该是11,然后从每行start_no自动添加+1。 如何设置代码?请帮帮我。

export default{
     data(){
      return{
        form:{
        items:[]
      },
    }
  },
  methods:{
    addNewLine(){
      this.form.items.push({
       start_no:1,
       end_no:10,
      })
    },
  }
}
<div>
    <button @click="addNewLine">Add New Line</button>
</div>

<form>
   <table>
       <thead>
           <tr>
             <th>SL.</th>
             <th>Name</th>
             <th>Start No</th>
             <th>End No</th> 
           </tr>
       </thead>
       <tbody>
           <tr v-for="(item , index) in form.items">
             <td>{{index + 1}}</td>
             <td><input type="text" v-model="item.start_no" /></td>
             <td><input type="text" v-model="item.end_no" /></td>
           </tr>
       </tbody>
    </table>
</form>

【问题讨论】:

    标签: laravel-5 vuejs2


    【解决方案1】:

    如果数组中项的顺序是固定的,那么你可以做的是获取数组的最后一项,并获取它的end_no属性。

    addNewLine() {
      const lastItemIndex = this.form.items.length - 1;
      const lastItemEndNo = this.form.items[lastItemIndex].end_no;
      this.form.items.push({
        start_no: lastItemEndNo + 1,
        end_no: ...
      });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-30
      • 1970-01-01
      • 2016-02-02
      • 1970-01-01
      • 1970-01-01
      • 2013-07-11
      • 2013-04-20
      相关资源
      最近更新 更多