【发布时间】:2018-01-18 14:37:10
【问题描述】:
我是 Vue 的新手,整天都在为此苦苦挣扎。我正在尝试使用允许用户添加和删除行的 Vue 组件构建一个表。
我遇到的问题是removeRow() 函数没有删除选定的行,而是删除了表格的最后一行。谁能帮帮我?
Vue.component('newtemplate', {
template: `<div>
<div>
<button class="button btn-xs btn-success" @click="addRow">+</button>
</div>
<table>
<thead>
<tr>
<th class="col-lg-6 nopadding"><input type="text" value="Col 1" class="borderless nopadding col-lg-6"></th>
<th class="col-lg-2 nopadding"><input type="text" value="Col 2" class="borderless nopadding col-lg-12"></th>
<th class="col-lg-2 nopadding"><input type="text" value="Col 3" class="borderless nopadding col-lg-12"></th>
<th class="col-lg-2 nopadding"><input type="text" value="Col 4" class="borderless nopadding col-lg-12"></th>
</tr>
</thead>
<tbody>
<tr v-for="(row, index) in rows" :row="row">
<td>
<input type="text" class="form-control nopadding text-center">
</td>
<td>
<input type="text" class="form-control nopadding text-center">
</td>
<td>
<input type="text" class="form-control nopadding text-center">
</td>
<td>
<input type="text" class="form-control nopadding text-center">
</td>
<td>
<button type="button" class="text-center button btn-xs btn-danger" v-on:click="removeRow(index)">-</button>
</td>
</tr>
</tbody>
</table>
</div>`,
data: function() {
return {
rows: [{row: ""}]
}
},
methods:{
addRow(){
this.rows.push({row: ''});
},
removeRow: function(index) {
this.rows.splice(index, 1);
}
}
});
更新 我做了一个小提琴 https://jsfiddle.net/4d2uzx0r/
【问题讨论】:
标签: vue.js components