有关购物车商品加减的小逻辑

人生的第一个笔记

原生小程序的购物车页面,注意,这里的购物车用的是自己的逻辑,而并非全部都是请求接口,首先,我们用了一个最简单粗暴的方法,商品详情页跳转购物车页面的时候,直接将当前的商品存到缓存里面,也就是storage里面,然后在购物车页面获取,渲染页面就可以了,当然,这些都不是问题,主要的问题在哪儿,如果用户点了两次当前商品的话,总不可能购物车里面出现相同的商品吧,所以,就用到了原生JS。

这是data里面定义的空数组

goodsList:[],//当前从缓存里面获取到的商品
ids:[],//当前商品的各个独有的id
item_list:[],//当前的商品
num_list:[],//当前商品是否为重复
check_list:[] // 当前商品的选中状态

=页面刚进入就渲染
onLoad: function (options) {
let goodList = wx.getStorageSync(‘SHOPLIST’)
, ids = []
, num_list = []
, item_list = []
, check_list = []
//循环遍历这个数组,拿到里面的每一项
goodList.forEach(item => {
//indexOf() 方法返回在数组中可以找到一个给定元素的第一个索引࿰

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现简单购物车加减商品结算,可以按照以下步骤进行: 1. 创建一个商品类,包含商品名称、商品价格、商品数量等属性。 ``` public class Goods { private String name; private double price; private int quantity; public Goods(String name, double price, int quantity) { this.name = name; this.price = price; this.quantity = quantity; } // getter和setter方法 } ``` 2. 在Activity中创建一个列表,用于展示购物车中的商品。 ``` List<Goods> goodsList = new ArrayList<>(); ``` 3. 创建一个适配器,将商品列表展示到RecyclerView中。 ``` public class GoodsAdapter extends RecyclerView.Adapter<GoodsAdapter.ViewHolder> { private List<Goods> mGoodsList; static class ViewHolder extends RecyclerView.ViewHolder { TextView goodsName; TextView goodsPrice; TextView goodsQuantity; public ViewHolder(View view) { super(view); goodsName = view.findViewById(R.id.goods_name); goodsPrice = view.findViewById(R.id.goods_price); goodsQuantity = view.findViewById(R.id.goods_quantity); } } public GoodsAdapter(List<Goods> goodsList) { mGoodsList = goodsList; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.goods_item, parent, false); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { Goods goods = mGoodsList.get(position); holder.goodsName.setText(goods.getName()); holder.goodsPrice.setText(String.valueOf(goods.getPrice())); holder.goodsQuantity.setText(String.valueOf(goods.getQuantity())); } @Override public int getItemCount() { return mGoodsList.size(); } } ``` 4. 在布局文件中添加RecyclerView和结算按钮。 ``` <androidx.recyclerview.widget.RecyclerView android:id="@+id/goods_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" /> <Button android:id="@+id/checkout_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dp" android:text="结算" /> ``` 5. 在Activity中初始化RecyclerView和适配器。 ``` RecyclerView recyclerView = findViewById(R.id.goods_list); LinearLayoutManager layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); GoodsAdapter adapter = new GoodsAdapter(goodsList); recyclerView.setAdapter(adapter); ``` 6. 添加添加商品和删除商品的功能。 ``` // 添加商品 goodsList.add(new Goods("商品1", 10.0, 1)); adapter.notifyDataSetChanged(); // 删除商品 goodsList.remove(position); adapter.notifyItemRemoved(position); ``` 7. 添加加减商品数量的功能。 ``` // 商品数量加1 goods.setQuantity(goods.getQuantity() + 1); adapter.notifyItemChanged(position); // 商品数量减1 if (goods.getQuantity() > 0) { goods.setQuantity(goods.getQuantity() - 1); adapter.notifyItemChanged(position); } ``` 8. 添加结算功能。 ``` double totalPrice = 0; for (Goods goods : goodsList) { totalPrice += goods.getPrice() * goods.getQuantity(); } Toast.makeText(this, "总价为:" + totalPrice, Toast.LENGTH_SHORT).show(); ``` 这样,一个简单的购物车加减商品结算就实现了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值