vue中兩行程式碼實現全選及子選項全部選中,則全選按鈕選中,反之有一個沒選中,就取消選中全選按鈕

every() 方法使用指定函數檢測數組中的所有元素:

  • 如果數組中檢測到有一個元素不滿足,則整個表達式返回 false ,且剩餘的元素不會再進行檢測。
  • 如果所有元素都滿足條件,則返回 true。

邏輯是不是很熟悉

data: {
    list: [
        { id: '0', name: '吃飯', completed: true },
        { id: '1', name: '睡覺', completed: true },
        { id: '2', name: '打豆豆', completed: false }
      ]
}
computed : {
    toggleAll : {
        get () {
            return this.list.every(v => v.completed)
        },
        set (val) {
            this.list.map(v => v.completed = val)
        }
    }
}
Tags: