【发布时间】:2018-10-07 22:24:02
【问题描述】:
我希望有一个按钮,单击该按钮会创建一个新表。有可能这样做吗?我在网上搜索过类似的东西,但我找不到任何东西。所有示例都处理单个表并从中添加/删除行。 我遇到的问题是使用附加的方法从 Vue 渲染 html 表格标签。那是我到达路障的地方。另外,我不太确定如何根据表格添加行。我想要完成的事情有可能吗?
更新
我可以插入多个组件,但前提是我将它们硬编码到 html 中。
html
<div class="temp">
<temp></temp>
<temp></temp>
<temp></temp>
</div>
关于 Vue,我创建了一个模板组件,一切正常。我的目标是在单击按钮时将<temp></temp> 插入到html页面中,以便可以创建另一个表。
Vue
Vue.component('temp', {
template: '\
<div>\
<table v-for="(name,i) in rows.slice(0,1)" :key="i">\
<tr>
<input class="form-control" type="text" v-model="name.exercise" placeholder="Exercise Name"/>
</tr>
<tr>
<th></th>
<th>Set</th>
<th>Reps</th>
<th>Rest</th>
<th>Comments</th>
</tr>
<tr v-for= "(row,k) in rows" :key="k">
<td scope="row">
<i class="fa fa-trash" v-on:click="delRow(k,row)"></i>
</td >
<td>
<input class="form-control" type="number"v-model="row.set_num" readonly="readonly"/>
</td>
<td>\
<input class="form-control" type="text" v-model="row.num_of_reps" placeholder="10"/>\
</td>\
<td>\
<input class="form-control" type="text" v-model="row.rest_time"placeholder="1:30"/>\
</td>\
<td>\
<input class="form-control" type="text" v-model="row.comments"/>\
</td>\
</tr>\
</table>\
<button type="button" class="btn btn-info" v-on:click="addRow">\
<i class="fa fa-plus"></i>\
Add Row\
</button>\
</div>',
data() {
return {
set_number: 2,
rows: [{
exercise:"exercise",
set_num: 1,
num_of_reps: "",
rest_time: "",
comments: ""
}]
}
},
methods: {
addRow: function () {
this.rows.push({
exercise:"exercise",
set_num: this.set_number++,
num_of_reps: "",
rest_time: "",
comments: ""
});
},
delRow: function (k, row) {
let row_index = this.rows.indexOf(row);
if (row_index > 0) {
this.rows.splice(row_index, 1);
}
this.set_number = 1;
for (let i in this.rows) {
this.rows[i].set_num = this.set_number;
this.set_number++;
};
}
}
});
let temp = new Vue({
el: ".temp",
});
【问题讨论】:
-
请做一些尝试过的工作并从您的数据中获取信息
-
我放弃了使用 v-html 的想法,转而创建了一个模板组件。 html 页面上的模板标签可以正常工作,因为每个表格都在按应有的方式工作。但是,我不确定如何在页面上动态创建更多它们,例如,如果单击了按钮。
标签: vue.js