简体   繁体   English

Vue 只允许数组中的一个元素为真

[英]Vue allow only one element in array be true

I'm creating multiple input fields with checkboxes in vue and I want that only one can be true.我在 vue 中创建了多个带有复选框的输入字段,我希望只有一个可以为真。 So if the user clicks on one the others should be false so that only the last clicked checkbox is true.因此,如果用户点击一个其他的应该是假的,这样只有最后一次点击的复选框是真的。

My code is like that:我的代码是这样的:

new Vue({
  el: "#app",
  data: {
      selected: null,
      options: [
          {"id": 1, "title": "One", "value": false},
          {"id": 2, "title": "Two", "value": false },
          {"id": 3, "title": "Three", "value": false},
      ]
  },
  watch: {
    selected(selected) {
      this.options.forEach((option, index) => {
        option.id == selected ? option.value = true : option.value = false;
      });
    }
  }

Unfortunately my watcher isn't working properly.不幸的是,我的观察者工作不正常。 I would be really glad if somebody can show me how to correct it.如果有人能告诉我如何纠正它,我会很高兴。 I want that always the last true element is the only true element and the watches sets all other elements in options to false.我希望最后一个真正的元素总是唯一的真正元素,手表将选项中的所有其他元素设置为假。

If i understood your requirements correctly you can still do it with radio buttons.如果我正确理解了您的要求,您仍然可以使用单选按钮来完成。 You can specify the value to be used inside the selected variable, as described here: https://v2.vuejs.org/v2/guide/forms.html#Radio-1 .您可以指定要在selected变量中使用的值,如下所述: https://v2.vuejs.org/v2/guide/forms.html#Radio-1 This means that you can set up a watcher and then mutate the options list accordingly:这意味着您可以设置一个观察者,然后相应地改变选项列表:

selected: function (newVal) {
    this.options.forEach(option => {
      if (option.id === newVal) option.value = true
      else option.value = false
    })
    console.log(this.options)
}

Here is a sandbox to see it in action: https://codesandbox.io/s/heuristic-goldberg-lilsw这是一个沙箱,可以看到它的实际效果: https://codesandbox.io/s/heuristic-goldberg-lilsw

Update: Just saw that you want to use the </b-switch> from buefy.更新:刚刚看到你想使用 buefy 的</b-switch> You can still do something similar by calling a function from the input event which then mutates the options list according to the just changed element.您仍然可以通过从input事件调用 function 来执行类似的操作,然后根据刚刚更改的元素更改选项列表。 Something like this:像这样的东西:

<div v-for="(option,index) in options" :key="index">
      <div class="box">
        <div class="field">
           <b-switch v-model="option.value" @input="(modelValue) => onSwitchChanged(modelValue, option.id)">
            {{ option.title }}
           </b-switch>
          <label :for="index">{{ option.title }}</label>
        </div>
    </div>
</div>

function onSwitchChanged(modelValue, id) {
    if (!modelValue) return
    this.options.forEach(option => {
      if (option.id === id) option.value = true
      else option.value = false
    })
}

If you want that Only One will be selected then you have to use radio button.如果您希望只选择一个,那么您必须使用radio按钮。 Checkbox has options to select all But One by One . Checkbox具有 select all But One by One 的选项。

Without watch you can use methods .没有watch你可以使用methods Pass index to the method.将索引传递给方法。

<input 
   type="checkbox"
   :id="index" 
   :value="option.id" 
   @click="selectAnOption(index)"
>

Method:方法:

methods: {
  selectAnOption(index) {
   this.options[index].value = true
  }
}

Full Code here: https://jsfiddle.net/8ktdp9ew/完整代码在这里: https://jsfiddle.net/8ktdp9ew/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM