栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

解析jquery easyui tree异步加载子节点问题

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

解析jquery easyui tree异步加载子节点问题

easyui中的树可以从标记中建立,也可以通过指定一个URL属性读取数据建立。如果想建立一棵异步树,需要为每个节点指定一个id属性值,这样在加载数据时会自动向后台传递id参数。

    编写前台代码:

    $('#tt').tree({
      url:'/demo2/node/getNodes'  // The url will be mapped to NodeController class and getNodes method
    });

    为测试用,建立一个节点的数据模型:

    @Table(name="nodes")
    public class Node extends ActiveRecordbase{
      @Id public Integer id;
      @Column public Integer parentId;
      @Column public String name;
     
      public boolean hasChildren() throws Exception{
        long count = count(Node.class, "parentId=?", new Object[]{id});
        return count > 0;
      }
    }
    
    

    编写后台的控制器代码:

    public class NodeController extends ApplicationController{
      
      public View getNodes(int id) throws Exception{
        List nodes = null;
     
        if (id == 0){  // return the first level nodes
          nodes = Node.findAll(Node.class, "parentId=0 or parentId is null", null);
        } else {  // return the children nodes
          nodes = Node.findAll(Node.class, "parentId=?", new Object[]{id});
        }
     
        List> items = new ArrayList>();
        for(Node node: nodes){
          Map item = new HashMap();
          item.put("id", node.id);
          item.put("text", node.name);
     
          // the node has children, 
          // set the state to 'closed' so the node can asynchronous load children nodes 
          if (node.hasChildren()){
     item.put("state", "closed");
          }
          items.add(item);
        }
     
        return new JsonView(items);
      }
    }

    官网例子地址:http://www.jeasyui.com/tutorial/tree/tree2.php

    demo下载:easyui-tree2_jb51.rar 

    重要的事情说三遍!!!

    $('#tt').tree({
      method:"POST",
      url:'/demo2/node/getNodes'  // The url will be mapped to NodeController class and getNodes method
    });

    method一定要用POST,GET的话要在URL后面用一个变量来做时间戳处理。

    method一定要用POST,GET的话要在URL后面用一个变量来做时间戳处理。

    method一定要用POST,GET的话要在URL后面用一个变量来做时间戳处理。

    以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

    转载请注明:文章转载自 www.mshxw.com
    本文地址:https://www.mshxw.com/it/89326.html
    我们一直用心在做
    关于我们 文章归档 网站地图 联系我们

    版权所有 (c)2021-2022 MSHXW.COM

    ICP备案号:晋ICP备2021003244-6号