亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:77日期:2022-10-21 17:23:44

本文實例為大家分享了Vue實現(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'>編號:</label> <input type='text' v-model=’id’ :disabled=’flag’ v-focus> <label for='name'>名稱:</label> <input type='text' 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>編號</th><th>名稱</th><th>時間</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='http://www.aoyou183.cn/bcjs/10534.html' @click.prevent=’toEdit(item.id)’>修改</a> <span>|</span> <a href='http://www.aoyou183.cn/bcjs/10534.html' @click.prevent=’deleteBook(item.id)’>刪除</a> </td> </tr> </tbody> </table></div><script src='http://www.aoyou183.cn/bcjs/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(), //小時 '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ù)當前的id去更新數(shù)組中對應(yīng)的數(shù)據(jù) //箭頭函數(shù)的this不是window //some方法判斷什么時候終止循環(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方法進行刪除 //this.books=this.books.filter(function(item){ //return item.id!=id; //}); }//deleteBook結(jié)束 }, computed:{ total:function(){ //計算圖書的總數(shù) return this.books.length; } },//computed結(jié)束 watch:{ name:function(val){ //驗證圖書名稱是否已經(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ā)的時候,模板已經(jīng)可以使用 //一般用于獲取后臺數(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)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 国产高清在线免费视频 | 青青草国产免费久久久91 | 九九九精品在线观看 | 玖玖精品在线观看 | 免费视频爱爱太爽在线观看 | 欧美视频亚洲色图 | 欧美日韩专区国产精品 | 一级做α爱过程免费视频 | 精品国产日韩亚洲一区二区 | 日韩欧美国产中文字幕 | 黄色免费观看视频 | 奇米色88欧美一区二区 | 日韩一本| 国产成人综合高清在线观看 | 免费看日韩欧美一级毛片 | 天天色综合2 | 免费精品国产 | 嫩草视频在线 | 欧美成人爽毛片在线视频 | 极品美女国产精品免费一区 | 伊人午夜 | 欧美丰满丝袜videossex | 麻豆视频在线免费观看 | 欧美一级毛片视频 | 大伊香蕉精品视频在线天堂 | 99久久精品免费看国产情侣 | 亚洲欧美在线免费观看 | 国产黄色大片网站 | 久久美女精品 | 久久久久国产午夜 | 国产福利视频一区二区 | 久久国产免费福利资源网站 | 免费啪视频一区二区三区 | 婷婷国产偷v国产偷v亚洲 | 亚洲日韩中文字幕天堂不卡 | 日韩国产中文字幕 | 拍拍拍无挡视频免费全程1000 | 在线欧美日韩国产 | 青青自拍视频一区二区三区 | 女人被男人狂躁的免费视频 | 国产资源在线观看 |