This article example shares the specific code of Vue to implement book management for your reference. The specific content is as follows Case Effect Case ideas 1. Book List
2. Add books
3. Modify the book
4. Delete books
5. Common feature application scenarios
Code Basic Style <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> Static layout <div id="app"> <div class='grid'> <div> <h1>Book Management</h1> <div class="book"> <div> <label for='id'> serial number: </label> <input type="text" id="id" v-model='id' :disabled='flag' v-focus> <label for="name"> name: </label> <input type="text" id='name' v-model='name'> <button @click='handle' :disabled='submitFlag'>Submit</button> </div> </div> </div> <div class='total'> <span>Total number of books:</span><span>{{total}}</span> </div> <table> <thead> <tr> <th>Number</th> <th>Name</th> <th>Time</th> <th>Operation</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)'>Edit</a> <span>|</span> <a href="" @click.prevent='deleteBook(item.id)'>Delete</a> </td> </tr> </tbody> </table> </div> </div> Effect realization <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, //month "d": date.getDate(), //day "h": date.getHours(), //hours "m": date.getMinutes(), //minutes "s": date.getSeconds(), //seconds "q": Math.floor((date.getMonth() + 3) / 3), //quarter "S": date.getMilliseconds() //milliseconds }; 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) { // Edit operation // is to update the corresponding data in the array according to the current id this.books.some((item) => { if (item.id == this.id) { item.name = this.name //Terminate the loop after completing the update operation return true; } }) this.flag = false; } else { // Add a book var book = {}; book.id = this.id; book.name = this.name; this.data = ''; this.books.push(book); } // Clear the form this.id = ''; this.name = ''; }, toEdit: function (id) { // Disable modification of id this.flag = true; // Query the data to be edited based on id var book = this.books.filter(function (item) { return item.id == id; }); console.log(book) //Submit the obtained id to the form this.id = book[0].id; this.name = book[0].name; }, deleteBook: function (id) { // Delete books // Find the index of the element from the array according to the id // var index = this.books.findIndex(function (item) { // return item.id == id; // }); // Delete array elements according to index // this.books.splice(index, 1) // ----------------- // Method 2: Delete by filter method this.books = this.books.filter(function (item) { return item.id != id; }) } }, computed: { total: function () { // Calculate the total number of books return this.books.length; } }, watch: name: function (val) { // Verify that the book name already exists var flag = this.books.some(function (item) { return item.name == val; }) if (flag) { // Book name exists this.submitFlag = true } else { // The book name does not exist this.submitFlag = false } } }, mounted: function () { // When the lifecycle hook function is triggered. The template is ready to use // Generally used to obtain background data and then fill the data into the template var data = [{ id: 1, name: 'Romance of the Three Kingdoms', date: 252597867777 }, { id: 2, name: 'Water Margin', date: 564634563453 }, { id: 3, name: 'Dream of Red Mansions', date: 345435345343 }, { id: 4, name: 'Journey to the West', date: 345345346533 }] this.books = data } }); </script> The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: CentOS7 configuration Alibaba Cloud yum source method code
>>: Linux installation MySQL5.6.24 usage instructions
Input subsystem framework The linux input subsyst...
I just started learning database operations. Toda...
In the Linux system, in addition to various accou...
This article discusses the difficulties and ideas...
1. If MySQL is not started successfully, check th...
Table of contents 1. Compiler code format specifi...
Table of contents 1. What is a closure? 1.2 Memoi...
Today, let's talk about a situation that is o...
Add a DOCTYPE to the page Since different browser...
1. Log in to MySQL database mysql -u root -p View...
Copy code The code is as follows: <html xmlns=...
Table of contents 1. Data Type 1.1 Why do we need...
1. Install mysql Run the following command to upd...
1. Introduction A few days ago, a development col...
1. MySQL rpm package installation # Download the ...