欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue實(shí)現(xiàn)圖書管理小案例

 更新時(shí)間:2020年12月03日 08:40:14   作者:代碼在路上  
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)圖書管理小案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Vue實(shí)現(xiàn)圖書管理的具體代碼,供大家參考,具體內(nèi)容如下

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
     .grid{
      margin:auto;
      width:500px;
      text-align:center;
     }
     .grid table{
      width:100%;
      border-collapse:collapse;
     }
     .grid th,td{
      padding:10px;
      border:1px solid orange;
      height:35px;
      line-height:35px;
     }
     .grid th{
      background-color:orange;
     }
     .book{
      background-color:orange;
      border-bottom:1px solid #ccc;
      padding:5px;
     }
     input{
      width:150px;
      outline:none;
     }
     .grid .total{
      height:30px;
      line-height:30px;
      background-color:orange;
      border-bottom:1px solid #ccc;
     }
  </style>
</head>
<body>
<div id="app">
  <div class="grid">
   <div>
     <h1>圖書管理</h1>
     <div class="book">
      <label for="id">編號(hào):</label>
      <input type="text" id="id" v-model='id' :disabled='flag' v-focus>
      <label for="name">名稱:</label>
      <input type="text" id="name" v-model='name'>
      <button @click='handle' :disabled='submitFlag'>提交</button>
     </div>
   </div>
   <div class="total">
     <span>圖書總數(shù):</span>
     <span>{{total}}</span>
   </div>
   <table>
     <thead>
       <tr>
        <th>編號(hào)</th>
        <th>名稱</th>
        <th>時(shí)間</th>
        <th>操作</th>
       </tr>
     </thead>
     <tbody>
     <tr :key='item.id' v-for='item in books'>
       <td>{{item.id}}</td>
       <td>{{item.name}}</td>
       <td>{{item.date | format('yyyy-MM-dd hh:mm:ss')}}</td>
       <td>
         <a href="" @click.prevent='toEdit(item.id)'>修改</a>
         <span>|</span>
         <a href="" @click.prevent='deleteBook(item.id)'>刪除</a>
       </td>
     </tr>
     </tbody>
   </table>
</div>
<script src="js/vue.js"></script>
<script>
  //自定義指令
  Vue.directive('focus',{
    inserted:function(el){
      el.focus();
    }
  })
  //過濾器(格式化日期)
  Vue.filter('format', function(value, arg) {
      function dataFormat(date, format) {
        if (typeof date === "string") {
          var mts = date.match(/(\/Date\((\d+)\)\/)/);
          if (mts && mts.length >= 3) {
            date = parseInt(mts[2]);
          }
        }
        date = new Date(date);
        if (!date || date.toUTCString() == "Invalid Date") {
          return "";
        }
        var map = {
          "M": date.getMonth() + 1, //月
          "d": date.getDate(), //日
          "h": date.getHours(), //小時(shí)
          "m": date.getMinutes(), //分
          "s": date.getSeconds(), //秒
          "q": Math.floor((date.getMonth() + 3) / 3), //季度
          "S": date.getMilliseconds() //毫秒
        };
        format = format.replace(/([yMdhmsqS])+/g, function(all, t) {
          var v = map[t];
          if (v !== undefined) {
            if (all.length > 1) {
              v = '0' + v;
              v = v.substr(v.length - 2);
            }
            return v;
          } else if (t == 'y') {
            return (date.getFullYear() + '').substr(4 - all.length);
          }
          return all;
        });
        return format;
      }
      return dataFormat(value, arg);
    })

    var vm=new Vue({
      el:'#app',
      data:{
       flag:false,
       submitFlag:false,
       id:'',
       name:'',
       books:[]
      },
      methods:{
       handle:function(){
         if(this.flag){
          //修改操作:就是根據(jù)當(dāng)前的id去更新數(shù)組中對(duì)應(yīng)的數(shù)據(jù)
          //箭頭函數(shù)的this不是window
          //some方法判斷什么時(shí)候終止循環(huán)
          this.books.some((item)=>{
            if(item.id==this.id){
              item.name=item.name;
              //完成更新操作之后,要終止循環(huán)
              return true;
            }
          });
          this.flag=false;
         }else{
          //添加操作
          //添加圖書
          var book={};
          book.id=this.id;
          book.name=this.name;
          book.date=new Date();
          this.books.push(book);
         }
         //清空表單
         this.id='';
         this.name='';
       },//handle結(jié)束
       toEdit:function(id){
         //禁止修改id
         this.flag=true;
         //根據(jù)id查詢出要編輯的數(shù)據(jù)
         var book=this.books.filter(function(item){
           return item.id==id;
         });
         //把獲取的信息填充到表單
         this.id=book[0].id;
         this.name=book[0].name;
       },//toEdit結(jié)束
       deleteBook:function(id){
         //刪除圖書
         //根據(jù)id從數(shù)組中查找元素的索引
         var index=this.books.findIndex(function(item){
           return item.id==id;
         });
         //根據(jù)索引刪除數(shù)組元素
         this.books.splice(index,1);
         
         //方法二:通過filter方法進(jìn)行刪除
         //this.books=this.books.filter(function(item){
          //return item.id!=id;
         //});
       }//deleteBook結(jié)束
      },
      computed:{
       total:function(){
         //計(jì)算圖書的總數(shù)
         return this.books.length;
       }
      },//computed結(jié)束
      watch:{
       name:function(val){
         //驗(yàn)證圖書名稱是否已經(jīng)存在
         var flag=this.books.some(function(item){
          return item.name==val;
         });
         if(flag){
          //圖書名稱存在
          this.submitFlag=true;
         }else{
          this.submitFlag=false;
         }
       }
      },//watch結(jié)束
      mounted:function(){
       //該生命周期鉤子函數(shù)被觸發(fā)的時(shí)候,模板已經(jīng)可以使用
       //一般用于獲取后臺(tái)數(shù)據(jù),然后把數(shù)據(jù)填充到模板
       //模擬接口
       var data=[{
         id:1,
         name:'三國演義',
         date:1585609975000
       },{
         id:2,
         name:'水滸傳',
         date:1586609975000
       },{
         id:3,
         name:'紅樓夢',
         date:1587609975000
       },{
         id:4,
         name:'西游記',
         date:1588609975000
       }];
       this.books=data;
      }
    });
