基于Vue实现图书管理功能

作者:damys 时间:2024-04-27 16:16:59 

本文实例为大家分享了vue简单的图书管理具体代码,供大家参考,具体内容如下


<table class="table table-bg table-border table-bordered">
<tr>
 <th>ID</th>
 <th>书名</th>
 <th>作者</th>
 <th>价格</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>价格:<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('确认要删除?')){
   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'
  }

// 计算插入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实现图书管理功能

来源:http://blog.csdn.net/damys/article/details/72235587

标签:Vue,图书管理
0
投稿

猜你喜欢

  • Python编程基础之字典

    2021-10-02 13:34:56
  • 使用有趣的自定义标记来布局页面

    2013-11-10 19:07:48
  • python3+PyQt5实现支持多线程的页面索引器应用程序

    2022-02-17 02:02:11
  • python使用pandas处理excel文件转为csv文件的方法示例

    2021-09-13 07:15:52
  • d3.js实现简单的网络拓扑图实例代码

    2024-05-09 10:19:58
  • TensorFlow神经网络学习之张量与变量概念

    2023-07-06 20:58:02
  • 在asp中使用js的encodeURIComponent方法

    2012-11-30 20:05:53
  • SQL Server 2005恢复数据库详细图文教程

    2024-01-14 10:18:02
  • python基于TCP实现的文件下载器功能案例

    2021-12-30 05:14:26
  • JavaScript 经典实例日常收集整理(常用经典)

    2023-09-15 07:40:56
  • Go语言开发redis封装及简单使用详解

    2024-05-08 10:53:30
  • c# 获取数据库中所有表名称的方法

    2024-01-16 18:41:33
  • python3.6+selenium实现操作Frame中的页面元素

    2023-10-13 01:37:00
  • MySql数据引擎简介与选择方法

    2024-01-28 12:04:29
  • mysql 替换字段部分内容及mysql 替换函数replace()

    2024-01-23 19:25:27
  • Go语言实现遗传算法的实例代码

    2024-02-10 01:04:54
  • matplotlib共享坐标轴的实现(X或Y坐标轴)

    2023-12-01 23:58:12
  • 查看mysql当前连接数的方法详解

    2024-01-21 03:24:59
  • Python数学建模学习模拟退火算法多变量函数优化示例解析

    2021-05-07 09:36:37
  • mysql 获取时间方式

    2024-01-20 03:26:14
  • asp之家 网络编程 m.aspxhome.com