
图书管理系统
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue测试</title>
<script src="./js/vue.js"></script>
<link rel="stylesheet" href="./css/demo.css">
</head>
<body>
<div id="app">
<h1>图书管理系统</h1>
<fieldset>
<legend>新商品信息</legend>
<div class="form-group">
<label>
名称:
</label>
<input type="text" v-model="newProduct.name" />
</div>
<div class="form-group">
<label>
作者:
</label>
<input type="text" v-model="newProduct.auth" />
</div>
<div class="form-group">
<label>
单价:
</label>
<input type="number" v-model="newProduct.price" />
</div>
<div class="form-group">
<!-- <labe></label> -->
<button @click="createProd">添加</button>
</div>
</fieldset>
<div class="form-group">
<h3>搜索图书(图书名称):</h3>
<div class="search">
<input type="text" v-model="key" placeholder="请输入查询关键字..." />
</div>
</div>
<table>
<thead>
<th>名称</th>
<th>作者</th>
<th>价格</th>
<th>删除</th>
</thead>
<tbody>
<tr v-for="(prod, index) in findProdList">
<td>{{prod.name}}</td>
<td>{{prod.auth}}</td>
<td>{{prod.price|priceFormat}}</td>
<td class="text-center">
<button @click="deleteProd(prod)">删除</button>
</td>
</tr>
</tbody>
</table>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
key: "",
newProduct: {
name: "",
price: 0,
auth: ""
},
products: [
{
name: "原则",
auth: "[美]瑞达利欧/刘波、綦相/中信出版社",
price: 98,
},
{
name: "爸爸,穷爸爸",
auth: "[美]罗伯特. T .清崎、莎伦.L.莱希特/杨君,杨明/世界图书出版公司",
price: 18.8,
},
{
name: "影响力",
auth: "[美]罗伯特西奥迪尼/陈叙/中国人民大学出版社",
price: 45,
},
]
},
filters: {
// 过滤器,格式化价格
priceFormat: function (val) {
return "¥" + parseFloat(val).toFixed(2) + "元"
}
},
methods: {
// 添加图书
createProd: function () {
this.products.push(this.newProduct);
this.newProduct = {
name: "",
price: 0,
auth: ""
}
},
// 删除图书
deleteProd: function (prod) {
var index = this.products.indexOf(prod);
// console.log(index)
if (confirm("删除当前图书信息吗?")) {
this.products.splice(index, 1);
}
}
},
computed: {
findProdList: function () {
var _this = this;
return _this.products.filter(function (prod) {
return prod.name.indexOf(_this.key) != -1;
})
}
}
})
</script>
</body>
</html>
css样式:
* {
margin: 0;
padding: 0;
box-sizing: border-box
}
html {
font-size: 12px;
font-family: Ubuntu, simHei, sans-serif;
font-weight: 400
}
body {
font-size: 1rem
}
table,
td,
th {
border-collapse: collapse;
border-spacing: 0
}
table {
width: 100%
}
td,
th {
border: 1px solid #bcbcbc;
padding: 5px 10px
}
th {
background: #42b983;
font-size: 1.2rem;
font-weight: 400;
color: #fff;
cursor: pointer
}
tr:nth-of-type(odd) {
background: #fff
}
tr:nth-of-type(even) {
background: #eee
}
fieldset {
margin-top: 20px;
border: 1px solid #BCBCBC;
padding: 15px;
}
input {
outline: none
}
input[type=text] {
border: 1px solid #ccc;
padding: .5rem .3rem;
}
input[type=text]:focus {
border-color: #42b983;
}
button {
outline: none;
padding: 5px 8px;
color: #fff;
border: 1px solid #BCBCBC;
border-radius: 3px;
background-color: #009A61;
cursor: pointer;
}
button:hover {
opacity: 0.8;
}
#app {
margin: 0 auto;
max-width: 640px
}
.form-group {
text-align: center;
margin: 10px;
}
.form-group>label {
display: inline-block;
/* width: 10rem; */
text-align: right;
}
.form-group>input,
.form-group>select{
display: inline-block;
height: 2.5rem;
line-height: 2.5rem;
}
.form-group>button{
display: block;
width: 80px;
height: 30px;
margin: 10px auto;
border-radius: 10px;
}
.text-center {
text-align: center;
}
.search input{
/* border: 1px solid black; */
width: 100%;
margin: 10px;
}
.form-group h3{
text-align: left;
}