Ext JS with PHP: How to Add Children to Async Tree Nodes

After reading Ext JS with PHP: How to Create Nodes for a TreePanel, some readers have asked how the aproach discussed in the article can be extended to add children to any async node, as shown in the following screenshot:

extjs-tree-nodes-2

In our Ext JS TreePanel, expanding any parent node (My Project, Datasources and Reports) will generate an http request for the node’s children.  What we need to do then is monitor the request parameters, determine which parent node should be populated, and generate the child nodes.  Let’s modify our original code so we can accomplish this.

A PHP model for Ext JS tree nodes

First, remember that we’re using the following PHP classes to model the Ext JS tree nodes:

class TreeNode {
    
    public $text = "";
    public $id = "";
    public $iconCls = "";
    public $leaf = true;
    public $draggable = false;
    public $href = "#";
    public $hrefTarget = "";

    function  __construct($id,$text,$iconCls,$leaf,$draggable,
            $href,$hrefTarget) {
    
        $this->id = $id;
        $this->text = $text;
        $this->iconCls = $iconCls;
        $this->leaf = $leaf;
        $this->draggable = $draggable;
        $this->href = $href;
        $this->hrefTarget = $hrefTarget;    
    }    
}

class TreeNodes {
    
    protected $nodes = array();
    
    function add($id,$text,$iconCls,$leaf,$draggable,
        $href,$hrefTarget) {
    
        $n = new TreeNode($id,$text,$iconCls,$leaf,
                $draggable,$href,$hrefTarget);
        
        $this->nodes[] = $n;
    }
    
    function toJson() {
        return json_encode($this->nodes);
    }
}

Creating children for Ext JS async tree nodes

We now need to capture the parent node that is making the request:

$requestedNode = "";

if (isset($_REQUEST["node"])) {
    $requestedNode = $_REQUEST["node"];
}

With the parent node captured, we can build its child nodes, and send them to the Ext JS client:

$treeNodes = new TreeNodes();

if ('node-root' == $requestedNode) {
    $treeNodes->add("node-datasources","Datasources","",false,false,"","");
    $treeNodes->add("node-reports","Reports","",false,false,"","");
} else if ('node-datasources' == $requestedNode) {
    $treeNodes->add("employees-system-node","Employee Management System","datasource",true,false,"","");
    $treeNodes->add("customers-system-node","Customer Management System","datasource",true,false,"","");
    $treeNodes->add("order-system-node","Order Processing System","datasource",true,false,"","");
} else if ('node-reports' == $requestedNode) {
    $treeNodes->add("time-report-node","Time and Attendance","report",true,false,"","");
    $treeNodes->add("orders-by-quarter-report-node","Orders By Quarter","report",true,false,"","");
    $treeNodes->add("customers-trends-report-node","Customer Trends","report",true,false,"","");
}

echo $treeNodes->toJson();

And this is the TreePanel definition:

var tree = new Ext.tree.TreePanel({
    renderTo: Ext.getBody(),
    title: 'Reporting Project',
    width: 250,
    height: 250,
    userArrows: true,
    animate: true,
    autoScroll: true,
    dataUrl: 'tree-nodes-2.php',
    root: {
        nodeType: 'async',
        text: 'My Project',
        id:'node-root'
    },
    listeners: {
        render: function() {
            this.getRootNode().expand();
        }
    }
})

This is, my friends, all it takes to add children to Ext JS async tree nodes using PHP.  Want to share your preferred approach?

Downloads

Grab the code for this article from my downloads page.

Want to learn more?

Ext-JS-Cookbook Check out my Ext JS 3.0 Cookbook.  It contains more than a hundred step-by-step recipes that explain useful techniques you can use to build great Ext JS applications.

E-mail   Permalink    Comments(7)   Trackback

Tags: , ,

Comments

By aries4203  on Tuesday, March 16, 2010

hello sir Jorge:

thank you very much for this article. it helped me a lot in creating a tree that displays data from a database after some tweaks.

i have a read your book and tried running the sample on chapter 6 - Editing rows with the Row Editor plugin but failed. Errors like Ext.ux.RowEditor is not a constructor -  saveText: 'Update' and the likes are bugging me. I am using ExtJS 3.1.1 when i tried you sample. I hope you can feature a tutorial on this topic.

thank you very much and more power to you.

aries

By Jorge  on Tuesday, March 16, 2010

@aries:

Looks like you're missing the RowEditor's js file.  You can obtain it at: www.extjs.com/deploy/dev/examples/ux/RowEditor.js

By aries  on Sunday, March 21, 2010

i have a copy of the RowEditor .js and .css in their respective directories.

By aries4203  on Sunday, March 21, 2010

sir Jorge,

i am using RowEditor .js and .css 3.1.1 and using the sample code in your book. i am able to display the grid as of now though and i get this error when i click on the following:

New button = ed is undefined
      ed.margins = pm('0 1 2');
      "www.buwbcs.com/EXTbuwbcs/js/RowEditor.js"" rel="nofollow">www.buwbcs.com/EXTbuwbcs/js/RowEditor.js"" rel="nofollow">www.buwbcs.com/EXTbuwbcs/js/RowEditor.js"
      Line 274

Delete button = fields[i] is undefined
         value = this.p...[i].getValue(), oldValue, r, dindex);
      "www.buwbcs.com/EXTbuwbcs/js/RowEditor.js"" rel="nofollow">www.buwbcs.com/EXTbuwbcs/js/RowEditor.js"" rel="nofollow">www.buwbcs.com/EXTbuwbcs/js/RowEditor.js"
      Line 206

a Row in the grid =  ed.field is undefined
      ed.field.focus();
      "www.buwbcs.com/EXTbuwbcs/js/RowEditor.js"" rel="nofollow">www.buwbcs.com/EXTbuwbcs/js/RowEditor.js"" rel="nofollow">www.buwbcs.com/EXTbuwbcs/js/RowEditor.js"
      Line 412

thank you in advance for any help i will receive.

aries

By aries4203  on Sunday, March 21, 2010

sorry for the error on how my post was displayed. i dont know whay it displayed like that

aries

By Jorge  on Monday, March 22, 2010

@aries4203

Did you try using relative references for your js files, instead of absolute?

By aries4203  on Saturday, March 27, 2010

thanks, I already got rid of those errors when i re-downloaded and used ExtJS version 3.1.1.

my problem now is that when i save edited fields the dirty indicator (red corner tab) remains displayed even if the record was already updated.

another thing is that the phone number won't save.

any idea how to solve this issues?

thanks,

aries4203

(Note: i'm still using your book sample with revisions on the php module.)