vue.js - 禁用/启用循环VueJS中每个项目的输入
问题描述
我尝试启用/禁用循环中的每个输入。问题是我的方法在刷新后就起作用了。在我修改代码并保存后,输入就可以了。
<tr v-for="(item, index) in customer_names" :key="item.id">
<td>
<input :disabled="!item.disabled"
v-model="item.name"
type="text"
</td>
</tr>
<div class="edit_mode"
:class="{'display-none':!item.disabled}">
<i class="fa fa-save" @click="disableInput(index)" aria-hidden="true"></i>
</div>
<div class="edit_mode"
:class="{'display-none':item.disabled}">
<i class="fa fa-edit" @click="enableInput(index)" aria-hidden="true"></i>
</div>
props:['customer_names'],
data(){
return{
disabled: [],
}
}
enableInput(index){
console.log('enableInput',this.customer_names[index].disabled);
this.customer_names[index].disabled = false;
},
disableInput(index){
console.log('disabeInput',this.customer_names[index].disabled);
this.customer_names[index].disabled = true;
}
解决方案
我没有完全理解你的问题。我推断您可能想要启用或禁用您从提供的数据创建的文本字段。如果这仍然不是您的意思,请通过粘贴更多源代码来纠正您的问题,并更详细地解释您的问题。
Vue.component("custom", {
template: "#custom-template",
props: ["customer_names"],
methods: {
enableInput(item) {
item.disabled = false;
},
disableInput(item) {
item.disabled = true;
},
toggleInput(item) {
item.disabled = !item.disabled;
}
}
});
new Vue({
data() {
return {
items: [
{ name: "fus", disabled: false },
{ name: "ro", disabled: false },
{ name: "dah", disabled: true }
]
};
}
}).$mount("#app");
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<custom :customer_names="items" />
</div>
<script type="text/x-template" id="custom-template">
<table cellpadding=5>
<tr>
<th>Input</th>
<th>Version 1</th>
<th>Version 2</th>
</tr>
<tr v-for="item in customer_names" :key="item.id">
<td>
<input :disabled="item.disabled" v-model="item.name" type="text" />
</td>
<td>
<button @click="item.disabled = false">E</button>
<button @click="item.disabled = true">D</button>
<button @click="item.disabled = !item.disabled">T</button>
</td>
<td>
<button @click="enableInput(item)">E</button>
<button @click="disableInput(item)">D</button>
<button @click="toggleInput(item)">T</button>
</td>
</tr>
</table>
</script>
推荐阅读
- pseudocode - 编写基本伪代码时遇到问题
- c# - 从 PropertyDeclarationSyntax 获取数据类型
- python - 可以为多个列设置类别和类型吗?
- python - 使用 Python 和 winreg 在 Windows 10 上设置代理
- google-chrome - Chrome 扩展程序:从弹出窗口访问数据
- java - 使用 Hashmap 检测列表中的重复项和重复项计数
- python - 如何使用 Telethon 运行多个会话?
- python - 边界附近有更多点的非均匀间隔的 np.array
- java - 如何检查远程存储库中是否存在依赖项?
- python - 为什么我的第二个数据库列填充了“无”?