通过切换复选框 Vue.js 来切换 属性
Toggle property by toggling checkbox Vue.js
如果我解释得不好,请提前道歉,我在这方面有点新手。
如果我有一个包含多个类似于下面的对象的数组,我如何在 Vue 中 unselect/select 模式中的复选框来切换基于 visible
属性 的值在它的名字上?我只想显示 visible 为 true 的项目。
目前,我有一个模式弹出窗口,显示每个对象的名称 属性 以及一个复选框。当我 uncheck/check 一个或多个名称旁边的文本框时,我希望我必须根据 visibility
.
更新列表
我想象的是这样的逻辑; 如果未选中复选框,将可见性设置为 false
下面的代码基本上是我要解释的内容的大纲,我知道它的语法并不完美,它更像是一个视觉指南来尝试展示我的要求。
再次抱歉,如果我解释得不好。
非常感谢您的帮助
<!-- this would be in my modal -->
<div>
<input type="checkbox"
value="usd" />
<label for="usd">USD</label>
</div>
<div>
<input type="checkbox"
value="cad"/>
<label for="cad">CAD</label>
</div>
<!-- End modal -->
<section v-for="loop through MyArray" v-show="myArray.visible">
<div>{{name}}</div>
<div>{{value}}</div>
<div>{{another}}</div>
<div>{{high}}</div>
<div>{{low}}</div>
</section>
myArray[
{
name:"USD",
value: 0.75,
another: 0,
high: 0,
low: 0,
visible:true},
{
name:"CAD",
value: 1.75,
another: 0,
high: 0,
low: 0,
visible:true},
]
我试图模拟一个适合你的情况的解决方案,下面的卡片代表一个模式,当 check/uncheck 复选框时,你的 table 中的值将被更改,你也 hide/show 根据其 visibility
状态的项目,例如:
<div class="flex" v-if="item.visible">...</div>
new Vue({
el: '#app',
data() {
return {
myArray:[
{
name:"USD",
value: 0.75,
another: 0,
high: 0,
low: 0,
visible:true},
{
name:"CAD",
value: 1.75,
another: 0,
high: 0,
low: 0,
visible:true},
],
selectedItem:{visible:false}
}
},
methods:{
setVisible(){
this.myArray= this.myArray.filter((item)=>{
if(item.name===this.selectedItem.name){
item.visible=this.selectedItem.visible;
return item;
}else{ return item}
})
}
}
});
.flex{
display:flex;
justify-content:space-between;
align-items:center;
padding:10px;
}
.mymodal{
padding:50px;
box-shadow:1px 1px 4px #555;
}
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<div id="app" class="container">
<section v-for="item in myArray" >
<div class="flex">
<div>{{item.name}}</div>
<div>{{item.value}}</div>
<div>{{item.another}}</div>
<div>{{item.high}}</div>
<div>{{item.low}}</div>
<div>{{item.visible}}</div>
<div><button class="btn btn-primary" @click="selectedItem=item"> Show details</button></div>
</div>
</section>
<div class="mymodal" v-if="selectedItem.visible">
<input type="checkbox"
v-model="selectedItem.visible" @change="setVisible">
<label for="cad">{{selectedItem.name}}</label>
</div>
</div>
如果我解释得不好,请提前道歉,我在这方面有点新手。
如果我有一个包含多个类似于下面的对象的数组,我如何在 Vue 中 unselect/select 模式中的复选框来切换基于 visible
属性 的值在它的名字上?我只想显示 visible 为 true 的项目。
目前,我有一个模式弹出窗口,显示每个对象的名称 属性 以及一个复选框。当我 uncheck/check 一个或多个名称旁边的文本框时,我希望我必须根据 visibility
.
我想象的是这样的逻辑; 如果未选中复选框,将可见性设置为 false
下面的代码基本上是我要解释的内容的大纲,我知道它的语法并不完美,它更像是一个视觉指南来尝试展示我的要求。
再次抱歉,如果我解释得不好。
非常感谢您的帮助
<!-- this would be in my modal -->
<div>
<input type="checkbox"
value="usd" />
<label for="usd">USD</label>
</div>
<div>
<input type="checkbox"
value="cad"/>
<label for="cad">CAD</label>
</div>
<!-- End modal -->
<section v-for="loop through MyArray" v-show="myArray.visible">
<div>{{name}}</div>
<div>{{value}}</div>
<div>{{another}}</div>
<div>{{high}}</div>
<div>{{low}}</div>
</section>
myArray[
{
name:"USD",
value: 0.75,
another: 0,
high: 0,
low: 0,
visible:true},
{
name:"CAD",
value: 1.75,
another: 0,
high: 0,
low: 0,
visible:true},
]
我试图模拟一个适合你的情况的解决方案,下面的卡片代表一个模式,当 check/uncheck 复选框时,你的 table 中的值将被更改,你也 hide/show 根据其 visibility
状态的项目,例如:
<div class="flex" v-if="item.visible">...</div>
new Vue({
el: '#app',
data() {
return {
myArray:[
{
name:"USD",
value: 0.75,
another: 0,
high: 0,
low: 0,
visible:true},
{
name:"CAD",
value: 1.75,
another: 0,
high: 0,
low: 0,
visible:true},
],
selectedItem:{visible:false}
}
},
methods:{
setVisible(){
this.myArray= this.myArray.filter((item)=>{
if(item.name===this.selectedItem.name){
item.visible=this.selectedItem.visible;
return item;
}else{ return item}
})
}
}
});
.flex{
display:flex;
justify-content:space-between;
align-items:center;
padding:10px;
}
.mymodal{
padding:50px;
box-shadow:1px 1px 4px #555;
}
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<div id="app" class="container">
<section v-for="item in myArray" >
<div class="flex">
<div>{{item.name}}</div>
<div>{{item.value}}</div>
<div>{{item.another}}</div>
<div>{{item.high}}</div>
<div>{{item.low}}</div>
<div>{{item.visible}}</div>
<div><button class="btn btn-primary" @click="selectedItem=item"> Show details</button></div>
</div>
</section>
<div class="mymodal" v-if="selectedItem.visible">
<input type="checkbox"
v-model="selectedItem.visible" @change="setVisible">
<label for="cad">{{selectedItem.name}}</label>
</div>
</div>