1.type_list.js
var columns = [
{
field : 'selectItem',
radio : true
},
{
title : '分类id',
field : 'id',
visible : false,
align : 'center',
valign : 'middle',
width : '80px'
},
{
title : '分类名称',
field : 'name',
align : 'center',
valign : 'middle',
sortable : true,
width : '180px'
},
{
title : '上级分类',
field : 'parentName',
align : 'center',
valign : 'middle',
sortable : true,
width : '180px'
},
{
title : '排序号',
field : 'sort',//与数据库查询的属性一一对应
align : 'center',
valign : 'middle',
sortable : true,
width : '100px'
}];
$(document).ready(function () {
//给删除按钮绑定删除事件
$("#formHead").on("click",".btn-delete",deleteType);
//加载编辑页面
$("#formHead").on("click",".btn-add",loadEditPage);
findAllProductType();
});
//加载编辑页面
function loadEditPage() {
var url="productType/editUI";
$(".content").load(url,function () {
$(".page-title").html("添加产品分类");
});
}
//删除节点
function deleteType() {
//获取点中按钮的id值
var id = getSelectedId();
if(!id){
alert("请先选择要删除的分类信息!");
return;
}
//根据选中的id值进行删除分类信息
var url = "productType/delectType";
var params = {"id":id};
$.post(url,params,function (result) {
if(result.state == 1){
alert(result.message);
//查询所有数据
findAllProductType();
}else{
alert(result.message);
}
});
}
//获取选中的id值
function getSelectedId() {
//用来获取选中的记录,返回成数组
var selections = $("#typeTable").bootstrapTreeTable("getSelections");
if(selections.length == 0){//表示没有选中
return;
}
//获取选中的id值
return selections[0].id;
}
//查询所有数据
function findAllProductType() {
var tableId = "typeTable";//不用写#,后边js处理过了
//访问服务端的url地址
var url = "productType/findAllProductType";
var table = new TreeTable(tableId, url, columns);//创建表格对象
table.setExpandColumn(2);//设置默认展开列
//初始化table对象
table.init();//发起异步请求获取数据,更新页面
}
// $(document).ready(function(){
// $("#formHead")
// .on("click",".btn-delete",deleteType)
// .on("click",".btn-add",loadEditPage)
// ;
// findAllProductType();
// });
// //加载新增修改页面
// function loadEditPage(){
// var url = "productType/editUI";
// $(".content").load(url,function(){
// //设置标题
// $("#pageTitle").html("添加产品分类");
// });
// }
//
// function deleteType(){
// var id = getSelectedId();
// if(!id){
// alert("请先选择要删除的分类信息!");
// return ;
// }
// // console.log(id);
// var url = "productType/deleteType";
// var params = {"id":id};
// $.post(url,params,function(result){
// if(result.state==1){
// alert(result.message);
// //重新查询
// findAllProductType();
// }else{
// alert(result.message);
// }
// })
// }
// function getSelectedId(){
// var selections = $("#typeTable").bootstrapTreeTable("getSelections");
// if(selections.length==0){
// return ;//表示没选择任何对象
// }
// return selections[0].id;
// }
//
// function findAllProductType(){
// var tableId="typeTable";//对象type_list.jsp中的table id
// var url="productType/findAllProductType";
// var table=new TreeTable(tableId,url,columns);
// // table.setIdField("id");//设置选中记录的返回id()
// // table.setCodeField("id");//设置级联关系的id
// // table.setParentCodeField("parentId");//设置级联关系中的parentId
// table.setExpandColumn(2);//设置默认展开列
// // table.setExpandAll(false);//设置默认不展开
// table.init();//初始化对象树(底层会发起异步请求)
// }
2.type_edit.js(添加界面js)
var zTree;
var setting = {//用于插件数据显示
data : {
simpleData : {
enable : true,
idKey : "id", //节点数据中保存唯一标识的属性名称
pIdKey : "parentId", //节点数据中保存其父节点唯一标识的属性名称
rootPId : null //根节点id
}
}
}
$(document).ready(function(){
//给back按钮绑定事件
$("#btn-return").click(function () {
back();
});
//给上级分类文本框绑定事件
$("#editTypeForm").on("click","#parentNameId",loadZTreeNodes);
//取消事件
$("#typeLayer").on("click",".btn-cancle",hideTree);
//给树的确定绑定事件
$("#typeLayer").on("click",".btn-confirm",setType);
});
//设置上级分类信息
function setType() {
//先获取选中数据
var nodes = zTree.getSelectedNodes();
console.log(nodes[0]);
//将选中的数据信息填充到form的表单中
//隐藏树
};
//隐藏树
function hideTree() {
$("#typeLayer").css("display","none");
}
//加载上级分类表单的树
function loadZTreeNodes() {
//显示树
$("#typeLayer").css("display","block");
var url = "productType/findZTreeNode";
$.getJSON(url,function (result) {
if(result.state == 1){//正常返回数据jquery.zTree.js
zTree = $.fn.zTree.init($("#typeTree"),setting,result.data);
}else {
alert(result.message);
}
});
}
//返回到分类页面
function back() {
var url="productType/listUI?t="+Math.random(1000);
$(".content").load(url);
}
3.productController.java
package com.tanzhou.tzms.product.controller;
import com.tanzhou.tzms.common.vo.Node;
import com.tanzhou.tzms.common.web.JsonResult;
import com.tanzhou.tzms.product.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/productType")
public class ProductTypeController {
@Autowired
private ProductTypeService typeService;
/**
* 返回页面方法
* @return
*/
@RequestMapping("/listUI")
public String listUI(){
return "product/type_list";
}
/**
* 返回产品分类数据
* @return
*/
@RequestMapping("/findAllProductType")
@ResponseBody
public JsonResult findAllProductType(){
List<Map<String, Object>> list = typeService.findAllProductType();
return new JsonResult(list);
}
/**
* 删除结点
*/
@RequestMapping("/delectType")
@ResponseBody
public JsonResult delectType(Integer id){
typeService.delectType(id);
return new JsonResult("删除成功");
}
/**
* 添加产品编辑页面
* @return
*/
@RequestMapping("editUI")
public String editUI(){
return "product/type_edit";
}
/**
* ZTree分层
*/
@RequestMapping("/findZTreeNode")
@ResponseBody
public JsonResult findZTreeNode(){
List<Node> list = typeService.findZTreeNode();
System.out.println(list);
return new JsonResult(list);
}
}
4.productServiceImpl.java
package com.tanzhou.tzms.product.service.impl;
import com.tanzhou.tzms.common.exception.ServiceException;
import com.tanzhou.tzms.common.vo.Node;
import com.tanzhou.tzms.product.dao.ProductTypeDao;
import com.tanzhou.tzms.product.service.ProductTypeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
@Service("productTypeService")
public class ProductTypeServiceImpl implements ProductTypeService {
@Autowired
private ProductTypeDao typeDao;//spring用动态代理的方式实现我们的对象
@Override
public List<Map<String, Object>> findAllProductType() {
List<Map<String, Object>> list = typeDao.findAllProductType();
return list;
}
@Override
public void delectType(Integer id) {
if(id == null || id <= 0){
throw new ServiceException("id值无效:id="+id);
}
//查询当前id下是否有子
Integer i = typeDao.hasChildType(id);
if(i >= 1){
throw new ServiceException("当前结点有子元素,不允许删除");
}
Integer rows = typeDao.delectType(id);
if(rows <= 0){
throw new ServiceException("删除失败");
}
}
@Override
public List<Node> findZTreeNode() {
List<Node> list = typeDao.findZTreeNode();
return list;
}
}
5.productType.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tanzhou.tzms.product.dao.ProductTypeDao">
<!-- public Integer delectType(@Param("id") Integer id);-->
<delete id="delectType">
delete from tz_producttype where id = #{id}
</delete>
<!-- public List<Map<String,Object>> findAllProductType();-->
<select id="findAllProductType" resultType="java.util.Map">
SELECT p1.*,p2.name as parentName FROM tz_producttype p1 LEFT JOIN tz_producttype p2 on p1.parentId = p2.id
</select>
<!-- public Integer hasChildType(Integer id);-->
<select id="hasChildType" resultType="java.lang.Integer">
select count(*) from tz_producttype where parentId = #{id}
</select>
<!-- public List<Node> findZTreeNode();-->
<select id="findZTreeNode" resultType="com.tanzhou.tzms.common.vo.Node">
select id,name,parentId from tz_producttype
</select>
</mapper>