精灵商场项目(三)--商品分类列表+商品CRUD

mac2024-07-31  53

文章目录

一、EasyUI中弹出框二、商品分类列表展现1.1 商品分类列表展现1.1.1 页面分析 1.2 EasyUI中的Tree1.3 编辑VO值对象-EasyUITree1.4 商品分类页面TreeAjax请求1.5 EasyUI异步树加载1.6 编辑ItemCatController1.7 编辑ItemCatService1.8 页面效果在这里插入图片描述 三、EasyUI 数据校验四、商品新增4.1 页面分析4.2 封装SysResult VO对象4.3 全局异常处理机制4.4 编辑ItemController4.5 编辑ItemService 五、商品更新5.1 页面跳转过程5.2 修改页面分析5.3 编辑ItemController5.4 编辑ItemService 六、商品上架/下架6.1 修改页面分析6.2 编辑ItemController6.3 编辑ItemService 七、商品的删除7.1 修改页面分析7.2 编辑ItemController7.3 编辑ItemService

今天主要是做精灵项目后台的维护功能

一、EasyUI中弹出框

页面的主要展现弹出框

自定义弹出窗口

定义退出消息框

<script type="text/javascript"> $(function(){ $("#btn1").bind("click",function(){ $("#win1").window({ title:"弹出框", width:400, height:400, modal:true //这是一个模式窗口,只能点击弹出框,不允许点击别处 }) }) $("#btn3").click(function(){ alert("关闭"); $("#win2").window("close"); }); /*定义退出消息框 */ $("#btn4").click(function(){ $.messager.confirm('退出','你确定要退出吗',function(r){ if (r){ alert("确认退出"); } }); }) /*定义消息提示框 */ $.messager.show({ title:'My Title', msg:'龚玲博你都胖成一个球了,圆的', timeout:3000, height:200, width:300, showType:'slide' }); }) </script> </head> <body> <h1>Easy-弹出窗口</h1> <!--主要展现弹出框 --> <a id="btn1" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-filter'">搜索</a> <div id="win1"></div> <!--定义弹出窗口 --> <div id="win2" class="easyui-window" title="My Window" style="width:600px;height:400px" data-options="iconCls:'icon-save',modal:true"> 我是一个窗口 <a id="btn3" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-back'">关闭</a> </div> <div style="float: right"> <a id="btn4" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'">退出系统</a> </div> </body>

二、商品分类列表展现

1.1 商品分类列表展现

1.1.1 页面分析

页面分析

"javascript:void(0)" 空方法 , 相当于占位符的作用

js显示分类列表

<a href="javascript:void(0)" class="easyui-linkbutton selectItemCat">选择类目</a> $(".selectItemCat").each(function(i,e){//i= index 下标,e:element:元素 var _ele = $(e); if(data && data.cid){ _ele.after("<span style='margin-left:10px;'>"+data.cid+"</span>"); }else{ _ele.after("<span style='margin-left:10px;'></span>"); } _ele.unbind('click').click(function(){ $("<div>").css({padding:"5px"}).html("<ul>") .window({ width:'500', height:"450", modal:true, closed:true, iconCls:'icon-save', title:'选择类目', onOpen : function(){......}

1.2 EasyUI中的Tree

通过js创建树形结构

Tree结构的JSON串格式 : [{id:"编号",text:"文本信息",state:"open/closed"},{....}]

<script type="text/javascript"> /*通过js创建树形结构 */ $(function(){ $("#tree").tree({ url:"tree.json", //加载远程JSON数据 method:"get", //请求方式 POST animate:true, //表示显示折叠端口动画效果 checkbox:true, //表述复选框 lines:false, //表示显示连接线 dnd:true, //是否拖拽 onClick:function(node){ //添加点击事件 alert(node.text); //控制台 console.info(node); } }); }) </script> </head> <body> <h1>EasyUI-树形结构</h1> <ul id="tree"></ul> </body>

1.3 编辑VO值对象-EasyUITree

用来封装商品分类Tree结构对象信息,应用于业务层

@Data @Accessors(chain = true) @NoArgsConstructor @AllArgsConstructor public class EasyUITree { private Long id; //节点ID private String text; //文本信息 private String state; //open/closed }

1.4 商品分类页面TreeAjax请求

$("ul",_win).tree({ url:'/item/cat/list', animate:true, onClick : function(node){ if($(this).tree("isLeaf",node.target)){ // 填写到cid中 _ele.parent().find("[name=cid]").val(node.id); _ele.next().text(node.text).attr("cid",node.id); $(_win).window('close'); if(data && data.fun){ data.fun.call(this,node); } } } });

1.5 EasyUI异步树加载

树控件读取URL。子节点的加载依赖于父节点的状态。当展开一个封闭的节点,如果节点没有加载子节点,它将会把节点id的值作为http请求参数并命名为’id’,通过URL发送到服务器上面检索子节点。

1.6 编辑ItemCatController

获取商品分类列表信息

url:/item/cat/list

返回值: EasyUITree

@RequestParam 获取参数实现数据的转化

@RequestMapping("/list") public List<EasyUITree> findItemCatByParentId (@RequestParam(value = "id",defaultValue = "0") Long parentId){ return itemCatService.findItemCatByParentId(parentId); }

1.7 编辑ItemCatService

根据parentId查询数据库记录

循环遍历数据,之后封装EasyUITree的list集合

步骤:

查询数据实现数据封装 @Override public List<EasyUITree> findItemCatByParentId(Long parentId) { //1.查询数据 List<ItemCat> itemCatList = findItemCatListByParentId(parentId); //2.实现数据封装 List<EasyUITree> treeList = new ArrayList<EasyUITree>(itemCatList.size()); for (ItemCat itemCat : itemCatList) { Long id = itemCat.getId(); String text = itemCat.getName(); //如果是父级 closed,否则 表示3级标题 open String state = itemCat.getIsParent()?"closed":"open"; EasyUITree tree = new EasyUITree(id,text,state); treeList.add(tree); } return treeList; } private List<ItemCat> findItemCatListByParentId(Long parentId) { QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("parent_id", parentId); List<ItemCat> itemCatList = itemCatMapper.selectList(queryWrapper); return itemCatList; } }

1.8 页面效果

实现商品分类列表展示

三、EasyUI 数据校验

非空校验

<td> <input class="easyui-textbox" type="text" name="title" data-options="required:true" style="width: 280px;"> </input> </td>

有效性校验

<input class="easyui-numberbox" type="text" name="priceView" data-options="min:1,max:99999999,precision:2,required:true" />

四、商品新增

4.1 页面分析

页面URL分析 : http: //localhost: 8091/item/save

页面JS分析

function submitForm(){ //表单校验 if(!$('#itemAddForm').form('validate')){ $.messager.alert('提示','表单还未填写完成!'); return ; } //转化价格单位,将元转化为分 //获取元素值 $("xxx").val(); //为元素赋值 $("XXX").val(100); '1'*1=11 $("#itemAddForm [name=price]").val(eval($("#itemAddForm [name=priceView]").val()) * 100); itemAddEditor.sync();//将输入的内容同步到多行文本中 $("#itemAddForm [name=itemParams]").val(paramJson); /*$.post/get(url,JSON,function(data){....}) ?id=1&title="天龙八部&key=value...." */ //alert($("#itemAddForm").serialize()); $.post("/item/save",$("#itemAddForm").serialize(), function(data){ if(data.status == 200){ $.messager.alert('提示','新增商品成功!'); }else{ $.messager.alert("提示","新增商品失败!"); } }); }

4.2 封装SysResult VO对象

该类是系统级VO对象 , 放在jl-common子项目中

用来封装状态信息以及数据

@Data @Accessors(chain = true) @NoArgsConstructor @AllArgsConstructor public class SysResult { private Integer status; //状态码 200成功 201失败 private String msg; //提示信息 private Object data; //返回数据 /** * 1.只是告知用户 执行成功 * @return */ public static SysResult success() { return new SysResult(200, null, null); } /** * 2.成功之后返回数据data String=name */ public static SysResult success(Object data) { return new SysResult(200, null, data); } /** * 3.成功之后返回msg */ public static SysResult success(String msg, Object data) { return new SysResult(200, msg, data); } /*失败之后调用*/ public static SysResult fail() { return new SysResult(201, "业务执行失败", null); } }

4.3 全局异常处理机制

在jt-common中添加SysExecution类.实现全局异常处理

当系统中出现运行时异常时生效

返回数据为JSON

@RestControllerAdvice //异常通知 对Controller层生效 @Slf4j //记录日志 public class SysExecution { //当系统中出现运行时异常时生效 @ExceptionHandler(RuntimeException.class) public SysResult error(Exception exception) { exception.printStackTrace(); log.error(exception.getMessage()); return SysResult.fail(); } }

4.4 编辑ItemController

实现商品新增

@RequestMapping("/save") public SysResult saveItem(Item item) { itemService.saveItem(item); return SysResult.success(); }

4.5 编辑ItemService

实现商品新增的业务,通过mybatis-plus的insertAPI持久化到数据库

@Override public void saveItem(Item item) { item.setStatus(1) //表示正常状态 .setCreated(new Date()) .setUpdated(item.getCreated()); itemMapper.insert(item); }

五、商品更新

5.1 页面跳转过程

text:'编辑', iconCls:'icon-edit', handler:function(){ //获取用户选中的数据 var ids = getSelectionsIds(); if(ids.length == 0){ $.messager.alert('提示','必须选择一个商品才能编辑!'); return ; } if(ids.indexOf(',') > 0){ $.messager.alert('提示','只能选择一个商品!'); return ; }

5.2 修改页面分析

url分析 : http: //localhost: 8091/item/update

页面JS分析

$.post("/item/update",$("#itemeEditForm").serialize(), function(data){ if(data.status == 200){ $.messager.alert('提示','修改商品成功!','info',function(){ $("#itemEditWindow").window('close'); $("#itemList").datagrid("reload"); }); }else{ $.message.alert("提示",data.msg); } });

5.3 编辑ItemController

实现商品更新

@RequestMapping("/update") public SysResult updateItem(Item item) { itemService.updateItem(item); return SysResult.success(); }

5.4 编辑ItemService

通过updateByIdAPI来实现所有数据更新

@Override public void updateItem(Item item) { item.setUpdated(new Date()); itemMapper.updateById(item); }

六、商品上架/下架

6.1 修改页面分析

下架url分析 : http: //localhost: 8091/item/instock

上架url分析 : http: //localhost: 8091/item/reshelf

页面JS分析

{ text: '下架', iconCls: 'icon-remove', handler: function () { //获取选中的ID串中间使用","号分割 var ids = getSelectionsIds(); if (ids.length == 0) { $.messager.alert('提示', '未选中商品!'); return; } $.messager.confirm('确认', '确定下架ID为 ' + ids + ' 的商品吗?', function (r) { if (r) { var params = {"ids": ids}; $.post("/item/instock", params, function (data) { if (data.status == 200) { $.messager.alert('提示', '下架商品成功!', undefined, function () { $("#itemList").datagrid("reload"); }); } }); } }); } }, { text: '上架', iconCls: 'icon-remove', handler: function () { var ids = getSelectionsIds(); if (ids.length == 0) { $.messager.alert('提示', '未选中商品!'); return; } $.messager.confirm('确认', '确定上架ID为 ' + ids + ' 的商品吗?', function (r) { if (r) { var params = {"ids": ids}; $.post("/item/reshelf", params, function (data) { if (data.status == 200) { $.messager.alert('提示', '上架商品成功!', undefined, function () { $("#itemList").datagrid("reload"); }); } }); } }); } }

6.2 编辑ItemController

实现商品上/下架

@RequestMapping("instock") public SysResult instockItem(Long[] ids) { int status = 2; //表示下架 itemService.updateStatus(ids,status); return SysResult.success(); } @RequestMapping("/reshelf") public SysResult reshelf(Long[] ids) { int status = 1; //表示上架 itemService.updateStatus(ids,status); return SysResult.success(); }

6.3 编辑ItemService

下架将ids中所有的数据的状态status改为2

下架将ids中所有的数据的状态status改为1

编辑updateStatus方法 , 调用updateAPI以及UpdateWrapper 条件构造器进行修改状态,实现上/下架业务

@Override public void updateStatus(Long[] ids, Integer status) { Item item = new Item(); item.setStatus(status).setUpdated(new Date()); UpdateWrapper<Item> updateWrapper = new UpdateWrapper<Item>(); List idList = Arrays.asList(ids); updateWrapper.in("id", idList); itemMapper.update(item, updateWrapper); }

七、商品的删除

7.1 修改页面分析

url分析 : http: //localhost: 8091/item/delete

页面JS分析

{ text: '删除', iconCls: 'icon-cancel', handler: function () { var ids = getSelectionsIds(); if (ids.length == 0) { $.messager.alert('提示', '未选中商品!'); return; } $.messager.confirm('确认', '确定删除ID为 ' + ids + ' 的商品吗?', function (r) { if (r) { var params = {"ids": ids}; $.post("/item/delete", params, function (data) { if (data.status == 200) { $.messager.alert('提示', '删除商品成功!', undefined, function () { $("#itemList").datagrid("reload"); }); } else { $.messager.alert("提示", data.msg); } }); } }); } }

7.2 编辑ItemController

实现商品删除

@RequestMapping("/delete") public SysResult delete(Long[] ids){ itemService.deleteItem(ids); return SysResult.success(); }

7.3 编辑ItemService

使用deleteByIdAPI对选择的商品进行删除

@Override public void deleteItem(Long[] ids) { List idList = Arrays.asList(ids); itemMapper.deleteBatchIds(idList); }
最新回复(0)