1.视图js代码

    1. $(function(){
    2. // 点击商品分类
    3. $('.sort_list').click(function(){
    4. // 商品分类左侧样式
    5. $(this).addClass('bianse');
    6. $(this).siblings().removeClass('bianse');
    7. // 获取分类的id
    8. var id = $(this).attr('cate_id');
    9. $.post("{:U('addon/WeiuidDeit/Mobile/shopAjax')}",{id:id},function(data){
    10. var str = '';
    11. for(var i = 0; i<data.data.length;i++){
    12. str += '<div class="right_total" shop_id="'+data['data'][i]['id']+'"><div class="food_detail_img"><img src="./Addons/WeiuidDeit/View/Public/Web/image/100/'+data['data'][i]['food_picture']+'" ></div><div class="food_detail_info"><h1>'+data['data'][i]['food_name']+'</h1><p>月销量:10笔</p><strong>[优惠详情]:八折优惠</strong></div><div class="food_detail_menu"><p><span>¥'+data['data'][i]['food_price']+'</span>&nbsp;/份</p>';
    13. var num = data['data'][i]['id'];
    14. if(data['cart'][num]['user_num']){
    15. str +='<div class="number"><div class="add">+</div><div class="hidebox num">'+data['cart'][num]['user_num']+'</div><div class="hidebox reduce">-</div></div>';
    16. }else{
    17. str +='<div class="number"><div class="add">+</div><div class="none hidebox num">0</div><div class="none hidebox reduce">-</div></div>';
    18. }
    19. str +='</div><div class="man_jian"><span>减</span>满20减10;满40减20</div><div class="man_zhe"><span>折</span>折扣商品三折起</div></div>';
    20. }
    21. // 商品的数据
    22. $('#food').html(str);
    23. },'json')
    24. })
    25. })

    2.视图前端代码

    1. <div class="foodsort_left" id="typelist">
    2. <!-- <div class="sort_list '+bianse+'" data-id="'+typeinfo[i].id+'">全部</div> -->
    3. <!-- 使用ajax无刷新 -->
    4. <ul>
    5. <li class="sort_listtotal sort_list '+bianse+'" data-id="'+typeinfo[i].id+'">
    6. 全部
    7. </li>
    8. <foreach name="category_data" item='v'>
    9. <li class="sort_list '+bianse+'" cate_id="{$v.id}">
    10. {$v.type_name}
    11. </li>
    12. </foreach>
    13. <div class="clearfix"></div>
    14. <ul/>
    15. </div>
    16. //html代码
    17. <!-- 循环商品列表 -->
    18. <foreach name="cart_data" item="v" key="k">
    19. <div class="right_total" shop_id="{$v.id}">
    20. <div class="food_detail_img">
    21. <img src="./Addons/WeiuidDeit/View/Public/Web/image/100/{$v.food_picture}" >
    22. </div>
    23. <div class="food_detail_info">
    24. <h1>{$v.food_name}</h1>
    25. <p>月销量:{$v.sales_count}笔</p>
    26. <strong>[优惠详情]:八折优惠</strong>
    27. </div>
    28. <div class="food_detail_menu">
    29. <p>
    30. <span id="price">¥{$v.food_price}</span>&nbsp;/份
    31. </p>
    32. <!-- 左下方购物车信息 -->
    33. <div class="number">
    34. <div class="add" style="cursor:pointer">+</div>
    35. --------------------------------------------------------------------------------
    36. <if condition="!isset($_SESSION['Cart'][$k]['user_num'])">
    37. <div class="none hidebox num" data-id="'+foodinfo[i].id+'">0</div>
    38. <div class="none hidebox reduce" style="cursor:pointer">-</div>
    39. <else />
    40. <div class=" hidebox num" data-id="'+foodinfo[i].id+'"> {$_SESSION['Cart'][$k]['user_num']}
    41. </div>
    42. <div class=" hidebox reduce" style="cursor:pointer">-</div>
    43. </if>
    44. --------------------------------------------------------------------------------
    45. </div>
    46. </div>
    47. <div class="man_jian"><span></span>满20减10;满40减20</div>
    48. <div class="man_zhe"><span></span>折扣商品三折起</div>
    49. </div>
    50. </foreach>

    3.后台控制器代码

    1. // 自取商品列表无刷新ajax
    2. public function shopAjax(){
    3. // 接收前台传送过来的分类
    4. $category_id = I('id');
    5. // 商品列表
    6. $cart = D('food');
    7. // 判断前台有没有传送过来分类的id
    8. if($category_id){
    9. // 有则根据分类id查询
    10. $condition['food_type'] = $category_id;
    11. // 商品上下加
    12. $condition['food_status'] = 1;
    13. $condition['shop_id'] = session('business_id');
    14. $cart_data = $cart->where($condition)->select();
    15. }else{
    16. // 无分类则查询所有
    17. $cart_data = $cart->where([
    18. 'shop_id'=>session('business_id')
    19. ])->select();
    20. }
    21. //在组装新数据
    22. $return['cart'] = session('Cart');
    23. //循环组装数据
    24. foreach($cart_data as $k=>$v){
    25. //判断session的id是否存在,如果不存在则组装,初始化
    26. if(!isset(session('Cart')[$v['id']])){
    27. $return['cart'][$v['id']] = array(
    28. 'id'=>'',
    29. 'user_num'=>'',
    30. 'price'=>'',
    31. 'name'=>'',
    32. 'total'=>'',
    33. );
    34. }
    35. }
    36. // 返回的是商品列表的信息
    37. $return['data'] = $cart_data;
    38. $this->ajaxReturn($return,'json');
    39. }

    4.商品表
    商品分类无刷新 - 图1