React == 实现简易购物车

摘要:
all//onchange事件发生,就是对当前的状态进行取反for{list[i].checked=all//全选框的状态直接影响所有的单选框,就把全选框的状态赋给所有的单选框即可}this.setStatethis.SumPrice()}4、购物车数量加减的实现1)数量增加Add重要的还是传递对应的index,才能准确地知道操作的是哪个购物车条目list[index].count++2)数量减少Minus还有进行一步判断,当此时购物车的数量已经是1的时候,不允许再继续减了list[index].count--list[index].count˂1?

React == 实现简易版购物车

1、几个要点:

为了方便后面使用input type = "checkbox" 实现复选框的选中/不选中,给传递过来的属性要在遍历的时候,单独加上一个新属性 checked

count 属性 默认值 都是1.

state = {

all : false,
        sumprice :0,
        one : false,
        sumcount:0

}

state对象 :

  •   all -----> 用来定义全选按钮
  •   sumprice -----> 用来定义总价
  • one -----> 用来控制 结算按钮的样式(当选中的其中任何一条购物车条目时候,显示橘色,当没有任何一条选中,显示灰色)
  • sumcount ----> 用来显示购物车的总数量,显示在页面中

2、单选框的实现

1)首先是渲染的都是同样的样式,所以在这里传递一个index特别关键,通过index的传递才能够知道操作的是哪条

2)当onchange事件发生的时候,对当前checked属性进行取反。list[index].checked = ! list[index].checked。

3)单选框决定全选框:使用了数组的every方法(只有数组的每一项都满足条件,才会返回true),用所有datalist的单选框都是true的时候,全选按钮才会为true

4)单选框的选中与否决定结算框的样式:one : list[index].checked // 为true的时候,one : true

<inputtype="checkbox"className={style.checkbtn+''+style.UnChecked}ref="mytext"
onChange={()=>{this.handleChange(index)}}checked={item.checked}value=""
/>
 
//单选 handleChange(index){       var list = [...this.state.datalist] list[index].checked = !list[index].checked // every方法 var every=list.every((item,index)=>{ return item.checked==true; }) //单选框中如果有一个是 checked的是true就可以 var some = list.some((item,index)=>{ returnlist[index].checked }) this.setState({ datalist :list, all : every, //全选按钮,只有当所有的list[index]=== true 的时候才会返回true one : some //设定结算框的样式是哪个,根据list[index].checked }) this.SumPrice() }

3、全选按钮的实现

1)当点击全选框,对全选框的状态进行取反

2)点击全选按钮的时候,所有的单选框的为true / false 直接取决的 全选框按钮当前的状态true / false

遍历所有的list[i].checked = all , 把全选框的状态(true/false)直接赋值给所有的list[i].checked 。

3)当全选的时候,结算框的样式直接会跟随变动,当为false,即没有一个购物车条目呗选中,此时结算框的状态为灰色。当为true,结算框为橘色。

 <input type="checkbox" onChange={()=>{this.handleAll()}} checked={this.state.all} value=""/>

 //全选
handleAll(){
        var list = [...this.state.datalist]
        var all = this.state.all
        all = !all //onchange事件发生,就是对当前的状态进行取反
        for(var i = 0 ; i < list.length ;i++){
            list[i].checked =all // 全选框的状态直接影响所有的单选框,就把全选框的状态赋给所有的单选框即可
        }      

        this.setState({
            all : all,
            one : all //全选的状态直接影响结算框的样式
})
        this.SumPrice()
    }

4、购物车数量加减的实现

1)数量增加Add

重要的还是传递对应的index,才能准确地知道操作的是哪个购物车条目

list[index].count++

2)数量减少Minus

还有进行一步判断,当此时购物车的数量已经是1的时候,不允许再继续减了

list[index].count--

list[index].count<1?1:list[index].count

