10、新增商品——商品类目选择

新增商品——商品类目选择

使用的是 EasyUI 的 tree 控件

原型

点击新增商品。最终目标如下:
在这里插入图片描述
点击发送的请求为:
POST http://localhost:8081/item/cat/list 404 (Not Found)

功能分析

点击,触发一个事件
在这里插入图片描述
搜索一下selectItemCat,该jsp文件没有,那就是一个外部的 js
在这里插入图片描述
展示商品分类列表,使用EasyUItree控件展示。
在这里插入图片描述

初始化tree请求的url/item/cat/list
参数:
初始化tree时只需要把第一级节点展示,子节点异步加载。
long id(父节点id)
返回值:json。数据格式

 [{    
    "id": 1,    
    "text": "Node 1",    
    "state": "closed"
},{    
    "id": 2,    
    "text": "Node 2",    
    "state": "closed"   
}] 

state:如果节点下有子节点closed,如果没有子节点open

步骤

创建EasyUITreeNode

创建一个pojo来描述tree的节点信息,包含三个属性idtextstate。放到e3-common工程中。

package cn.ynx.e3mall.common.pojo;

import java.io.Serializable;

public class EasyUITreeNode implements Serializable {

    private long id;
    private String text;
    private String state;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }

}

查询的表:
tb_item_cat
查询列:
Idnameisparent
查询条件parentId

Dao

tb_item_cat
可以使用逆向工程生成的代码

Service

参数:long parentId
业务逻辑:
1、根据parentId查询节点列表
2、转换成EasyUITreeNode列表。
3、返回。
返回值:List<EasyUITreeNode>

package cn.ynx.e3mall.service;

import cn.ynx.e3mall.common.pojo.EasyUITreeNode;

import java.util.List;

public interface ItemCatService {

    List<EasyUITreeNode> getCatList(long parentId);

}
package cn.ynx.e3mall.service.impl;


import cn.ynx.e3mall.common.pojo.EasyUITreeNode;
import cn.ynx.e3mall.mapper.TbItemCatMapper;
import cn.ynx.e3mall.pojo.TbItemCat;
import cn.ynx.e3mall.pojo.TbItemCatExample;
import cn.ynx.e3mall.service.ItemCatService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Resource
    private TbItemCatMapper tbItemCatMapper;

    @Override
    public List<EasyUITreeNode> getCatList(long parentId) {
        //根据parentId查询子节点列表
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbItemCat> list = tbItemCatMapper.selectByExample(example);
        //创建返回结果List
        List<EasyUITreeNode> resultList = new ArrayList<>();
        //把列表转换成EasyUITreeNode列表
        for (TbItemCat tbItemCat : list) {
            EasyUITreeNode node = new EasyUITreeNode();
            //设置属性
            node.setId(tbItemCat.getId());
            node.setText(tbItemCat.getName());
            node.setState(tbItemCat.getIsParent()?"closed":"open");
            //添加到结果列表
            resultList.add(node);
        }
        //返回结果
        return resultList;
    }

}

发布服务:

    <dubbo:service interface="cn.ynx.e3mall.service.ItemCatService" ref="itemCatServiceImpl" timeout="300000" />

Controller

引用服务:

    <dubbo:reference interface="cn.ynx.e3mall.service.ItemCatService" id="itemCatService" />

初始化tree请求的url/item/cat/list
参数:
long id(父节点id)
返回值:json。数据格式
List<EasyUITreeNode>

package cn.ynx.e3mall.controller;

import cn.ynx.e3mall.common.pojo.EasyUITreeNode;
import cn.ynx.e3mall.service.ItemCatService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class ItemCatController {

    @Resource
    private ItemCatService itemCatService;

    @RequestMapping("/item/cat/list")
    @ResponseBody
    public List<EasyUITreeNode> getItemCatList(
            @RequestParam(name = "id" ,defaultValue = "0")Long parentId){
        //调用服务查询节点列表
        List<EasyUITreeNode> list = itemCatService.getCatList(parentId);
        return list;
    }

}

@RequestParam(value="id", defaultValue="0")Long parented
id一个默认值0

猜你喜欢

转载自blog.csdn.net/weixin_42112635/article/details/84888716