1

Sorry in advance if I'm explaining this poorly, I'm a bit of novice at this.

If I have an array that contains multiple objects similar to below, how in Vue do I unselect/select a checkbox in a modal to toggle the value of the visible property based on it's name? I just want to show items that have a visible of true.

Currently, I have a modal popup that displays the name property for each object along with a checkbox. When I uncheck/check the textbox beside one or multiple of the names, I would like the list I have to update, based on the visibility.

I'm picturing the logic to be; if checkbox unchecked, set visibility to false

The code below is basically an outline to what I'm trying to explain, I know it's not syntax perfect, it's more of a visual guide to try and show what I'm asking.

Again, sorry if I'm explaining this poorly.

Thanks so much for any help

<!-- 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},
           ]
0

1 Answer 1

1

i tried to simulate a solution to fit to your case, the card in below represents a modal and when check/uncheck the checkbox the value in your table would be changed, you also hide/show the item according to its visibility state like :

   <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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.