</script>
</body>
</html>

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 一個(gè)可復(fù)用的vue分頁組件

    一個(gè)可復(fù)用的vue分頁組件

    這篇文章主要為大家詳細(xì)介紹了一個(gè)可復(fù)用的vue分頁組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • 解決vue 子組件修改父組件傳來的props值報(bào)錯(cuò)問題

    解決vue 子組件修改父組件傳來的props值報(bào)錯(cuò)問題

    今天小編就為大家分享一篇解決vue 子組件修改父組件傳來的props值報(bào)錯(cuò)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • 詳解Vue中keep-alive的使用

    詳解Vue中keep-alive的使用

    keep-alive是Vue的內(nèi)置組件,當(dāng)它包裹動(dòng)態(tài)組件時(shí),會(huì)緩存不活動(dòng)的組件實(shí)例,而不是銷毀,這篇文章主要介紹了詳解Vue中keep-alive的使用,需要的朋友可以參考下
    2023-03-03
  • vue-ajax小封裝實(shí)例

    vue-ajax小封裝實(shí)例

    下面小編就為大家?guī)硪黄獀ue-ajax小封裝實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • vue-cli 引入、配置axios的方法

    vue-cli 引入、配置axios的方法

    這篇文章主要介紹了vue-cli 引入axios的方法,文中還給大家提到了vue-cli 配置axios的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2018-05-05
  • Vue實(shí)現(xiàn)一個(gè)返回頂部backToTop組件

    Vue實(shí)現(xiàn)一個(gè)返回頂部backToTop組件

    本篇文章主要介紹了Vue實(shí)現(xiàn)一個(gè)返回頂部backToTop組件,可以實(shí)現(xiàn)回到頂部效果,具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-07-07
  • Vue導(dǎo)入excel文件的兩種方式(form表單和el-upload)

    Vue導(dǎo)入excel文件的兩種方式(form表單和el-upload)

    最近開發(fā)遇到一個(gè)點(diǎn)擊導(dǎo)入按鈕讓excel文件數(shù)據(jù)導(dǎo)入的需求,下面這篇文章主要給大家介紹了關(guān)于Vue導(dǎo)入excel文件的兩種方式,分別是form表單和el-upload兩種方法,需要的朋友可以參考下
    2022-11-11
  • vue-cli3 取消eslint校驗(yàn)代碼的解決辦法

    vue-cli3 取消eslint校驗(yàn)代碼的解決辦法

    這篇文章主要介紹了vue-cli3 取消eslint校驗(yàn)代碼的解決辦法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • vue el-tree 默認(rèn)展開第一個(gè)節(jié)點(diǎn)的實(shí)現(xiàn)代碼

    vue el-tree 默認(rèn)展開第一個(gè)節(jié)點(diǎn)的實(shí)現(xiàn)代碼

    這篇文章主要介紹了vue el-tree 默認(rèn)展開第一個(gè)節(jié)點(diǎn)的實(shí)現(xiàn)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • vue router demo詳解

    vue router demo詳解

    這篇文章主要為大家詳細(xì)介紹了vue router demo演示代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10

最新評(píng)論