【问题标题】:Vue indeterminate checkbox bindingVue不确定复选框绑定
【发布时间】:2017-05-09 03:38:24
【问题描述】:

我正在使用 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} ]

我希望得到以下结果:

  • 第一个复选框:选中

  • 第二个复选框:未选中

  • 另一个:不确定

【问题讨论】:

    标签: checkbox binding vue.js


    【解决方案1】:

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

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

    考虑到这些注意事项,在 Vue 2 中,您可以将不确定的属性添加到复选框,如下所示:

    &lt;input type="checkbox" indeterminate.prop="true"&gt;

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

    &lt;input type="checkbox" :indeterminate.prop="dataProperty"&gt;

    我建议在重构时考虑到这一点。

    【解决方案2】:

    我在使用带有复选框的道具来支持 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-16
      相关资源
      最近更新 更多