使用 Vue.JS 编辑数组对象
Edit object of an array using Vue.JS
我正在使用 Vuejs + Laravel 开发我的第一个应用程序,我遇到了一个直到现在才解决的问题!
我有一个对象数组,我需要在不删除的情况下编辑一个 then 并添加一个新对象!我做了一个JS Bin来展示我需要的!
当您单击“编辑”并开始输入新值时,原始值也会进行编辑,但我只需要在用户点击保存按钮后更改原始值!
有人可以帮助我吗?
PS:我将更新我的数据库,然后在 table!
上显示新值
有没有像我在没有同步的编辑功能上所做的那样复制我的记录?
JS
new Vue({
el: 'body',
data: {
cache: {},
record: {},
list: [
{ name: 'Google', id: 1 },
{ name: 'Facebook', id: 2 },
],
},
methods: {
doEdit: function (record) {
this.cache = record;
},
edit: function (record) {
this.record = _.cloneDeep(record);
this.cache = record;
}
}
});
HTML
<div class="container">
<form class="form-horizontal" @submit.prevent="doEdit(record)">
<div class="row">
<div class="col-md-12">
<label>Name</label>
<input type="text" class="form-control" v-el:record-name v-model="record.name">
</div>
<div class="col-xs-12" style="margin-top:15px">
<button type="submit" class="col-xs-12 btn btn-success">Save</button>
</div>
</div>
</form>
<hr>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr v-for="r in list">
<td class="text-center" style="width:90px"> {{ r.id }} </td>
<td> {{ r.name }} </td>
<td class="text-center" style="width:90px">
<span class="btn btn-warning btn-xs" @click="edit(r)"><i class="fa-fw fa fa-pencil"></i></span>
</td>
</tr>
</tbody>
</table>
</div>
我正在使用 Vuejs + Laravel 开发我的第一个应用程序,我遇到了一个直到现在才解决的问题!
我有一个对象数组,我需要在不删除的情况下编辑一个 then 并添加一个新对象!我做了一个JS Bin来展示我需要的!
当您单击“编辑”并开始输入新值时,原始值也会进行编辑,但我只需要在用户点击保存按钮后更改原始值!
有人可以帮助我吗?
PS:我将更新我的数据库,然后在 table!
上显示新值有没有像我在没有同步的编辑功能上所做的那样复制我的记录?
JS
new Vue({
el: 'body',
data: {
cache: {},
record: {},
list: [
{ name: 'Google', id: 1 },
{ name: 'Facebook', id: 2 },
],
},
methods: {
doEdit: function (record) {
this.cache = record;
},
edit: function (record) {
this.record = _.cloneDeep(record);
this.cache = record;
}
}
});
HTML
<div class="container">
<form class="form-horizontal" @submit.prevent="doEdit(record)">
<div class="row">
<div class="col-md-12">
<label>Name</label>
<input type="text" class="form-control" v-el:record-name v-model="record.name">
</div>
<div class="col-xs-12" style="margin-top:15px">
<button type="submit" class="col-xs-12 btn btn-success">Save</button>
</div>
</div>
</form>
<hr>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr v-for="r in list">
<td class="text-center" style="width:90px"> {{ r.id }} </td>
<td> {{ r.name }} </td>
<td class="text-center" style="width:90px">
<span class="btn btn-warning btn-xs" @click="edit(r)"><i class="fa-fw fa fa-pencil"></i></span>
</td>
</tr>
</tbody>
</table>
</div>