網站首頁 編程語言 正文
頁面組件拆分圖
功能點
- 實現全選反選
- 數量的增加和減少
- 選中刪除,單獨刪除
- 商品總價和商品總數量的變化
首先在根組件中把頁面的布局以及功能先實現,然后再拆分組件,最后通過組件傳值進行屬性和方法的傳遞
代碼展示
app.js組件
import axios from 'axios';
import React, { Component } from 'react';
import './App.css';
import Cartfoot from './Component/Cartfoot';
import CartList from './Component/CartList';
export default class App extends Component {
constructor() {
super()
this.state = {
list: [],
checked:false
}
this.getCart()
}
getCart = async () => {
let res = await axios.get('http://127.0.0.1:3002/getCart')
console.log(res.data.data);
let carts = res.data.data
carts.map(item => {
return item.checked = false
})
this.setState({
list: carts
})
}
// 全選
qx=(e)=>{
let {list}=this.state
list.map(item=>item.checked=e.target.checked)
this.setState({
list:list,
checked:e.target.checked
})
}
// 反選
fx=(index)=>{
let {list}=this.state
list[index].checked = !list[index].checked
this.setState({
list:list,
checked:list.every(item=>item.checked)
})
}
// 選中刪除
checkDel=()=>{
let {list}=this.state
let delAll=list.filter(item=>!item.checked)
this.setState({
list:[...delAll]
})
}
// 數量加減操作
handlerNum=(index,type="jia")=>{
let {list}=this.state
type==='jia'?list[index].num++ :list[index].num--
this.setState({
list:list
})
}
// 計算操作
count=()=>{
let total=0
let nums=0
let {list}=this.state
list.forEach(item=>{
if(item.checked){
total+=item.num*item.prize
nums+=item.num
}
})
return [total,nums]
}
// 刪除按鈕
Del=(index)=>{
let {list}=this.state
list.splice(index,1)
this.setState({
list:list
})
}
render() {
let { list ,checked} = this.state
return (
<div className='App'>
<table className='table'>
<thead>
<tr>
<th><input type="checkbox" checked={checked} onChange={this.qx}/></th>
<th>ID</th>
<th>名字</th>
<th>圖片</th>
<th>價格</th>
<th>數量</th>
<th>操作</th>
</tr>
</thead>
<CartList list={list} fx={this.fx} qx={this.qx} handlerNum={this.handlerNum} Del={this.Del} checked={checked}></CartList>
<Cartfoot count={this.count} checkDel={this.checkDel}></Cartfoot>
</table>
</div>
)
}
}
在app組件中,我們把所有的方法和請求到的數據接口寫在這里,然后再通過props屬性進行組件間的通信
CartList.js組件
import React from 'react'
import CartItem from './CartItem'
export default function CartList(props) {
return (
// <div>
<tbody>
{
props.list.map((item, index) => {
return (
<CartItem {...props} item={item} index={index} key={index}/>
)
})
}
</tbody>
// </div>
)
}
上面的{...props}是因為組件在傳遞屬性時,如果傳遞的時候是一個對象屬性,我們可以使用擴展運算符傳遞數據
Cartfoot.js組件
import React from 'react'
export default function Cartfoot(props) {
return (
<tfoot>
<tr>
<td colSpan={7}>
商品總價格:<strong>{props.count()[0]}</strong>
商品總數量:<strong>{props.count()[1]}</strong>
<button onClick={props.checkDel}>選中刪除</button>
</td>
</tr>
</tfoot>
)
}
CartItem.js組件
import React from 'react'
import CartColltract from './CartColltract'
export default function CartItem(props) {
let {item,index}=props
return (
<tr>
<td><input type="checkbox" checked={item.checked} onChange={()=>props.fx(index)}/></td>
<td>{item._id}</td>
<td>{item.name}</td>
<td><img src={item.phopo} alt="" /></td>
<td>{item.prize}</td>
<td>
<CartColltract {...props} item={item} index={index}></CartColltract>
</td>
<td><button onClick={()=>props.Del(index)}>刪除</button></td>
</tr>
)
}
CartColltract.js組件
import React from 'react'
export default function CartColltract(props) {
let {index,item}=props
return (
<div>
<button className='danger' disabled={item.num === 1} onClick={()=>props.handlerNum(index,'jian')}>-</button>
<input type="text" value={item.num} readOnly />
<button onClick={()=>props.handlerNum(index,'jia')}>+</button>
</div>
)
}
像我們上面組件那樣,組件之間層層嵌套,我們不僅可以使用普通父傳子,子傳父的組件通信方式進行組件傳值,也可以使用context兄弟組件通信
原文鏈接:https://blog.csdn.net/qq_60976312/article/details/126125371
相關推薦
- 2022-07-06 C語言超詳細講解字符串函數和內存函數_C 語言
- 2023-01-13 Android?Parcleable接口的調用源碼層分析_Android
- 2023-03-15 pandas計算相關系數corr返回空的問題解決_python
- 2022-09-02 Python?如何實時向文件寫入數據(附代碼)_python
- 2022-05-07 react中的雙向綁定你真的了解嗎_React
- 2022-02-10 g2繪制雙折線圖+柱形圖+自定義legend
- 2022-12-28 React組件實例三大核心屬性State?props?Refs詳解_React
- 2022-05-22 Nginx反向代理與負載均衡概念理解及模塊使用_nginx
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支