vue实现todolist

vue实现todolist_第1张图片

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>todolist</title>
    <!-- Latest compiled and minified CSS -->
    <link
      rel="stylesheet"
      href="http://netdna.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"
    />
  </head>
  <body>
    <div class="container" id="app">
      <div class="panel panel-info">
        <div class="panel-heading">
          <h3 class="panel-title">待办事项</h3>
        </div>
        <div class="panel-body">
          <input
            type="text"
            class="form-control"
            v-model="txt"
            placeholder="请输入代办事项"
            @keyup="save"
          />

          <ul class="list-group">
            <li class="list-group-item" v-for="todo in todos" :key="todo.id">
              <div class="checkbox">
                <label>
                  <input type="checkbox" v-model="todo.checked" />
                  {{todo.status===0?'【未完成】':'【已完成】'}}{{todo.content}}
                  <button
                    type="button"
                    @click="todo.status=1"
                    class="btn btn-info btn-sm"
                    v-show="todo.status==0"
                  >
                    完成
                  </button>
                  <button
                    @click="delOne(todo)"
                    type="button"
                    class="btn btn-danger btn-sm"
                  >
                    删除
                  </button>
                </label>
              </div>
            </li>
          </ul>

          <div class="checkbox">
            <label>
              <input type="checkbox" v-model="checkAll" />
              全选
              <button @click="endChecked">完成</button>
            </label>
          </div>
        </div>
      </div>
    </div>
    <script src="./libs/vue.js"></script>
    <script>
      // { id: 1,content: '起床', status: 0 }
      new Vue({
        el: "#app",
        data: {
          txt: "",
          todos: [],
        },
        methods: {
          save(e) {
            if (e.keyCode === 13) {
              this.todos.push({
                id: Date.now(),
                content: this.txt,
                status: 0,
                checked: false,
              });
              this.txt = "";
            }
          },
          delOne(item) {
            // var index = ;
            this.todos.splice(
              this.todos.findIndex((i) => i.id === item.id),
              1
            );
          },
          endChecked() {
            this.todos
              .filter((item) => item.checked)
              .forEach((item) => (item.status = 1));
          },
        },
        computed: {
          checkAll: {
            get() {
              if (this.todos.length == 0) {
                return false;
              }
              return (
                this.todos.filter((item) => item.checked).length ===
                this.todos.length
              );
            },
            set(v) {
              this.todos.forEach((item) => (item.checked = v));
            },
          },
        },
      });
    </script>
  </body>
</html>

你可能感兴趣的:(vue)