Lazy loading with jsTree

前端 未结 5 1043
一向
一向 2021-02-07 10:45

I am trying to dynamically load the nodes of a jtree when they are expanded. The little documentation I found is at the end of this page.

I found some solutions that cre

5条回答
  •  余生分开走
    2021-02-07 11:15

    Extending Nathans answer with a (very minimalistic) example: the Demo tree with lazy loading.

    JS:

    $('#the_tree').jstree({
        'core' : {
            'data' : {
                'url' : "tree/ajax.php", 
                  'data' : function (node) {
                      return { 'id' : node.id };
                  }
            }
        },
    
    });
    

    PHP:

    header('Content-Type: application/json');
    
    if ( $_GET["id"] === "#" ) { 
        $data = array(
                array( "id" => "ajson1", "parent" => "#", "text" => "Simple root node"  ),
                array( "id" => "ajson2", "parent" => "#", "text" => "Root node 2", "children" => true ),
    
        );
    }
    
    else if ( $_GET["id"] === "ajson2" ) {
        $data = array(
            array( "id" => "ajson3", "parent" => "ajson2", "text" => "Child 1" ),
            array( "id" => "ajson4", "parent" => "ajson2", "text" => "Child 2" )
        );
    }
    
    echo json_encode( $data);
    

    only Nodes that have "children" : true, will generate a request for children when opened, other nodes are treated as leaves.

提交回复
热议问题