react 阶段案例-3

在这里插入图片描述

结构搭建

<style>
	 table{
		 border:1px solid #eee;
		 border-collapse: collapse; /* 合并边框 */
	 }
	 th,td{
		 border:1px solid #eee;
		 padding: 10px 16px;
		 text-align: center;
	 }
	 th{
		 background-color: #ccc;
	 }
	 .count{
		 margin:0 5px;
	 }
</style>

<script type="text/babel">
	class App extends React.Component{
		constructor(){
			super();
			this.state={
				books:[
					{
						id:1,
						name:'算法',
						date:'2006-9',
						price:85.00,
						count:1
					},
					{
						id:2,
						name:'UNIX',
						date:'2006-2',
						price:59.00,
						count:1
					},
					{
						id:3,
						name:'代码大全',
						date:'2006-3',
						price:128.00,
						count:1
					},
					{
						id:4,
						name:'编程',
						date:'2008-10',
						price:39.00,
						count:1
					}
				]
			}
		}
		render(){
			return(
				<div>
					<table>
						<thead>
							<tr>
								<th></th>
								<th>书籍名称</th>
								<th>出版日期</th>
								<th>价格</th>
								<th>购买数量</th>
								<th>操作</th>
							</tr>
						</thead>
						<tbody>
							{
								this.state.books.map((item,index)=>{
									return (
										<tr>
											<td>{index+1}</td>
											<td>{item.name}</td>
											<td>{item.date}</td>
											<td>{item.price}</td>
											<td>
												<button type="button">-</button>
												<span className="count">{item.count}</span>
												<button type="button">+</button>
											</td>
											<td>
												<button type="button">移除</button>
											</td>
										</tr>
									)
								})
							}
						</tbody>
					</table>
				</div>
			)
		 }
	}
	ReactDOM.render(<App/>,document.getElementById("app"));
 </script>

格式化价格,封装函数

在这里插入图片描述

format_utils.js

function formatPrice(price){
	if(typeof price != "number"){
		price = Number("aaa") || 0;
	}
	return "¥" + price.toFixed(2);
}

总价格显示

<h2>总价格:{formatPrice(this.getTotalPrice())}</h2>

getTotalPrice(){
	let totalPrice=0;
	for(let item of this.state.books){
		totalPrice += item.price * item.count;
	}
	return totalPrice;
}

移除按钮操作

react设计原则:state中的数据不可变性

<button type="button" onClick={e=>this.removeBook(index)}>移除</button>

removeBook(index){
	this.setState({
		books:this.state.books.filter((item,indey)=>index!=indey)
	})
}

书籍数量改变

<button type="button" disabled={item.count<=1} onClick={e=>this.changeBooksCount(index,-1)}>-</button>
<span className="count">{item.count}</span>
<button type="button" onClick={e=>this.changeBooksCount(index,1)}>+</button>

changeBooksCount(index,type){
	const newBooks=[...this.state.books]; // 不违背state不可变原则
	newBooks[index].count =  newBooks[index].count + type;
	this.setState({
		books:newBooks
	})
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值