<button className={style.minus} onClick={()=>{this.handleMinus(index)}}>-</button>
<input type="text" value={this.state.datalist[index].count||''}/>
<button className={style.add} onClick={()=>{this.handleAdd(index)}}>+</button>


//
handleAdd(index){
        //设定的value= {this.state.datalist[index].count}
        var list = [...this.state.datalist]
        list[index].count++;
        
        this.setState({
            datalist : list,
        })
       
        this.SumPrice()
       
    }

//
handleMinus(index){
        //设定的value= {this.state.datalist[index].count}
        var list = [...this.state.datalist];
        list[index].count--list[index].count=list[index].count<1?1:list[index].count; 
        this.setState({
            datalist : list
        })

        this.SumPrice()
    }

5、总价的实现

1)遍历所有的datalist,只有当其中每一个checked属性为true的时候(表明已经被勾选上了,此时可以计算),才去计算金额

2)得到所有的总价,还能得到当前选中的数量一共有多少

SumPrice(){
        var sum=0
        var count = 0;
        var list = [...this.state.datalist]
        for(var i =0; i< list.length ;i++){
            if(list[i].checked=== true){
                sum += list[i].newprice *list[i].count
                count +=list[i].count
            }
        }
        
        this.setState({
            sumprice : sum,
            sumcount : count //结算个数
})
    }

6、当进行 数量 的增加、减少、单选按钮、全选按钮的时候 都要重新调用计算总价的函数。

============================================================================= 

完整代码:

JS >

class CartPage extends Component {
    state ={
        datalist:[
            {
                "imgUrl":"https://wochu.oss-cn-hangzhou.aliyuncs.com/upload/c8db2f99-d79e-4c4a-97e8-3e95c67a3b2e.jpg",
                "name": "小青菜350g",
                "newprice" : "4.5",
                "oldprice" : "4.9",
                "checked" :false,
                "count" :1},
        
            {
                "imgUrl":"https://img.wochu.cn/upload/abbc6852-711f-4d09-8e61-216c13505ccd.jpg",
                "name": "洪湖渔家香辣大闸蟹500g",
                "newprice" : "15.9",
                "oldprice" : "39.9",
                "checked" :false,
                "count" :1
            },
            {
                "imgUrl":"https://wochu.oss-cn-hangzhou.aliyuncs.com/upload/c8db2f99-d79e-4c4a-97e8-3e95c67a3b2e.jpg",
                "name": "小青菜350g",
                "newprice" : "4.5",
                "oldprice" : "4.9",
                "checked" :false,
                "count" :1
            },
        ],
        all : false,
        sumprice :0,
        one : false,
        sumcount:0}

    render() {
        return(
            <div className={style.cartList}>
                <div className={style.cartListItem}>
                    <ul className={style.shopList} ref="myul">{/*对应的每个购物车条目 */}
                        {
                            this.state.datalist.map((item,index)=>
                                
                            <li key={index}> 
                            <input type="checkbox" className={style.checkbtn+' '+style.UnChecked}  ref="mytext"onChange={()=>{this.handleChange(index)}} checked={item.checked} value=""
                            />
                            <div className={style.shopImg}>{/*点击图片跳转到页面详情 */}
                                <div className={style.shopImgShow}>
                                    <img src={item.imgUrl} alt=""/>
                                </div>
                            </div>
                            {/*商品详情 */}
                            <div className={style.shopInfo}>
                                <div className={style.shopTitle}>{item.name}</div>
                                <div className={style.shopCoupen}></div>
                                <div className={style.shopPrice}>
                                    <div className={style.price}>
                                        <span>¥{item.newprice}</span>
                                        <i>¥{item.oldprice}</i>
                                    </div>
                                    <div className={style.shopSelect}>
                                        <button className={style.minus} onClick={()=>{this.handleMinus(index)}}>-</button>
                                        <input type="text" value={this.state.datalist[index].count||''}/>
                                        <button className={style.add} onClick={()=>{this.handleAdd(index)}}>+</button>
                                    </div>
                                </div>
                            </div>
                        </li>
)
                        }
                        
                    </ul>
                </div>

                <div className={style.sum}>
                    <input type="checkbox" onChange={()=>{this.handleAll()}} checked={this.state.all} value=""/>
                    <div className={style.checkPrice}>{/*合算 */}
                        <div className={style.totalPrice}>
                            <span className={style.allsum}>合计</span>
                            <span>¥{this.state.sumprice}</span>
                        </div>
                        {/*不含运费 */}
                        <div className={style.fee}>(不含运费)</div>
                    </div>
{/*结算按钮 */}
                        <div className={this.state.one?style.btnCheck:style.btnNoCheck}>结算
                        <span>({this.state.sumcount})</span>
                        </div>
                </div>
               
            </div>
);
    }


