Vue實(shí)現(xiàn)圖書(shū)管理案例
本文實(shí)例為大家分享了Vue實(shí)現(xiàn)圖書(shū)管理的具體代碼,供大家參考,具體內(nèi)容如下
案例效果
案例思路
1、圖書(shū)列表
- 實(shí)現(xiàn)靜態(tài)列表效果
- 基于數(shù)據(jù)實(shí)現(xiàn)模板效果
- 處理每行的操作按鈕
2、添加圖書(shū)
- 實(shí)現(xiàn)表單的靜態(tài)效果
- 添加圖書(shū)表單域數(shù)據(jù)綁定
- 添加按鈕事件綁定
- 實(shí)現(xiàn)添加業(yè)務(wù)邏輯
3、修改圖書(shū)
- 修改信息填充到表單
- 修改后重新提交到表單
- 重用添加和修改方法
4、刪除圖書(shū)
- 刪除按鈕綁定時(shí)間處理方法
- 實(shí)現(xiàn)刪除業(yè)務(wù)邏輯
5、常用特性應(yīng)用場(chǎng)景
- 過(guò)濾器(格式化日期)
- 自定義指令(獲取表單焦點(diǎn))
- 計(jì)算屬性(統(tǒng)計(jì)圖書(shū)數(shù)量)
- 偵聽(tīng)器(驗(yàn)證圖書(shū)和編號(hào)的存在性)
- 生命周期(圖書(shū)數(shù)據(jù)處理)
代碼
基本樣式
<style type="text/css"> .grid { margin: auto; width: 550px; text-align: center; } .grid table { width: 100%; border-collapse: collapse; } .grid th, td { padding: 10; border: 1px dashed orange; height: 35px; } .grid th { background-color: orange; } .grid .book { width: 550px; padding-bottom: 10px; padding-top: 5px; background-color: lawngreen; } .grid .total { height: 30px; line-height: 30px; background-color: lawngreen; border-top: 1px solid orange; } </style>
靜態(tài)布局
<div id="app"> <div class='grid'> <div> <h1>圖書(shū)管理</h1> <div class="book"> <div> <label for='id'> 編號(hào): </label> <input type="text" id="id" v-model='id' :disabled='flag' v-focus> <label for="name"> 名稱(chēng): </label> <input type="text" id='name' v-model='name'> <button @click='handle' :disabled='submitFlag'>提交</button> </div> </div> </div> <div class='total'> <span>圖書(shū)總數(shù):</span><span>{{total}}</span> </div> <table> <thead> <tr> <th>編號(hào)</th> <th>名稱(chēng)</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> </div>
效果實(shí)現(xiàn)
<script type="text/javascript" src="../js/vue.js"></script> <script type="text/javascript"> Vue.directive('focus', { inserted: function (el) { el.focus(); } }) Vue.filter('format', function (value, arg) { function dateFormat(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 dateFormat(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ù) this.books.some((item) => { if (item.id == this.id) { item.name = this.name // 完成更新操作后終止循環(huán) return true; } }) this.flag = false; } else { // 添加圖書(shū) var book = {}; book.id = this.id; book.name = this.name; this.data = ''; this.books.push(book); } // 清空表單 this.id = ''; this.name = ''; }, toEdit: function (id) { // 禁止修改id this.flag = true; // 根據(jù)id查詢(xún)出要編輯的數(shù)據(jù) var book = this.books.filter(function (item) { return item.id == id; }); console.log(book) // 把獲取到的id提交到表單 this.id = book[0].id; this.name = book[0].name; }, deleteBook: function (id) { // 刪除圖書(shū) // 根據(jù)id從數(shù)組中查找元素的索引 // var index = this.books.findIndex(function (item) { // return item.id == id; // }); // 根據(jù)索引刪除數(shù)組元素 // this.books.splice(index, 1) // ----------------- // 方法2 通過(guò)filter方法進(jìn)行刪除 this.books = this.books.filter(function (item) { return item.id != id; }) } }, computed: { total: function () { // 計(jì)算圖書(shū)的總數(shù) return this.books.length; } }, watch: { name: function (val) { // 驗(yàn)證圖書(shū)名稱(chēng)是否已經(jīng)存在 var flag = this.books.some(function (item) { return item.name == val; }) if (flag) { // 圖書(shū)名存在 this.submitFlag = true } else { // 圖書(shū)名不存在 this.submitFlag = false } } }, mounted: function () { // 該生命周期鉤子函數(shù)被出發(fā)的時(shí)候。模板已經(jīng)可以使用 // 一般此時(shí)用于獲取后臺(tái)數(shù)據(jù),然后把數(shù)據(jù)填充到模板 var data = [{ id: 1, name: '三國(guó)演義', date: 252597867777 }, { id: 2, name: '水滸傳', date: 564634563453 }, { id: 3, name: '紅樓夢(mèng)', date: 345435345343 }, { id: 4, name: '西游記', date: 345345346533 }] this.books = data } }); </script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue+better-scroll 實(shí)現(xiàn)通訊錄字母索引的示例代碼
通訊錄字母索引是常用的一種功能,本文主要介紹了Vue+better-scroll 實(shí)現(xiàn)通訊錄字母索引,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06Vue組件全局注冊(cè)實(shí)現(xiàn)警告框的實(shí)例詳解
這篇文章主要介紹了Vue組件全局注冊(cè)實(shí)現(xiàn)警告框的實(shí)例,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06vue實(shí)現(xiàn)虛擬滾動(dòng)的示例詳解
虛擬滾動(dòng)或者移動(dòng)是指禁止原生滾動(dòng),之后通過(guò)監(jiān)聽(tīng)瀏覽器的相關(guān)事件實(shí)現(xiàn)模擬滾動(dòng),下面小編就來(lái)和大家詳細(xì)介紹一下vue實(shí)現(xiàn)虛擬滾動(dòng)的示例代碼,需要的可以參考下2023-10-10vue使用數(shù)組splice方法失效,且總刪除最后一項(xiàng)的問(wèn)題及解決
這篇文章主要介紹了vue使用數(shù)組splice方法失效,且總刪除最后一項(xiàng)的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09Vue2響應(yīng)式系統(tǒng)之set和delete
這篇文章主要介紹了Vue2響應(yīng)式系統(tǒng)之set和delete,通過(guò)為對(duì)象收集依賴(lài),將對(duì)象、數(shù)組的修改、刪除也變成響應(yīng)式的了,同時(shí)為用戶(hù)提供了和方法,下文詳細(xì)介紹需要的朋友可以參考一下2022-04-04解決vue.js出現(xiàn)Vue.js not detected錯(cuò)誤的問(wèn)題
這篇文章主要介紹了解決vue.js出現(xiàn)Vue.js not detected錯(cuò)誤的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10Vue3在css中使用v-bind綁定js/ts變量(在scss和less中使用方式)
v-bind是Vue.js中的一個(gè)核心指令,用于在Vue組件或DOM元素上綁定數(shù)據(jù)屬性,下面這篇文章主要給大家介紹了關(guān)于Vue3在css中使用v-bind綁定js/ts變量的相關(guān)資料,也可以在scss和less中使用方式,需要的朋友可以參考下2024-04-04Vue3監(jiān)聽(tīng)store中數(shù)據(jù)變化的三種方式
這篇文章給大家介紹了Vue3監(jiān)聽(tīng)store中數(shù)據(jù)變化的三種方法,使用watch和storeToRefs函數(shù),使用計(jì)算屬性computed和使用watchEffect函數(shù)這三種方法,文中通過(guò)代碼講解非常詳細(xì),需要的朋友可以參考下2024-01-01關(guān)于Element-ui中table默認(rèn)選中toggleRowSelection問(wèn)題
這篇文章主要介紹了關(guān)于Element-ui中table默認(rèn)選中toggleRowSelection問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08