【问题标题】:How to uncheck a checkbox bound to a vuex store state array item?如何取消选中绑定到 vuex 存储状态数组项的复选框?
【发布时间】:2020-12-22 06:22:35
【问题描述】:

我正在开发一项功能,其中用户有一个优先级列表,他们可以通过选中复选框将优先级标记为已完成(下图供参考)。

list of priorities with checkbox

在继续完成/完成优先级之前,我要求使用对话框进行确认。用户可以单击“确定”继续或单击“取消”关闭对话框而不继续操作。我已经完成了“确定”部分,但是对于“取消”,我需要复选框保持未选中状态(或者至少让它看起来没有选中)。为了处理这个问题,我将$event 对象传递给@change 事件处理程序,并在取消对话框时将目标值设置为false:evt.target.value = false;

我对这个解决方案持怀疑态度,因为我认为在视图模型中设置事件目标的值是不正确的,但我想不出别的办法。有没有其他方法来处理这种情况?

我的模板/代码是这样的(简化):

<ul>
  <li :key="item.id" v-for="(item, index) in priorities">
    <div>
      <input
        type="checkbox"
        :checked="item.isCompleted"
        @change="handleComplete(item, $event)"
      />
      <div>
        <a href="#">{{ item.title }}</a>
        <p class="subtitle is-7">{{ item.targetDateTime | short }}</p>
      </div>
    </div>
    <button>
      <fa :icon="['fas', 'minus-circle']" />
    </button>
  </li>
</ul>
export default {
  name: "PriorityList",
  computed: {
    ...mapState({
      priorities: state => state.priorities
    }),
  },
  methods: {
    ...mapActions({
      confirmComplete: "confirmComplete"
    }),
    handleComplete(priority, evt) {
      this.$buefy.dialog.confirm({
        title: "Confirm",
        message: "This action cannot be undone from the priorities list. Do you want to continue?",
        type: "is-danger",
        size: "is-small",
        onConfirm: async () => {
            // item.isCompleted is mutated in the confirmComplete() action
            await this.confirmComplete(priority);
        },
        onCancel: () => {
            // TODO: A different solution to uncheck checkbox
            evt.target.value = false; // current solution
        }
      });
    },
  }
};

注意:我没有在复选框中使用v-model,因为它绑定到来自 vuex 存储的数组项,当复选框值更改时,这将引发错误(不要改变突变之外的状态)。

【问题讨论】:

    标签: javascript vue.js vuex nuxt.js buefy


    【解决方案1】:

    使用 v-model 绑定到输入

      <input
        type="checkbox"
        :value="item.isCompleted"
        @input="handleComplete(index)"
          />
    

    用方法处理复选框状态变化

    handleComplete(index) {
          this.$buefy.dialog.confirm({
            title: "Confirm",
            message: "This action cannot be undone from the priorities list. Do you want to continue?",
            type: "is-danger",
            size: "is-small",
            onConfirm: async () => {
                // I suppose this is mutating the state and that the store will get updated automatically then
                await this.confirmComplete(this.priorities[index]);
            },
            onCancel: () => {
                // TODO: use store actions to mutate state
                this.priorities[index] = false
            }
          });
        },
    

    主要是关于模板绑定

    【讨论】:

    • 我尝试了您的解决方案,但没有成功。复选框在我单击后被选中,当我取消对话框时无法恢复。
    • 所以你想恢复它?在这种情况下,使用索引而不是项目 js &lt;input type="checkbox" :value="item.isCompleted" @input="handleComplete(index)" /&gt; js // handleComplete (index) onConfirm: () =&gt; this.confirmComplete(this.priorities[index]) onCancel: () =&gt; this.priorities[index] = false ** 直接改变状态是一种非常糟糕的做法 *** 考虑创建一个操作来切换优先级
    • 我知道不要在突变之外改变状态,这就是为什么我在帖子中提到我没有使用v-model,而是使用:checked@change。我面临的问题是我是否通过单击对话框上的ok 按钮通过confirmComplete 操作提交item.isComplete 的状态更改,或者通过取消对话框什么都不做,复选框保持选中状态。我的解决方法是将evt.target.value = false 设置为在单击cancel 时取消选中(或恢复其值)复选框。除了直接设置evt.target.value,还有其他方法吗?
    • 这正是我根据您建议的解决方案所做的:我将:checked 替换为:value,将@change 替换为@input。然后我添加了一个突变:setNotCompleted(state, index) { state.priorities[index].isCompleted = false; }。我调用对话框的这个突变onCancel。它没有用。
    猜你喜欢
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    相关资源
    最近更新 更多