接上一篇学习日记,今天做了些页面的其它一些操作,比如说添加了库存的选项并设置可以自由增删库存货物数量;增加了日期选项和删除货物按钮;以及增加了选项卡删除的操作,具体的代码附上,不好理解的地方有注释。加油,今天也是进步的一天~
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/60e8fd537306694be2f438e788f69f14.png)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>商品列表</title>
<script type="text/javascript" src="../js/vue.js" ></script>
<script>
window.onload = () =>{
new Vue({
el:'.container',
data:{
imgUrl:'../res/images/',
imgName:'logo.jpg',
colNum:8,
goods:{
id:'',
name:'',
price:'',
stock:'',
type:'',
addDate:''
},
goodsType:['日用百货','文具图书','服装'],
goodsArray:[
{id:'001',name:'蚊香',price:4.5,stock:300,type:'日用百货',addDate:'2020-03-05'},
{id:'002',name:'演员的自我修养',price:47.9,stock:180,type:'文具图书',addDate:'2020-05-19'},
{id:'003',name:'沙滩裤',price:59,stock:100,type:'服装',addDate:'2020-07-13'},
],
delArray:[]
},
methods:{
addGoods(){
this.goodsArray.push(this.goods);
this.goods = {};
var d = new Date();
var y = d.getFullYear();
var m = d.getMonth() + 1;
var day = d.getDate() < 10 ? '0' + d.getDate():d.getDate();
var myDate = y + '-' + m + '-' +day;
this.goods.addDate = myDate;
},
delGoods(index){
this.goodsArray.splice(index,1);
},
clearGoodsArray(){
this.goodsArray = [];
},
delSelected(){
this.delArray.sort((a,b)=>{
return a - b;
});
for(var i=0;i<this.delArray.length;i++){
this.goodsArray.splice(this.delArray[i]-i,1);
}
this.delArray = [];
}
}
});
}
</script>
<style type="text/css">
.container{
margin: 0 auto;
text-align: center;
width: 1000px;
border: 5px solid gray;
}
.logo{
position: relative;
top: 10px;
}
.header{
border: 1px solid gray;
margin: 20px;
}
.header .title{
color.rgb(55,75,95);
background:rgb(65,180,130);
}
.form-warp{
border: 1px solid gray;
margin: 20px;
padding-bottom: 10px;
}
.form-warp .content{
line-height: 35px;
}
.form-warp input{
width: 150px;
height: 18px;
}
.form-warp select{
width: 154px;
height: 24px;
}
.sub-title{
color.rgb(142,5,1);
background:rgb(124,211,122);
}
.table-warp{
border: 1px solid gray;
margin: 20px;
padding-bottom: 10px;
}
.table-warp th{
width: 80px;
background:rgb(124,211,122);
}
.table-warp a{
text-decoration: none;
}
.clear-btn{
text-align: right;
padding-right: 10px;
}
.fontColor{
color:gray;
text-align: center;
}
.myBackgroundColor{
background:rgb(65,180,130);
}
.myFontSize{
font-size: 200px;
}
</style>
</head>
<body>
<!--容器内放三部分-->
<div class="container">
<!--包括标题、logo-->
<div class="header">
<!--src路径是url+name-->
<img :src="imgUrl+imgName" class="logo" height="80px" />
<h1 class="title">商品管理v1.0</h1>
</div>
<!--输入部分,也包含三个部分,商品的全属性,操作按钮-->
<div class="form-warp">
<h2 class="sub-title">添加商品</h2>
<div class="content">
商品编号:<input type="text" placeholder="这里填写商品的编号" v-model="goods.id" /><br />
商品名称:<input type="text" placeholder="这里填写商品的名称" v-model="goods.name" /><br />
商品价格:<input type="text" placeholder="这里填写商品的价格" v-model="goods.price" /><br />
商品库存:<input type="text" placeholder="这里填写商品的库存" v-model="goods.stock" /><br />
商品种类:<select v-model="goods.type">
<option value="">--选择商品类型--</option>
<option v-for="type in goodsType">{{type}}</option>
</select>
</div>
<div class="form-btn">
<button @click="addGoods">确认添加</button>
<button @click="goods = {}">重置</button>
</div>
</div>
<!--显示部分-->
<div class="table-warp">
<h2 class="sub-title">商品列表</h2>
<div class="content">
<table border="1" align="center" >
<tr>
<th>序号</th>
<th>编号</th>
<th>名称</th>
<th>价格</th>
<th>库存</th>
<th>种类</th>
<th style="width: 100px;">日期</th>
<th>删除</th>
<th>选择</th>
</tr>
<tr>
<td :colspan="colNum" height="150px" v-show="goodsArray.length <= 0">暂无商品</td>
</tr>
<tr v-for="(item,index) in goodsArray" :key="item.id">
<td>{{index}}</td>
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td>{{item.price}}</td>
<td style="display: flex;">
<a style="flex: 0.5;" href="#" @click.prevent="item.stock = item.stock-- <= 0 ? 0:item.stock--">-</a>
{{item.stock}}
<a style="flex: 0.5;" href="#" @click.prevent="item.stock++">+</a>
</td>
<td>{{item.type}}</td>
<td>{{item.addDate}}</td>
<td>
<button @click="delGoods(index)">删除</button>
</td>
<td>
<input type="checkbox" :value="index" v-model="delArray" />
</td>
</tr>
</table>
</div>
<div class="clear-btn">
<a href="#" @click.prevent="delSelected" v-show="delArray.length > 0">删除选中商品</a>
<a href="#" @click.prevent="clearGoodsArray" v-show="goodsArray.length > 0">清空全部商品</a>
</div>
</div>
</div>
</body>
</html>