V-for单击所有按钮,而不只是一个

时间:2019-06-18 18:12:31

标签: javascript arrays vue.js

我是Vue.js的新手。我正在尝试基于数组从v-for(正确渲染)渲染复选框。

然后我尝试在每个复选框旁边呈现一个按钮,该按钮将基于所选索引打开多选。但是,每当我单击渲染按钮时,它都会打开所有复选框按钮的多重选择。

HTML:

<div>
  <label class='label'>countrys:* </label><br><br>
  <div 
    v-for="(country, index) in countries" 
    class="label" 
    style="display: inline-block;">
    <input 
      type='checkbox' 
      value="country">&nbsp
    {{country.geos}} &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
    <img 
      class='addIcon' 
      v-bind="country" 
      :key="country.index" 
      style="width: 26px;height: 20px;margin-right: 8px;margin-top: px;margin-left: -25px;margin-bottom:-5px"
      src='../../images/createScreen/addClient@2x.png' 
      @click="makeMarketsActive($event, index)">
    <select multiple v-if="!isHidden">
      <option 
        v-for="(market) in country.markets" 
        v-bind="country">
        {{ market }}
      </option>
    </select>
  </div>
</div>

JS:

export default {
  name: "Update",
  components: {
  },
  data() {
    return {    
      countries:[
        {
          "index":0,
          "geos":"America",
          "markets":['a','b','c','d']
        },
        {
          "index":1,
          "geos":"Canada",
          "markets":['a','b']
         },
           "index":2,
           "geos":"Africa",
           "markets":['x','z']
         }
      ],
      isHidden:true
    }
  },
  makeMarketsActive(event, index) {
    this.isHidden = !this.isHidden;
  }

预期结果:单击为每个复选框渲染的图像时,我只想查看每个地理位置而不是全部的市场。

2 个答案:

答案 0 :(得分:1)

您也不需要额外的功能

HTML

<div id="app">

  <label class='label'>countries:* </label><br><br>
  <div v-for="(country, index) in countries" class="label" style="display: inline-block;">
    <input type='checkbox' value="country">{{country.geos}}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

    <img class='addIcon' v-bind="country" :key="country.index" style="margin-right: 8px;margin-top: px;margin-left: -25px;margin-bottom:-5px" src='https://via.placeholder.com/26x20' v-on:click="country.isVisible = !country.isVisible">
    <select multiple v-show="country.isVisible">
       <option v-for="(market) in country.markets" v-bind="country" >{{ market }}</option>
    </select>
  </div>

</div>

JS

new Vue({
  el: '#app',
  data: {
    countries: [{
        "index": 0,
        "geos": "America",
        "markets": ['a', 'b', 'c', 'd'],
        "isVisible": false
      },
      {
        "index": 1,
        "geos": "Canada",
        "markets": ['a', 'b'],
        "isVisible": false
      }, {
        "index": 2,
        "geos": "Africa",
        "markets": ['x', 'z'],
        "isVisible": false
      }
    ]
  }
})

答案 1 :(得分:0)

首先,如注释中所述,您正在通过常规属性isHidden处理每个按钮状态。因此,您需要将此属性添加到数据数组:

new Vue({
  el: "#app",
  data: {
    countries:[
      {
                "index":0,
                "geos":"America",
                "markets":['a','b','c','d'],
								"isHidden":true
                },
                {
                "index":1,
                "geos":"Canada",
                "markets":['a','b'],
								"isHidden":true
                },
                {"index":2,
                "geos":"Africa",
                "markets":['x','z'],
								"isHidden":true
                }
                ]
  },
  methods: {
  	makeMarketsActive(event, index) {
            this.countries[index].isHidden = !this.countries[index].isHidden;
}
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <label class='label'>countrys:* </label><br><br>
  <div v-for="(country, index) in countries" class="label" style="display: inline-block;">
    <input type='checkbox' value="country">&nbsp{{country.geos}} &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp

    <img class='addIcon' v-bind="country" :key="country.index" style="width: 26px;height: 20px;margin-right: 8px;margin-top: px;margin-left: -25px;margin-bottom:-5px" src='../../images/createScreen/addClient@2x.png' @click="makeMarketsActive($event, index)">
    <select multiple v-if="!country.isHidden">
                     <option v-for="(market) in country.markets" v-bind="country" >
                    {{ market }}
                         </option>
                        </select>
  </div>
</div>