Vue不确定复选框绑定

时间:2017-05-09 03:38:24

标签: checkbox binding vue.js

我正在使用vue进行数据绑定。 我想创建一个用于访问级别控制的小部件,因此我需要允许,拒绝和不确定状态。

这个标记很好,但没有不确定的状态:

<div class="row" v-for='a in context.This.Actions'>
    <div class="col-96">
        <input class="custom-control-input access-checkbox" v-bind:id="'chk_'+a.Name" v-bind:value="a.Id" v-model="context.This.RoleActions" indeterminate="true" type="checkbox" />
        <label class="pointer" v-bind:for="'chk_'+a.Name">{{ a.Name }}</label>
    </div>
</div>

变量是:

context.This.Actions = [
    { "Id": "id_1",
      "Name": "AAA"
    },
    { "Id": "id_2",
      "Name": "BBB"
    },
    { "Id": "id_3",
      "Name": "CCC"
    }
]

context.This.RoleActions = [ "id_1", "id_2" ]

我想要这个改变:

context.This.RoleActions = [ {"id_1":true}, {"id_2":false} ]

我希望得到以下结果:

  • 第一个复选框:已选中

  • 第二个复选框:未选中

  • 另一个:不确定

2 个答案:

答案 0 :(得分:4)

Indeterminate是一个复选框上的DOM属性,这意味着将它放在标记中不起作用,需要以编程方式应用它。

即使这样做,请记住仍然选中或未选中复选框的状态。处理表单时请记住这一点很重要。差异只是视觉上的。 (source)

考虑到这些警告,在Vue 2中,您可以向一个复选框添加一个不确定的属性,如下所示:

<input type="checkbox" indeterminate.prop="true">

或绑定到组件中的动态值:

<input type="checkbox" :indeterminate.prop="dataProperty">

我建议考虑重构。

答案 1 :(得分:1)

我也遇到了类似问题,即使用带有复选框的道具来支持2和3状态。 为了解决这个问题,我使用Vuetify复选框使用getter和setter的计算属性

这是我的例子

<template>
  <v-container class="checkbox-container">
    <v-checkbox
      type="checkbox"
      :indeterminate="indeterminate"
      :color="indeterminate ? '#767575' : 'success'"
      v-model="internalState"
      @click.stop="onCheckbox"
      @keyup.space.prevent="onCheckbox"
    ></v-checkbox>
  </v-container>
</template>

<script>
/**
 * Responsability: boolean field editor checkbox
 * When @threeState is true : following states (check, uncheck, indeterminate) otherwise (check, uncheck)
 * @checkboxState is an external state where it contains always the current state of checkbox
 **/
export default {
  model: {
    // v-model prop
    prop: 'checkboxState',
  },
  props: {
    threeState: Boolean,
    /**
     * Init state is the starting state Which the chekbox starts from.
     * by defining initstate it will ignore the default input @boolProperty
     **/
    initState: {
      type: String,
      default: 'false',
    },
    // Reperesent the value of checked state in model
    config: {
      type: Object,
      default: () => ({
        checked: 'true',
        unchecked: 'false',
        indeterminate: null,
      }),
    },
    checkboxState: {
      type: String,
    },
  },

  data() {
    return {
      internalState: this.checkboxState,
      indeterminate: false,
    }
  },

  computed: {
    state: {
      get() {
        return this.checkboxState
      },
      set(newState) {
        this.changeCheckboxState(newState)
      },
    },
  },

  // Change the state of checkbox after DOM is mounted
  mounted() {
    this.changeCheckboxState(this.initState)
  },

  methods: {
    changeCheckboxState(state) {
      this.$vnode.data.model.callback(state)
      this.internalState = state === this.config.checked
      this.indeterminate = state === this.config.indeterminate
    },

    onCheckbox() {
      if (this.threeState) {
        switch (this.state) {
          case this.config.unchecked:
            this.state = this.config.checked
            break
          case this.config.checked:
            this.state = this.config.indeterminate
            break
          case this.config.indeterminate:
            this.state = this.config.unchecked
            break
        }
      } else {
        this.state = (!(this.state === this.config.checked)).toString()
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.checkbox-container {
  width: 50px;
}
</style>