【问题标题】:How to have Vue dynamic button disabled based on checkbox from list如何根据列表中的复选框禁用 Vue 动态按钮
【发布时间】:2021-10-15 17:58:56
【问题描述】:

我在学习 Vue 时创建了一个 Todo 应用程序,到目前为止一切都很好。我有一个问题,最初我将任务删除按钮设置为禁用,并且希望仅在选中相关复选框时才启用它。截至目前,当我单击任何任务复选框时,所有按钮都会受到影响。不确定如何创建与这 2 个元素的关系。

 <template>
        <div class="mt-5 todos-list">
          <div
            v-for="todo in todos"
            :key="todo.id"
            class="flex justify-between pb-4 todos-item"
          >
            <div class="flex items-center mr-10 todos-item__checkbox">
              <input
                v-model="checkboxes"
                :value="todo.task.title"
                type="checkbox"
                class="w-4 h-4 mr-2 border-2 border-red-500 appearance-none checked:bg-red-600 checked:border-transparent"
                @change="checkBoxChecked($event)"
              />
              <label for="todo" class="text-lg font-medium todos-checkbox ">{{
                todo.task.title
              }}</label>
            </div>
            <div class="todos-item__button">
              <button
                class="rounded-lg btn btn-delete"
                :class="{ disabled: btnDisabled[index] }"
                type="button"
                :disabled="btnDisabled[index]"
                @click="deleteTask(todo.task.id)"
              >
                Delete
              </button>
            </div>
        <AddTodo @addTodo="addTask" />
      </div>
    </template>
    
    <script>
    export default {
      data() {
        return {
          checkboxes: [],
          btnDisabled: [],
          todos: [
            {
              task: {
                id: 1,
                title: 'Go to store',
                isComplete: false
              }
            },
            {
              task: {
                id: 2,
                title: 'Go for walk',
                isComplete: false
              }
            },
            {
              task: {
                id: 3,
                title: 'Take dog out',
                isComplete: false
              }
            }
          ]
        }
      },
  mounted() {
    const disableArr = this.todos.map((el) => true)
    this.btnDisabled.push(...disableArr)
  },
      methods: {
        addTask(title) {
          this.todos.push({
            task: {
              id: Math.ceil(Math.random() * 1000000),
              title,
              isComplete: false
            }
          })
        },
        deleteTask(id) {
          const index = this.todos.findIndex((item) => item.id === id)
          this.todos.splice(index, 1)
        },
         checkBoxChecked(event, index) {
  if (event.target.checked) {
    this.btnDisabled = this.btnDisabled.map((el, i) => index !== i)
  } else {
    this.btnDisabled = this.btnDisabled.map((el, i) => index === i)
  }
}
      }
    }
    </script>

【问题讨论】:

    标签: javascript vue.js nuxt.js


    【解决方案1】:

    我认为这是因为您的所有待办事项只有一个布尔状态。如果您将btnDisabled 数据更改为数组,也许它会起作用,因此每个待办事项都会有自己的禁用或未禁用状态。

    这是我试图指出的一种方式

    将 btnDisabled 改为数组

    btnDisabled = this.todos.map(el => true)
    

    为 v-for 添加索引

    v-for="(todo,index) in todos"
    

    将索引传递给@change

    @change="checkBoxChecked($event,index)"
    

    修改checkBoxChecked方法

    checkBoxChecked(event, index) {
      if (event.target.checked) {
        this.btnDisabled = this.btnDisabled.map((el,i) => index === i ? false : true)
      } else {
        this.btnDisabled = this.btnDisabled.map((el,i) => index === i ? true : false)
      }
    }
    

    将禁用绑定到 btnDisabled 的特定索引

    :disabled="btnDisabled[index]"
    

    希望这能解决您的问题

    【讨论】:

    • 嗨@Qoyyima Fias Salam!感谢您的输入!为我收拾东西。我现在看到的唯一问题是 checkBoxChecked 方法返回此错误:在条件表达式中不必要地使用布尔文字
    • 嗨@MonTOne,很高兴为您提供帮助!也许你可以改变 'index === i ? false : true' 变成 'index !== i' 和 'index === i ? true : false' 变成 'index === i' ,看看错误是否仍然显示。如果答案确实回答了您的问题,请将答案设置为已接受的答案,谢谢!
    • 是的,我实际上改变了之前的错误,但仍然无法正常运行。在初始页面加载时,仅禁用按钮的第一个实例。我尝试在解决该问题的已安装生命周期上创建数组映射。现在发生的情况是,当我单击一个复选框时,它会删除正确的禁用状态,但其他项目也会删除禁用状态。
    • 用新的变化更新了原始帖子
    猜你喜欢
    • 1970-01-01
    • 2019-09-11
    • 2019-06-02
    • 2020-11-23
    • 2018-03-13
    • 1970-01-01
    • 2022-12-31
    • 2019-07-17
    • 1970-01-01
    相关资源
    最近更新 更多