老师:店铺列表开发时,点击二级店铺类别显示的结果和一级店铺类别一样

来源:9-7 店铺列表页前端的开发下

philip_I

2018-11-29

前端结果:图片描述
代码:
shoplist.js:
$(function() {
var loading = false;
// 分页允许返回的最大条数,超过此数值,禁止访问后台
var maxItems = 999;
// 一页返回的最大条数
var pageSize = 3;
var listUrl = ‘/o2o/fronted/listshops’;
var searchDivUrl = ‘/o2o/fronted/listshopspageinfo’;
// 页码
var pageNum = 1;
var parentId = getQueryString(‘parentId’);
var areaId = ‘’;
var shopCategoryId = ‘’;
var shopName = ‘’;

// 加载店铺列表以及区域列表 
getSearchDivData();
// 预先加载pageSize *pageNum 条
addItems(pageSize, pageNum);

function getSearchDivData() {
    var url = searchDivUrl + '?' + 'parentId=' + parentId;
    $.getJSON(url,
            function(data) {
                if (data.success) {
                    var shopCategoryList = data.shopCategoryList;
                    var html = '';
                    html += '<a href="#" class="button" data-category-id=""> 全部类别  </a>';
                    shopCategoryList.map(function(item, index) {
                                html += '<a href="#" class="button" data-category-id='
                                        + item.shopCategoryId
                                        + '>'
                                        + item.shopCategoryName
                                        + '</a>';
                            });
                    $('#shoplist-search-div').html(html);
                    var selectOptions = '<option value="">全部街道</option>';
                    var areaList = data.areaList;
                    areaList.map(function(item, index) {
                        selectOptions += '<option value="'
                                + item.areaId + '">'
                                + item.areaName + '</option>';
                    });
                    $('#area-search').html(selectOptions);
                }
            });
}
function addItems(pageSize, pageIndex) {
    // 生成新条目的HTML
    var url = listUrl + '?' + 'pageIndex=' + pageIndex + '&pageSize='
            + pageSize + '&parentId=' + parentId + '&areaId=' + areaId
            + '&shopCategoryId=' + shopCategoryId + '&shopName=' + shopName;
    loading = true;
    alert("url:"+url);
    $.getJSON(url, function(data) {
        if (data.success) {
            maxItems = data.count;
            var html = '';
            data.shopList.map(function(item, index) {
                html += '' + '<div class="card" data-shop-id="'
                        + item.shopId + '">' + '<div class="card-header">'
                        + item.shopName + '</div>'
                        + '<div class="card-content">'
                        + '<div class="list-block media-list">' + '<ul>'
                        + '<li class="item-content">'
                        + '<div class="item-media">' + '<img src="'
                        + item.shopImg + '" width="44">' + '</div>'
                        + '<div class="item-inner">'
                        + '<div class="item-subtitle">' + item.shopDesc
                        + '</div>' + '</div>' + '</li>' + '</ul>'
                        + '</div>' + '</div>' + '<div class="card-footer">'
                        + '<p class="color-gray">'
                        + new Date(item.lastEditTime).Format("yyyy-MM-dd")
                        + '更新</p>' + '<span>点击查看</span>' + '</div>'
                        + '</div>';
            });
            $('.list-div').append(html);
            var total = $('.list-div .card').length;
            if (total >= maxItems) {
                // 异常加载提示符
                $('.infinite-scroll-preloader').hide();
            }else{
                $('.infinite-scroll-preloader').show();
            }
            pageNum += 1;
            loading = false;
            // 刷新页面,显示新加载的店铺
            $.refreshScroller();
        }
    });
}

// 下滑屏幕 自动进行分页搜索
$(document).on('infinite', '.infinite-scroll-bottom', function() {
    if (loading)
        return;
    addItems(pageSize, pageNum);
});

$('.shop-list').on('click', '.card', function(e) {
    var shopId = e.currentTarget.dataset.shopId;
    window.location.href = '/o2o/fronted/shopdetail?shopId=' + shopId;
});

$('#shoplist-search-div').on('click','.button',function(e) {
            if (parentId) {// 如果传递过来的是一个父类下的子类
                shopCategoryId = e.target.dataset.categoryId;
                alert('ID:'+shopCategoryId);
                if ($(e.target).hasClass('button-fill')) {
                    $(e.target).removeClass('button-fill');
                    shopCategoryId = '';
                } else {
                    $(e.target).addClass('button-fill').siblings().removeClass('button-fill');
                }
                $('.list-div').empty();
                pageNum = 1;
                addItems(pageSize, pageNum);
            } else {// 如果传递过来的父类为空,则按照父类查询
                parentId = e.target.dataset.categoryId;
                if ($(e.target).hasClass('button-fill')) {
                    $(e.target).removeClass('button-fill');
                    parentId = '';
                } else {
                    $(e.target).addClass('button-fill').siblings()
                            .removeClass('button-fill');
                }
                $('.list-div').empty();
                pageNum = 1;
                addItems(pageSize, pageNum);
                parentId = '';
            }

        });

$('#search').on('change', function(e) {
    shopName = e.target.value;
    $('.list-div').empty();
    pageNum = 1;
    addItems(pageSize, pageNum);
});

$('#area-search').on('change', function() {
    areaId = $('#area-search').val();
    $('.list-div').empty();
    pageNum = 1;
    addItems(pageSize, pageNum);
});
$('#me').click(function() {
    $.openPanel('#panel-right-demo');
});
$.init();

});
shopDao.xml:

