jquery-easyui中创建异步加载树

jquery-easyui中创建异步加载树

objectexceptionasynchronousintegernullclass

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

Html代码

1.

    编写前台代码:

    Js代码

    1.$('#tt').tree({

    2. url:'/demo2/node/getNodes'// The url will be mapped to NodeController class and getNode

    s method

    3.});

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

    Java代码

    1.@Table(name="nodes")

    2.public class Node extends ActiveRecordBase{

    3.@Id public Integer id;

    4.@Column public Integer parentId;

    5.@Column public String name;

    6.

    7.public boolean hasChildren() throws Exception{

    8.long count = count(Node.class, "parentId=?", new Object[]{id});

    9.return count > 0;

    10. }

    - 1 -

    11.}

    编写后台的控制器代码:

    Java代码

    1.public class NodeController extends ApplicationController{

    2./**

    3. * get nodes, if the 'id' parameter equals 0 then load the first level nodes,

    4. * otherwise load the children nodes

    5. * @param id the parent node id value

    6. * @return the tree required node json format

    7. * @throws Exception

    8. */

    9.public View getNodes(int id) throws Exception{

    10. List nodes = null;

    11.

    12.if (id == 0){ // return the first level nodes

    13. nodes = Node.findAll(Node.class, "parentId=0 or parentId is null", null);

    14. } else { // return the children nodes

    15. nodes = Node.findAll(Node.class, "parentId=?", new Object[]{id});

    16. }

    17.

    18. List> items = new ArrayList>();

    19.for(Node node: nodes){

    20. Map item = new HashMap();

    21. item.put("id", node.id);

    22. item.put("text", https://www.360docs.net/doc/5211068905.html,);

    23.

    24.// the node has children,

    25.// set the state to 'closed' so the node can asynchronous load children nodes

    26.if (node.hasChildren()){

    27. item.put("state", "closed");

    28. }

    29. items.add(item);

    30. }

    31.

    32.return new JsonView(items);

    33. }

    - 2 -

    34.}

    运行测试程序:http://localhost:8080/demo2/

    - 3 -

    相关主题
    相关文档
    最新文档