1.视图js代码
$(function(){
// 点击商品分类
$('.sort_list').click(function(){
// 商品分类左侧样式
$(this).addClass('bianse');
$(this).siblings().removeClass('bianse');
// 获取分类的id
var id = $(this).attr('cate_id');
$.post("{:U('addon/WeiuidDeit/Mobile/shopAjax')}",{id:id},function(data){
var str = '';
for(var i = 0; i<data.data.length;i++){
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> /份</p>';
var num = data['data'][i]['id'];
if(data['cart'][num]['user_num']){
str +='<div class="number"><div class="add">+</div><div class="hidebox num">'+data['cart'][num]['user_num']+'</div><div class="hidebox reduce">-</div></div>';
}else{
str +='<div class="number"><div class="add">+</div><div class="none hidebox num">0</div><div class="none hidebox reduce">-</div></div>';
}
str +='</div><div class="man_jian"><span>减</span>满20减10;满40减20</div><div class="man_zhe"><span>折</span>折扣商品三折起</div></div>';
}
// 商品的数据
$('#food').html(str);
},'json')
})
})
2.视图前端代码
<div class="foodsort_left" id="typelist">
<!-- <div class="sort_list '+bianse+'" data-id="'+typeinfo[i].id+'">全部</div> -->
<!-- 使用ajax无刷新 -->
<ul>
<li class="sort_listtotal sort_list '+bianse+'" data-id="'+typeinfo[i].id+'">
全部
</li>
<foreach name="category_data" item='v'>
<li class="sort_list '+bianse+'" cate_id="{$v.id}">
{$v.type_name}
</li>
</foreach>
<div class="clearfix"></div>
<ul/>
</div>
//html代码
<!-- 循环商品列表 -->
<foreach name="cart_data" item="v" key="k">
<div class="right_total" shop_id="{$v.id}">
<div class="food_detail_img">
<img src="./Addons/WeiuidDeit/View/Public/Web/image/100/{$v.food_picture}" >
</div>
<div class="food_detail_info">
<h1>{$v.food_name}</h1>
<p>月销量:{$v.sales_count}笔</p>
<strong>[优惠详情]:八折优惠</strong>
</div>
<div class="food_detail_menu">
<p>
<span id="price">¥{$v.food_price}</span> /份
</p>
<!-- 左下方购物车信息 -->
<div class="number">
<div class="add" style="cursor:pointer">+</div>
--------------------------------------------------------------------------------
<if condition="!isset($_SESSION['Cart'][$k]['user_num'])">
<div class="none hidebox num" data-id="'+foodinfo[i].id+'">0</div>
<div class="none hidebox reduce" style="cursor:pointer">-</div>
<else />
<div class=" hidebox num" data-id="'+foodinfo[i].id+'"> {$_SESSION['Cart'][$k]['user_num']}
</div>
<div class=" hidebox reduce" style="cursor:pointer">-</div>
</if>
--------------------------------------------------------------------------------
</div>
</div>
<div class="man_jian"><span>减</span>满20减10;满40减20</div>
<div class="man_zhe"><span>折</span>折扣商品三折起</div>
</div>
</foreach>
3.后台控制器代码
// 自取商品列表无刷新ajax
public function shopAjax(){
// 接收前台传送过来的分类
$category_id = I('id');
// 商品列表
$cart = D('food');
// 判断前台有没有传送过来分类的id
if($category_id){
// 有则根据分类id查询
$condition['food_type'] = $category_id;
// 商品上下加
$condition['food_status'] = 1;
$condition['shop_id'] = session('business_id');
$cart_data = $cart->where($condition)->select();
}else{
// 无分类则查询所有
$cart_data = $cart->where([
'shop_id'=>session('business_id')
])->select();
}
//在组装新数据
$return['cart'] = session('Cart');
//循环组装数据
foreach($cart_data as $k=>$v){
//判断session的id是否存在,如果不存在则组装,初始化
if(!isset(session('Cart')[$v['id']])){
$return['cart'][$v['id']] = array(
'id'=>'',
'user_num'=>'',
'price'=>'',
'name'=>'',
'total'=>'',
);
}
}
// 返回的是商品列表的信息
$return['data'] = $cart_data;
$this->ajaxReturn($return,'json');
}
4.商品表