easyUI tree 和构造tree 后台是一样的。

来源:互联网 发布:php前景 知乎 编辑:程序博客网 时间:2024/04/28 14:53
$(function() {
loadproductTree();
initForSearch();
});
// ///////////////////初始化树///////////////////////////////////////////////////////
function loadproductTree() {
$('#productTree')
.tree(
{
checkbox : false,
lines : true,
method : 'post',
url : 'platform/cmtModuleManager/CmtProductWHController/findChildsByNodeId.json',
dataType : "json",
loadFilter : function(data) {
if (data.data) {
return data.data;
} else {
return data;
}
},
onClick : function(node) {
nodeType=node.attributes.NODE_TYPE;
                             loadproductdimensionality(node.id);
                             $("#search_CURRENT_NODE_ID").val(currentNodeId);
                             if(node.attributes.P_ID=="-1"){
                            $("#toolbarhide").hide();
                             }else{
                            $("#toolbarhide").show();
                             }
},
onLoadSuccess : function(node){
var root = $('#productTree').tree('getRoot');
if(null!=root){
$('#productTree').tree('select',root.target);
currentNodeId=root.id;
loadproductdimensionality(root.id);
$("#search_CURRENT_NODE_ID").val(currentNodeId);
}
$("#toolbarhide").hide();
},
onContextMenu : function(e, node) {
e.preventDefault();
$(this).tree('select', node.target);

if(node.attributes.P_ID=="-1"){
$('#demoMenuRoot').menu('show', {
left : e.pageX,
top : e.pageY
});
                            }else{
                            $('#demoMenu').menu('show', {
left : e.pageX,
top : e.pageY
});
                            }
}
});
}
// //////////////////加载treeGrid树数据/////////////////////////////////////////////////////
/**
 * 查询
 */
function initForSearch() {
$('#formSearch')
.searchbox(
{
width : 200,
searcher : function(value) {
var path = "platform/cmtModuleManager/CmtProductWHController/searchTree";
if (value == null || value == "") {
path = "platform/cmtModuleManager/CmtProductWHController/findChildsByNodeId";
}
$.ajax({
cache : true,
type : "POST",
url : path,
dataType : "json",
data : {
search_productName : value
},
async : false,
timeout : 10000,
error : function(request) {
alert("操作失败,服务请求状态:" + request.status + " "
+ request.statusText
+ " 请检查服务是否可用!");
},
success : function(data) {
if (data.result == 0) {
$('#productTree').tree('loadData',
data.data);


} else {
alert("查询失败:" + data.msg);
}
}
});
},
prompt : "请输入关键字!"
});

}

---------------------------------查询后台

/**
   * 查询树
   * @param response
   * @param request
   * @return
   * @throws Exception
   */
@RequestMapping(value = "/searchTree")
public ModelAndView searchTree(HttpServletResponse response, HttpServletRequest request)
throws Exception {

ModelAndView mv = new ModelAndView();
TreeNode rootTreeNode=null;

try{

//获取查询参数
Map<String,Object> params=WebUtils.getParametersStartingWith(request,"search_");

List<TreeNode> nodeList=new ArrayList<TreeNode>();

//处理真实查到数据,高亮节点【高亮其实也可以放到前台js去画】
List<CmtProduct> dtList=(List<CmtProduct>)cmtProductWHService.findProductTree(params);

for(CmtProduct dt:dtList){
TreeNode node=new TreeNode();
node.setId(dt.getId());
node.setText("<a style='color:#fff;font-weight:normal;background:#3399ff;padding:0 4px;'>"+dt.getProductName()+"</a>");
node.setState("open");
String nodeType = dt.getNodeType();
String iconCls = cmtProductWHService.setProductIcon(nodeType);
if(dt.getParentId().equals("-1")){
iconCls="nodeType00";
}
node.setIconCls("tree-"+iconCls);
node.setAttributes(dt);
node.set_parentId(dt.getParentId());
nodeList.add(node);
}

//处理父节
List<CmtProduct> pathList=(List<CmtProduct>)cmtProductWHService.findPathDemoTree(dtList);
for(CmtProduct dt:pathList){
TreeNode node=new TreeNode();
node.setId(dt.getId());
node.setText(dt.getProductName());
String nodeType = dt.getNodeType();
String iconCls = cmtProductWHService.setProductIcon(nodeType);
if(dt.getParentId().equals("-1")){
iconCls="nodeType00";
}
node.setIconCls("tree-"+iconCls);
node.setState("open");
node.setAttributes(dt);
node.set_parentId(dt.getParentId());
nodeList.add(node);
}

//转换成具有父子关系的结构
rootTreeNode=TreeUtils.parseToTree(nodeList);
mv.addObject("result",OpResult.success.ordinal());
}catch(Exception e){
logger.error(e.getMessage(),e);
mv.addObject("result",OpResult.failure.ordinal());
mv.addObject("msg","后台处理异常,请联系管理员!");
}

if(rootTreeNode==null){
CmtProduct dt = this.cmtProductWHService.getRootCmtProductTree();
rootTreeNode = new TreeNode();
rootTreeNode.setId(dt.getId());
rootTreeNode.setText(dt.getProductName());
rootTreeNode.setIconCls("tree-nodeType00");
rootTreeNode.setState("open");
rootTreeNode.setAttributes(dt);
rootTreeNode.set_parentId(dt.getParentId());
mv.addObject("data",new Object[]{rootTreeNode});
}else{
mv.addObject("data",new Object[]{rootTreeNode});
}
return mv;
}

------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
* 查询数据
*/
public List<?> findProductTree(Map<String, Object> params) {


DetachedCriteria dc = DetachedCriteria.forClass(CmtProduct.class);
for (String key : params.keySet()) {
Criterion c = Restrictions.like(key, "%" + params.get(key) + "%");
dc.add(c);
}
List<CmtProduct> treeList = (List<CmtProduct>) this.hibernateDao
.findByCriteria(dc);
return treeList;
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
* 查询节点的所有父新节点

* @param dtList
*            当前节点
* @return 所有当前节点父节点集合
*/
public List<?> findPathDemoTree(List<CmtProduct> dtList) {


List<CmtProduct> treeList = new ArrayList<CmtProduct>();
List<String> pathList = new ArrayList<String>();


// 查询父节点
for (CmtProduct dt : dtList) {
// String path=dt.getPath();
String path = dt.getAttribute01();
if (StringUtils.isEmpty(path))
continue;
List<String> templist = new ArrayList<String>(Arrays.asList(path
.split("\\[&]")));
templist.removeAll(pathList);
pathList.addAll(templist);
}


if (pathList.size() > 0) {
// 防止重复查询
for (CmtProduct dt : dtList) {
if (pathList.contains(dt.getId())) {
pathList.remove(dt.getId());
}
}
if (pathList.size() > 0) {
DetachedCriteria dc2 = DetachedCriteria
.forClass(CmtProduct.class);
Criterion c = Restrictions.in("id", pathList);
dc2.add(c);
List<CmtProduct> dtList2 = (List<CmtProduct>) this.hibernateDao
.findByCriteria(dc2);
treeList.addAll(dtList2);
}
}
return treeList;
}

0 0
原创粉丝点击