    //单选
handleChange(index){

        var list = [...this.state.datalist]
        list[index].checked = !list[index].checked
        
        
        var every=list.every((item,index)=>{
        return item.checked==true;
        })

        //单选框中如果有一个是 checked的是true就可以
        var some = list.some((item,index)=>{
            returnlist[index].checked
        })
        
       
        this.setState({
            datalist :list,
            all : every,
            one : some  //设定结算框的样式是哪个,根据list[index].checked
})
        
        this.SumPrice()

    }

    //全选
handleAll(){
        var list = [...this.state.datalist]
        var all = this.state.all
        all = !all
        for(var i = 0 ; i < list.length ;i++){
            list[i].checked =all
        }      

        this.setState({
            all : all,
            one : all //全选的状态直接影响结算框的样式
})
        this.SumPrice()
    }


    handleAdd(index){
        //设定的value= {this.state.datalist[index].count}
        var list = [...this.state.datalist]
        list[index].count++;
        
        this.setState({
            datalist : list,
        })
       
        this.SumPrice()
       
    }

    handleMinus(index){
        //设定的value= {this.state.datalist[index].count}
        var list = [...this.state.datalist];
        list[index].count--list[index].count=list[index].count<1?1:list[index].count; 
        this.setState({
            datalist : list
        })

        this.SumPrice()
    }

    SumPrice(){
        var sum=0
        var count = 0;
        var list = [...this.state.datalist]
        for(var i =0; i< list.length ;i++){
            if(list[i].checked=== true){
                sum += list[i].newprice *list[i].count
                count +=list[i].count
            }
        }
        
        this.setState({
            sumprice : sum,
            sumcount : count //结算个数
})
    }


    
    
    
}


export default CartPage;

CSS >

.cartList{background:#f4f5f7;width:100%;top:.99rem;.cartListItem{
        width:100%;background:#fff;margin-bottom:.04rem;
        // 购物车列表
        .shopList{
            width:100%;// height:1.11rem;padding:0 .09rem;background:#fff;li{
                width:100%;height:1.11rem;border-bottom:1px solid #e6e6e6;background:#fff;// 选中按钮
                .checkbtn{
                    width:.17rem;height:1.11rem;float:left;
                }
                // 选中时候的类名
                .UnChecked{background:url("../../../image/cart-img/unselect.png") no-repeat;background-size:100% .25rem;
                }
                // 点击图片跳转
                .shopImg{width:1.1rem;height:1.1rem;margin:0 .1rem;float:left;.shopImgShow{
                        width:1.1rem;height:1.1rem;img{
                            width:100%;
                        }}
                }
                // 购物车商品详情
                .shopInfo{width:2.08rem;height:1.1rem;padding:.1rem 0;float:left;.shopTitle{
                        width:100%;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;height:.3rem;font-size:.14rem;
                    }.shopCoupen{width:2.08rem;height:.12rem;margin:.1rem 0;
                    }.shopPrice{width:2.08rem;height:.25rem;//价格
                        .price{
                            width:1.08rem;height:.21rem;float:left;span{
                                font-size:.15rem;color:#f96d16;
                            }i{font-size:.13rem;color:#999;text-decoration:line-through;font-style:normal;
                            }}

                        // 按钮
                        .shopSelect{float:right;width:.775rem;height:.25rem;
                            .minus{
                                float:left;width:.225rem;height:.25rem;border:0;
                            }input{float:left;width:.325rem;height:.25rem;text-align:center;border:0;
                            }.add{float:left;width:.225rem;height:.25rem;border:0;
                            }}
                    }
                }
            }
        }
    }

    
    // 合算
