javascript - 如何在vue中单击按钮从数组中获取对象
问题描述
我创建了一个表,并在表中循环遍历一组对象。
<table class="table table-striped" v-if="bins.length > 0">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Location</th>
<th scope="col" class="text-end">Action</th>
</tr>
</thead>
<tbody>
<tr v-for="(bin, index) in bins" :key="index">
<th scope="row">{{index + 1}}</th>
<td ref="txtLocaton" contenteditable="false" v-text="bin.binlocation"></td>
<td class="text-end">
<div class="action-btn">
<button @click="btnEdit"><fa icon="edit" /> Edit</button>
<button><fa icon="trash" /> Delete</button>
</div>
</td>
</tr>
</tbody>
</table>
我想要的是在编辑按钮单击时,我想将 contenteditable 属性从 false 更改为 true。
这是数据()的代码
<script>
export default {
data(){
return{
bins:[
{
binlocation: '11 Garden Block, New City',
},
{
binlocation: 'Ali Towers, Lahore'
},
{
binlocation: 'The Mall Road'
}
]
}
},
methods:{
btnEdit(){
console.log(this.$refs.txtLocaton)
}
}
}
</script>
我正在考虑使用“ref”更改属性,但是当我控制台它时,它会在按钮单击时返回最后一个数组
解决方案
您可以将contenteditable
键存储在bins
数组中(false
最初?):
[{
binlocation: '11 Garden Block, New City',
contenteditable: false,
}, {
binlocation: 'Ali Towers, Lahore',
contenteditable: false,
}, ...]
然后将contenteditable
td
属性绑定到这些值(而不是false
直接传递):
<td ref="txtLocaton" :contenteditable="bin.contenteditable" v-text="bin.binlocation"></td>
当按下“编辑”按钮时,只需根据需要切换值:
<button @click="bin.contenteditable = !bin.contenteditable"><fa icon="edit" /> Edit</button>
或者
<button @click="btnEdit(index)"><fa icon="edit" /> Edit</button>
btnEdit(index) {
this.bins[index] = !this.bins[index];
}
推荐阅读
- sql-server-2012 - 使用从同一表计算的数据更改表添加列
- android - 布局管理器 setFirstVisibleItemPosition
- sql - PL/SQL 程序显示顶级“a”员工的薪水
- asp.net-mvc - JQuery Ajax Post - 操作返回部分视图但未成功或显示
- .net - 在 .NET Core 中添加 Content-Type JSON 标头
- java - Apache Camel data types between components
- python - numpy中的多维累积和
- ios - 如何使用 ARKit 或 Apple Vision 测量 3d 对象的尺寸?
- c++ - 避免只在源文件中定义的类的弱 vtable 警告
- c# - 在类“程序”上调用方法“BuildWebHost”时发生错误。在没有应用程序服务提供者的情况下继续 - ASP.NET CORE 迁移