//1.页面树配置 var setting = { data : { simpleData : { enable : true, idKey : "id", // id编号命名 pIdKey : "pId", // 父id编号命名 } }, async : { enable : true, url : "/loadCatalogTree", autoParam : [ "id" ] }, callback:{ beforeClick:function(treeId, treeNode, clickFlag){ }, onClick:function(treeId, name, treeNode){ if(treeNode.level == 2){ var id = treeNode.id; $("#catalogId").val(id); $.post( "/queryProductById",{"id":id},function(data){ var body = $("#tbody"); body.html(data); } ); } } } }; var zNodes; $(function(){ if(!zNodes){ $.ajax({ type: "POST", url: "/loadCatalogTree", success: function(data){ $(document).ready(function(){ $.fn.zTree.init($("#user_tree"), setting, zNodes); }); } }); } });
//2.ul <div class="zTreeDemoBackground left"> <ul id="user_tree" class="ztree" style="border: 1px solid #617775;overflow-y: scroll;height: 300px;"> </ul> </div>//3.Controller /** * 根据前台传过来的id查询出对应的商品数据 * @return */ @RequestMapping("queryProductById") public String queryProductById(@ModelAttribute("qo") ProductQueryObject qo, Model model, Long id) { // 商品数据 qo.setCatalogId(id); PageResult pageResult = productService.query(qo); model.addAttribute("pageResult", pageResult); //查询所有 return "product/product_result"; } /** *刷新页面时加载所有的一级菜单 * @return */ @RequestMapping("loadCatalogTree") @ResponseBody public List<CatalogVO> loadCatalogTree(Long id,Model model){ //刷新页面先加载所有的父类菜单 List<CatalogVO> list = catalogService.loadMenuByPid(id); return list; }
//Service // 一开始加载所有的父类 public List<CatalogVO> loadMenuByPid(Long id) { List<Catalog> menus = catalogMapper.loadMenuByPid(id); List<CatalogVO> voList = new ArrayList<>(); for (Catalog c : menus) { CatalogVO vo = new CatalogVO(); vo.setId(c.getId()); vo.setLevel(c.getLevel()); vo.setCode(c.getCode()); vo.setName(c.getName()); vo.setIsParent(true); vo.setpId(c.getParentCatalogId() == null ? -1 : c .getParentCatalogId()); voList.add(vo); } return voList; } //高级查询及分页 @Override public PageResult query(ProductQueryObject qo) { int count = productMapper.queryForCount(qo); if (count == 0) { return PageResult.empty(1); } else { List<Product> listData = productMapper.query(qo); return new PageResult(listData, count, qo.getPage(), qo.getPageSize()); } }
//qo对象查询 public class ProductQueryObject extends QueryObject { //名称关键字 private String likeName; //商品创建开始时间 private Date startCreatedDate; //商品创建截止时间 private Date endCreatedDate; //商品上架下架状态 private int state = -1; private Long catalogId= -1L; private Long brandId= -1L; @DateTimeFormat(pattern = "yyyy-MM-dd") public void setStartCreatedDate(Date startCreatedDate) { this.startCreatedDate = startCreatedDate; } @DateTimeFormat(pattern = "yyyy-MM-dd") public void setEndCreatedDate(Date endCreatedDate) { this.endCreatedDate = endCreatedDate; }
//mapper文件配置 <select id="queryForCount" resultType="int"> select count(p.id) from product p <include refid="BaseWhere"/> </select> <select id="query" resultMap="BaseResultMap"> select p.id, p.catalogId, p.brandId, p.createTime, p.name, p.sn, p.image, p.marketPrice, p.basePrice, p.shelve, p.recommend, p.keyword, p.state, p.recycle from product p <include refid="BaseWhere"/> </select> <sql id="BaseWhere"> <where> <if test="likeName!=null"> and p.name like concat('%',#{likeName,jdbcType=VARCHAR},'%') </if> <if test="startCreatedDate!=null"> and p.createTime >= #{startCreatedDate} </if> <if test="endCreatedDate!=null"> and p.createTime <= #{endCreatedDate} </if> <if test="state!=-1"> and p.state = #{state,jdbcType=INTEGER} </if> </where> </sql>
//Catalog接口方法 /** * 一开始加载所有的父类 * @param id * @return */ List<Catalog> loadMenuByPid(@Param("id")Long id);
//Catalog.xml文件配置 <!-- 父分类菜单的加载 --> <select id="loadMenuByPid" resultMap="BaseResultMap"> select id, version, name, createTime, lastModifiedTime, level, code, sequence, childrenCatalogs, products, parentCatalogId,visible,catalogImg,state from catalog <choose> <when test="id == null"> where parentCatalogId is null </when> <otherwise> where parentCatalogId = #{id} </otherwise> </choose> </select>
ztree的三级菜单动态加载(SpringMVC+MyBatis)
最新推荐文章于 2024-03-09 21:49:48 发布