div.sum{width:100%;height:.5rem;background:#fff;padding-left:.1rem;position:fixed;bottom:.5rem;left:0;
    input{
        height:100%;float:left;
    }.checkPrice{float:left;width:1.48rem;height:.41rem;line-height:.41rem;padding:.08rem 0;margin-left:.1rem;// 合计
        .totalPrice{
            float:left;width:.869rem;height:.36rem;line-height:.36rem;font-size:.16rem;color:#f96d16;.allsum{
                font-size:.13rem;color:#333;
            }}

        // 不含运费
        .fee{float:left;width:.61rem;font-size:.13rem;color:#999;
        }}

    // 结算按钮
    .btnCheck{width:1.15rem;height:.49rem;background:rgb(249, 109, 22);float:right;line-height:.49rem;font-size:.18rem;color:#fff;text-align:center;
    }.btnNoCheck{width:1.15rem;height:.49rem;background:rgb(153, 153, 153);float:right;line-height:.49rem;font-size:.18rem;color:#fff;text-align:center;
    }}

}

免责声明:文章转载自《React == 实现简易购物车》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇“渴了么”软件需求规格说明书Debian普通用户添加sudo权限下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

内存溢出的定位与分析

概述  内存溢出在实际的生产环境中经常会遇到,比如:不断的将数据写入到一个集合中,出现了死循环,读取超大的文件等等,都可能会造成内存溢出;   如果出现了内存溢出,首先我们需要定位到发生内存溢出的环节,并且进行分析,是正还是非正常情况,如果是正常的需求,就应该考虑加大内存的设置,如果是非正常需求,那么就要对代码进行修改,修改这个bug;   首先,我们要先...

vue实现CheckBox与数组对象绑定

实现需求: 实现一个简易的购物车,页面的表格展示data数据中的一个数组对象,并提供选中商品和全选商品checkbox复选框,页面实时显示选中商品的总金额: 分析: 1:使用v-for循环渲染arraylist对象; 2:使用computed计算属性计算总价; 3:使用computed计算全选复选框是否应该被选中(商品列表如果都被勾选,则设置全选复选框的...

比较全面的python类型转换

 前言提示: int(整形):a=1 str(字符串):a="asdsdf地方123" float(浮点):a="3.14150" bytes(字节包):a=b'xe2x82xac20' complex(复数):a=(34567+0j) list(列表):a=[1, 2, 3, '4', '五', 'liu'] set(集合):a={1,2,2,3,"4"...

SpringBoot + MyBatis(注解版),常用的SQL方法

一、新建项目及配置 1.1 新建一个SpringBoot项目,并在pom.xml下加入以下代码   <dependency>    <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starte...

react-native-baidu-map

1.新建一个RN项目:(BaiDuMapTest) 出现以下界面,就表示新建项目成功了 2.安装react-native-baidu-map(注意:一定要在项目根目录下进行安装): 在终端输入:npm install react-native-baidu-map --save 3.Xcode配置: Build Phases->Lin...

36种免费React模板和主题「干货」

前言 在Internet上搜索模板和主题时,很难找到免费的React资源。 即使您不在乎质量,它们似乎也很难被发现,并出于好奇而感动,我花了数小时在Google和Github上四处挖掘,结果得到了36个免费的React模板和主题的集合。我不会对此打赌,但是它们也是高质量的资源。 因此,在此列表中,您将找到各种各样的模板和主题,可以构建几乎可以想象的任何东西...