<?xml version="1.0" encoding="UTF-8"?>
SELECT s.shop_id, s.shop_name, s.shop_desc, s.shop_addr, s.phone, s.shop_img, s.priority, s.create_time, s.last_edit_time, s.enable_status, s.advice, a.area_id, a.area_name, sc.shop_category_id, sc.shop_category_name FROM tb_shop s, tb_area a, tb_shop_category sc and s.shop_category_id=#{shopCondition.shopCategory.shopCategoryId} and s.shop_category_id in(select shop_category_id from tb_shop_category WHERE parent_id = #{shopCondition.shopCategory.parent.shopCategoryId}) and s.area_id=#{shopCondition.area.areaId} and s.shop_name like '%${shopCondition.shopName}%' and s.enable_status = #{shopCondition.enableStatus} and s.owner_id=#{shopCondition.owner.userId} AND s.area_id = a.area_id AND s.shop_category_id = sc.shop_category_id ORDER BY s.priority DESC LIMIT #{rowIndex},#{pageSize}; SELECT count(1) FROM tb_shop s, tb_area a, tb_shop_category sc and s.shop_category_id=#{shopCondition.shopCategory.shopCategoryId} and s.shop_category_id in(select shop_category_id from tb_shop_category WHERE parent_id =#{shopCondition.shopCategory.parent.shopCategoryId}) and s.area_id=#{shopCondition.area.areaId} and s.shop_name like '%${shopCondition.shopName}%' and s.enable_status = #{shopCondition.enableStatus} and s.owner_id=#{shopCondition.owner.userId} AND s.area_id = a.area_id AND s.shop_category_id = sc.shop_category_id 数据库:![图片描述](http://img.mukewang.com/szimg/5bffc07b0001f98b12960586.jpg)
写回答

1回答

翔仔

2018-11-30

同学好,其实解决这个问题不用靠猜,我看同学已经有很好的意识能够在前端打印当时传入的shopCategoryId以及parent了,既然做到这,也可以尝试进入到后端调试,看看最终后端console输出的sql语句是啥,然后分析下这个sql为什么会获取到一模一样的数据,之后修改下mapper即可解决,我的mapper如下

<select id="queryShopList" resultMap="shopMap">
		SELECT
		s.shop_id,
		s.shop_name,
		s.shop_desc,
		s.shop_addr,
		s.phone,
		s.shop_img,
		s.priority,
		s.create_time,
		s.last_edit_time,
		s.enable_status,
		s.advice,
		a.area_id,
		a.area_name,
		sc.shop_category_id,
		sc.shop_category_name
		FROM
		tb_shop s,
		tb_area a,
		tb_shop_category sc
		<where>
			<if
				test="shopCondition.shopCategory != null and 
			shopCondition.shopCategory.shopCategoryId != null">
				and s.shop_category_id =
				#{shopCondition.shopCategory.shopCategoryId}
			</if>
			<if
				test="shopCondition.shopCategory != null 
			and shopCondition.shopCategory.parent!=null 
			and shopCondition.shopCategory.parent.shopCategoryId !=null">
				and s.shop_category_id in (select shop_category_id from
				tb_shop_category
				WHERE parent_id =
				#{shopCondition.shopCategory.parent.shopCategoryId})
			</if>
			<if
				test="shopCondition.area != null and 
			shopCondition.area.areaId != null">
				and s.area_id =
				#{shopCondition.area.areaId}
			</if>
			<if test="shopCondition.shopName != null">
				and s.shop_name like '%${shopCondition.shopName}%'
			</if>
			<if test="shopCondition.enableStatus != null">
				and s.enable_status = #{shopCondition.enableStatus}
			</if>
			<if
				test="shopCondition.owner != null and shopCondition.owner.userId != null">
				and s.owner_id = #{shopCondition.owner.userId}
			</if>
			AND
			s.area_id=a.area_id
			AND
			s.shop_category_id = sc.shop_category_id
		</where>
		ORDER BY
		s.priority DESC
		LIMIT #{rowIndex},#{pageSize};
	</select>


0
1
philip_I
谢谢老师:已经解决了,根据sql打印显示是Controller查询某个二级ShopCategory里面的店铺列表的参数传错了
2018-11-30
共1条回复

Java双版本(SSM到SpringBoot)校园商铺全栈开发

SSM商铺V1.0,解决毕设痛点;SpringBoot商铺V2.0,满足工作刚需

5113 学习 · 8144 问题

查看课程