待办事项网页实现-底部交互

121 阅读2分钟

准备

把原来的todos.length改掉,
image.png 换成total,再用computed计算
image.png

实现全选或取消全选

方法一

当所有的事项都完成打✔时,footer上的方框也打✔

image.png

上图这个打✔还是不打✔取决于下面这两个相不相等
image.png

所以我们可以这样
image.png

那么如果我们在洗碗这里打✔
image.png

最下面这个也会自动打✔

image.png

但是这样表达式就会比较复杂,我们还是通过computed来计算
image.png

这样也实现了
image.png

解决事项全清除后footer的方框还打✔的问题

但是这有一个bug,如果我把所有的已完成的实现都清除了,footor上的方框不应该打✔
image.png

所以我们要加些前提(当事项总数不等于0时)
image.png

这样就解决问题了
image.png

所有事项清除后footer这个也不显示

用v-show就可以实现所有事项清除后footer这个也不显示
image.png 效果如下
image.png

将footer上的方框打✔后,所有事项全选

先给footer上的方框一个change事件,并且获取方框的改变
image.png

因为我们要改变的是App下的todos中的done,所以我们要在App中添加方法
image.png

然后把方法传给footer
image.png

footer接收,并且调用该方法
image.png

这样点击footer上的方框打勾
image.png

就实现了全选
image.png

取消全选也可以
image.png

方法二

可以用v-model替代下面这两个

image.png image.png

这样是可以的
image.png

但是如果我们将footer的方框打勾上,就会出现报错
image.png

这是因为我们在isAll这里写的是简写形式,只能读取,不能改写
image.png

所以我们要写完整
image.png

这样当我们打勾时,就不会报错了
image.png

为了让所有的事项随footer方框的变化而变化,我们将checkAllTodo方法放在set里面
image.png

这样就可以得到我们想要的效果了
image.png

实现清除已完成事项

在清除已完成任务按钮上加上一个点击事件叫clearAll
image.png

因为我们还是要对app上的todos进行改变,清除todos中done为true的todo,所以要在app上加上一个清除方法clearAllTodo
image.png

将clearAllTodo方法传给footer
image.png

在footer上接收方法,并且调用该方法
image.png

在后面加一个弹窗效果
image.png

最后效果就是这样:

点击清除已完成任务按钮
image.png

会弹窗让你进行确认
image.png

点击确定后它会完成清除
image.png

这结的主要代码如下:

UserFooter.vue

<template>
  <div class="todo-footer" v-show="total">
    <label>
      <input type="checkbox" v-model="isAll" />
    </label>
    <span> <span>已完成{{ doneTodal }}</span> / 全部{{ total }} </span>
    <button class="btn btn-danger" @click="clearAll">清除已完成任务</button>
  </div>
</template>

<script>
// 组件交互相关的代码(数据、方法等等)
export default {
  name: "UserFooter",
  data() {
    return {};
  },
  props: ["todos", "checkAllTodo", "clearAllTodo"],
  computed: {
    total() {
      return this.todos.length;
    },
    doneTodal() {
      // let i = 0;
      // this.todos.forEach((todo) => {
      //   if (todo.done) i++;
      // });
      // return i;

      // return this.todos.reduce((pre, current) => {
      //   console.log("@", pre, current);
      //   return pre + (current.done ? 1 : 0);
      // }, 0);

      return this.todos.reduce(
        (pre, current) => pre + (current.done ? 1 : 0),
        0
      );

      // const a = this.todos.filter(todo =>  todo.done == true);
      // return a.length;
    },
    isAll: {
      get() {
        return this.total === this.doneTodal && this.total !== 0;
      },
      set(value) {
        // console.log("@@@",value)
        this.checkAllTodo(value);
      },
    },
  },
  methods: {
    // checkAll(e) {
    //   // console.log(e.target.checked);
    //   this.checkAllTodo(e.target.checked);
    // },
    clearAll() {
      if (confirm("确定清除吗?")) {
        this.clearAllTodo();
      }
    },
  },  
};
</script>

<style scoped>
/* footer */
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding: 0 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  vertical-align: middle;
  margin-right: 5px;
  position: relative;
  top: -1px;
  cursor: pointer;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>

App.vue

<template>
  <div id="root">
    <div class="todo-container">
      <div class="todo-wrap">
        <UserHeader :AddTodoObj="AddTodoObj" />
        <UserList :todos="todos" :checkTodo="checkTodo" :deleteTodo="deleteTodo" />
        <UserFooter :todos="todos" :checkAllTodo="checkAllTodo"         :clearAllTodo="clearAllTodo"/>
      </div>
    </div>
  </div>
</template>

<script>
// 引入组件
import UserHeader from "./components/UserHeader.vue";
import UserList from "./components/UserList.vue";
import UserFooter from "./components/UserFooter.vue";

export default {
  name: "App",
  components: { UserHeader, UserList, UserFooter },
  data() {
    return {
      todos: [
        { id: "001", title: "打代码", done: true },
        { id: "002", title: "看电视剧", done: true },
        { id: "003", title: "洗碗", done: false },
      ],
    };
  },
  methods: {
    // 添加一个todo
    AddTodoObj(todoObj) {
      this.todos.unshift(todoObj);
      // console.log("我是App组件,我收到了数据", x);
    },
    // 勾选or取消勾选一个todo
    checkTodo(id) {
      this.todos.forEach((todo) => {
        if (todo.id === id) todo.done = !todo.done;
      });
    },
    // 删除一个todo
    deleteTodo(id) {
      this.todos = this.todos.filter((todo) => todo.id !== id);
    },
    //全选or取消全选
    checkAllTodo(done) {
      this.todos.forEach((todo) => {
        todo.done = done;
      });
    },
    // 清除所有已完成
    clearAllTodo() {
      this.todos = this.todos.filter((todo) => {
        // return todo.done == false;
        return !todo.done;
      });
    },
  },
};
</script>

<style>
/* base */
body {
  background-color: #fff;
  /* margin: 0; */
}

.todo-container {
  width: 600px;
  margin: 0 auto;
  /* background-color: antiquewhite; */
}

.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  /* margin-bottom: 0; */
  font-size: 14px;
  /* line-height: 20px; */
  /* text-align: center; */
  /* vertical-align 用来指定行内元素(inline)或表格单元格(table-cell)元素的垂直对齐      方式 */
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0px rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn:focus {
  /* outline: none; */
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}
</style>