溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

基于Vue實(shí)現(xiàn)圖書管理功能

發(fā)布時(shí)間:2020-09-14 21:15:04 來源:腳本之家 閱讀:140 作者:damys 欄目:web開發(fā)

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

<table class="table table-bg table-border table-bordered">
 <tr>
  <th>ID</th>
  <th>書名</th>
  <th>作者</th>
  <th>價(jià)格</th>
  <th>操作</th>
 </tr>
 <tr v-for="(book,index) in books">
  <td>{{book.id}}</td>
  <td>{{book.name}}</td>
  <td>{{book.author}}</td>
  <td>{{book.price}}</td>
  <td>
   <button class="btn" @click="delBook(index)">刪除</button>
  </td>
 </tr>
</table>

<fieldset>
 <legend>添加新書</legend>
 <p>書名:<input type="text" v-model="newBook.name"></p>
 <p>作者:<input type="text" v-model="newBook.author"></p>
 <p>價(jià)格:<input type="text" v-model="newBook.price"></p>
 <p><button class="btn" @click="addBook">添加</button></p>
</fieldset>

<script>
new Vue({
 el:'#books',
 data:{
  books:[
   {id:1, name:'紅樓夢', author:'曹雪芹', price:'1'},
   {id:2, name:'西游記', author:'吳承恩', price:'2'},
   {id:3, name:'水滸傳', author:'施耐庵', price:'3'}
  ],
  newBook:{
   id:0,
   name:'',
   author:'',
   price:''
  }
 },
 methods:{
  delBook:function(idx){
   if(window.confirm('確認(rèn)要?jiǎng)h除?')){
    this.books.splice(idx, 1);
   }

  },
  addBook:function(){
   // 約束
   if(this.newBook.name.length == 0) {
    alert('書名不能為空');
    return;
   } 

   if(this.newBook.author.length == 0){
    alert('書的作者不能為空');
    return;
   }

   if(this.newBook.price.length == 0){
    this.newBook.price = '0'
   } 

   // 計(jì)算插入id
   var maxId = 0;
   for(var i=0; i<this.books.length; i++){
    if(maxId<this.books[i].id){
     maxId = this.books[i].id;
    }
   }
   this.newBook.id = maxId+1;

   // 插入到 books中
   this.books.push(this.newBook);

   // 清空新書
   this.newBook = {};
  }
 }
});
</script>

效果圖:

基于Vue實(shí)現(xiàn)圖書管理功能

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

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI