利用Vue制作一个商品管理页面(第二部分,小完结)

接上一篇学习日记,今天做了些页面的其它一些操作,比如说添加了库存的选项并设置可以自由增删库存货物数量;增加了日期选项和删除货物按钮;以及增加了选项卡删除的操作,具体的代码附上,不好理解的地方有注释。加油,今天也是进步的一天~

在这里插入图片描述

<!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:''     //存库日期   2020-10-20
						},
						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>

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值