天天看點

用vue腳手架寫的簡易留言闆

1.配置安裝好腳手架

2.留言闆:

<template>
  <div class="about">
    <input type="text" v-model="value" @keydown.enter="add" />
    <button @click="add">送出</button>
    <h1>{{value}}</h1>
    <h3>未完成({{listno.length}}):</h3>
    <button @click="dels1(index)">全部删除</button>
    <table>
      <tr>
        <th>選項</th>
        <th>内容</th>
        <th>送出時間</th>
        <th>間隔時間</th>
        <th>操作</th>
      </tr>
      <tr v-for="(item, index) in listno" :key="index">
        <td>
          <input type="checkbox" @click.prevent="changeYes($event,item,index)" />
        </td>
        <td>
          <span v-if="item.isShow">{{item.text}}</span>
          <input type="text" v-model="item.text" @blur="updateblur1(index)" v-else autofocus />
        </td>
        <td>{{item.times}}</td>
        <td>{{item.time | timeFilters}}</td>
        <td>
          <button @click="del1(index)">删除</button>
          <button @click="update1(index)">修改</button>
        </td>
      </tr>
    </table>
    <h3>已完成({{listyes.length}}):</h3>
    <button @click="dels2(index)">全部删除</button>
    <table>
      <tr>
        <th>選項</th>
        <th>内容</th>
        <th>送出時間</th>
        <th>間隔時間</th>
        <th>操作</th>
      </tr>
      <tr v-for="(item, index) in listyes" :key="index">
        <td>
          <input checked type="checkbox" name id @click.prevent="changeno($event,item,index)" />
        </td>
        <td>
          <span v-if="item.isShow">{{item.text}}</span>
          <input type="text" v-model="item.text" @blur="updateblur2(index)" v-else autofocus />
        </td>
        <td>{{item.times}}</td>
        <td>{{item.time |timeFilters}}</td>
        <td>
          <button @click="del2(index)">删除</button>
          <button @click="update2(index)">修改</button>
        </td>
      </tr>
    </table>
  </div>
</template>

<script>
export default {
  filters: {
    timeFilters(ms) {
      let daystr = "";
      let date = new Date();
      let now = date.getTime();
      let rel = (now - ms) / 1000 / 60;
      // console.log(now/1000/60)
      // console.log(ms/1000/60)
      // console.log(rel)
      if (rel<5) {
        daystr = "剛剛";
      } else if (rel >= 5&&rel<10) {
        daystr = "5分鐘前";
      } else if (rel >= 10&&rel<30) {
        daystr = "10分鐘前";
      } else if (rel >= 30&&rel<40) {
        daystr = "30分鐘前";
      }else if (rel >= 40&&rel<60) {
        daystr = "40分鐘前";
      } else if (rel >= 60&&rel<120) {
        daystr = "1小時前";
      } else if (rel >= 120&&rel<300) {
        daystr = "2小時前";
      } else if (rel >= 300&&rel<1440) {
        daystr = "5小時前";
      } else if (rel >= 1440&&rel<2880) {
        daystr = "1天前";
      } else if (rel >= 2880&&rel<10080) {
        daystr = "2天前";
      } else if (rel >= 10080&&rel<43200) {
        daystr = "7天前";
      } else if (rel >= 43200) {
        daystr = "30天前";
      } 
      return daystr;
    }
  },
  data() {
    return {
      value: "",
      listyes: [],
      listno: [],
      
    };
  },
  created() {
    let listno = localStorage.listno;
    if (listno != undefined) {
      this.listno = JSON.parse(listno);
    }
    let listyes = localStorage.listyes;
    if (listyes != undefined) {
      this.listyes = JSON.parse(listyes);
    }
  },
  methods: {
    // 添加送出
    add() {
      if (this.value !== "") {
        this.listno.push({
          isShow: true,
          text: this.value,
          time: new Date().getTime(),
          times: new Date().toLocaleString().replace(/\//g, "-")
        });
        this.value = "";
        localStorage.listno = JSON.stringify(this.listno);
      }
    },
    updateblur1(index) {
      this.listno[index].isShow = true;
      localStorage.listno = JSON.stringify(this.listno);
    },
    updateblur2(index) {
      this.listyes[index].isShow = true;
      localStorage.listyes = JSON.stringify(this.listyes);
    },
    // 全部删除未完成
    dels1(index) {
      if (this.listno.length == 0) {
        alert("資料為零,删除不了");
      }
      this.listno.splice(index, this.listno.length);
      localStorage.listno = JSON.stringify(this.listno);
    },
    // 全部删除已完成
    dels2(index) {
      if (this.listyes.length == 0) {
        alert("資料為零,删除不了");
      }
      this.listyes.splice(index, this.listyes.length);
      localStorage.listyes = JSON.stringify(this.listyes);
    },
    // 删除未完成
    del1(index) {
      this.listno.splice(index, 1);
      localStorage.listno = JSON.stringify(this.listno);
    },
    // 修改未完成
    update1(index) {
      // let x = prompt("請修改");
      // console.log(x);
      // this.listno.splice(index, 1, x);
      this.listno[index].isShow = false;
      localStorage.listno = JSON.stringify(this.listno);
    },
    // 删除已完成
    del2(index) {
      this.listyes.splice(index, 1);
      localStorage.listyes = JSON.stringify(this.listyes);
    },
    // 修改已完成
    update2(index) {
      // let x = prompt("請修改");
      // console.log(x);
      // this.listyes.splice(index, 1, x);
      this.listyes[index].isShow = false;
      localStorage.listyes = JSON.stringify(this.listyes);
    },
    // 點選複選框顯示到已完成
    changeYes(e, item, index) {
      // console.log(e.target.checked)
      var checked = e.target.checked;
      if (checked) {
        this.listyes.push(item);
        this.listno.splice(index, 1);
      }
      localStorage.listno = JSON.stringify(this.listno);
      localStorage.listyes = JSON.stringify(this.listyes);
    },
    // 點選顯示到未完成
    changeno(e, item, index) {
      // console.log(e.target.checked)
      var checked = e.target.checked;
      if (!checked) {
        this.listno.push(item);
        this.listyes.splice(index, 1);
      }
      localStorage.listyes = JSON.stringify(this.listyes);
      localStorage.listno = JSON.stringify(this.listno);
    }
  }
};
</script>


<style scoped>
table {
  margin: 0 auto;
  /* width: 600px; */
}
td,th{
  width: 300px;
}
h1 {
  height: 20px;
}
li {
  list-style-type: none;
}
</style>
      

繼續閱讀