今天我们来做一个图书管理的项目,我们需要用到API的接口(由黑马编程提供,案例由黑马程序员设计):
目录
一.需要使用的技术文件
我们先把需要用到的API接口来书写出来以供使用:
http://www.liulongbin.top:3006/api/getbooks获取图书接口
http://www.liulongbin.top:3006/api/delbook删除图书接口
http://www.liulongbin.top:3006/api/addbook添加图书接口
使用到的插件技术:
BootStrap3的css框架
jQuery.js
需要用到的API文档:
二.代码实现(代码详解)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>图书管理</title>
<link rel="stylesheet" href="./bootstrap.min.css">
<script src="./jQuery-min.js"></script>
</head>
<body style="padding: 15px;">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">添加新图书</h3>
</div>
<div class="panel-body form-inline">
<div class="input-group">
<div class="input-group-addon">书名</div>
<input type="text" class="form-control" id="bookname" placeholder="请输入书名">
</div>
<div class="input-group">
<div class="input-group-addon">作者</div>
<input type="text" class="form-control" id="Author" placeholder="请输入作者">
</div>
<div class="input-group">
<div class="input-group-addon">出版社</div>
<input type="text" class="form-control" id="publisher" placeholder="请输入出版社">
</div>
<button type="button" class="btn btn-primary">添加</button>
</div>
</div>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>书名</th>
<th>作者</th>
<th>出版社</th>
<th>操作</th>
</tr>
</thead>
<tbody id="db">
</tbody>
</table>
<script>
$(function(){
//获取图书列表get
function getBookList(){ //封装函数getBookList(),以便后续的重新渲染
$.get('http://www.liulongbin.top:3006/api/getbooks',function(res){
// console.log(res);
var rows = []; //声明一个空数组,用来存储从API上获取的数据
if(res.status !== 200){
return alert('获取数据失败');
}else{
$.each(res.data,function(i,n){ //遍历数组获得到ID值和数据生成代码放到数组里
rows.push("<tr><td>" + n.id + "</td><td>" + n.bookname + "</td><td>" + n.author + "</td><td>" + n.publisher + "</td><td><a href='javascript:;' class='del' date-id=" + n.id + ">删除</a></td></tr>");
})
}
$("#db").empty().append(rows.join('$')); //获取到存放上面代码的父亲,清空之后,最新数据依次放到最前
})
}
getBookList(); //浏览器一打开,就调用函数渲染一边
//删除del
//代理绑定
$("tbody").on("click",".del",function(){
var id = $(this).attr("date-id"); //自定义一个ID属性,获取当前点击删除的父亲元素的索引值
// console.log(id);
$.get('http://www.liulongbin.top:3006/api/delbook',
{id:id},
function(res){
if(res.status !== 200){
return alert('删除图书失败');
}else{
getBookList();//从新渲染
}
})
})
//添加图书 post
$("button").on("click",function(){
var bookname = $("#bookname").val().trim();
var author = $("#Author").val().trim();
var publisher = $("#publisher").val().trim();
if(bookname.length <= 0 || author.length <= 0 || publisher.length <= 0){
alert('添加图书失败,请输入完整的图书信息');
}
// alert('成功');
$.post('http://www.liulongbin.top:3006/api/addbook',
{bookname : bookname,author :author,publisher :publisher},
function(res){
if(res.status !== 201){
return alert('添加图书失败');
}
getBookList();//从新渲染
$("#bookname").val("");
$("#Author").val("");
$("#publisher").val("");
})
})
})
</script>
</body>
</html>
资源由黑马编程提供
学习前端,关注小蜗