homelessguestb | welcome to my space

Insert Node inside another Node

  Escrito el 9 de January del 2009 por wktd.
  • Hi.
    If I have this tree:

    root (AsyncTreeNode)
    -Category1 (id: cat1)
    ---Node1
    ---Node2
    -Category2 (id: cat2)
    ---Node3
    ---Node4

    And if I go to "Add Node" Button, I wish to add a new Node inside "Category2". How can i do that?

    I have this code, but this code insert the new Node inside root node:

    this.root.appendChild(node);


  • That's Ok, but the nodes are loaded by TreeLoader and if i put your code, doesn't work.
    Here is the code:

    var getfeeds = new Ext.tree.TreeLoader({
    dataUrl: 'script.php',
    baseParams:{
    accion: 'listar'
    }
    });

    var root = new Ext.tree.AsyncTreeNode({
    text: 'Feeds',
    id:'feeds',
    expanded: true,
    allowDrag:false,
    allowDrop:false
    });

    var tree = new Ext.tree.TreePanel({
    id:'feed-tree',
    region:'west',
    title:'Listado Feeds',
    split:true,
    width: 210,
    minSize: 210,
    maxSize: 400,
    collapsible: true,
    animate:true,
    maskDisabled: false,
    margins:'0 0 0 0',
    cmargins:'0 5 0 5',
    rootVisible:true,
    enableDD:true,
    root: root,
    loader: getfeeds,
    lines:false,
    autoScroll:true,
    collapseFirst:false,
    tbar: [{
    iconCls:'add-feed',
    text:'Agregar Feed',
    handler: this.showWindow,
    scope: this
    },{
    id:'delete',
    iconCls:'delete-icon',
    text:'Borrar Feed',
    handler: function(){
    var s = this.getSelectionModel().getSelectedNode();
    if(s){
    this.removeFeed(s.attributes.id);
    }
    },
    scope: this
    }],
    bbar: [{
    iconCls:'add-feed-cat',
    text:'Agregar Categoría',
    scope: this
    }, '->',{
    iconCls:'refresh3',
    text:'Actualizar',
    handler: function(){
    root.reload();
    },
    scope: this
    }]
    });

    And script.php have:

    function listar(){
    global $tablaCat;
    $sql = "SELECT * FROM ".$tablaCat;
    $result = mysql_query($sql);

    while($recCat = mysql_fetch_array($result, MYSQL_ASSOC)){

    $children = getFeeds($recCat['id']);
    if($children){
    $tmp['id'] = 'cat-'.$recCat['id'];
    $tmp['text'] = htmlentities($recCat['nombre']);
    $tmp['leaf'] = false;
    $tmp['cls'] = 'feeds-node';
    $tmp['iconCls'] = 'add-feed-cat';
    $tmp['children'] = $children;
    }else{
    $tmp['id'] = 'cat-'.$recCat['id'];
    $tmp['text'] = htmlentities($recCat['nombre']);
    $tmp['leaf'] = true;
    $tmp['cls'] = 'feeds-node';
    $tmp['iconCls'] = 'add-feed-cat';
    }

    $nodes = $tmp;
    }

    if (version_compare(PHP_VERSION,"5.2","<")){
    require_once("../../extjs/php/JSON.php");
    $json = new Services_JSON();
    $data=$json->encode($nodes);
    }else{
    $data = json_encode($nodes);
    }
    echo $data;
    }

    function getFeeds($id){
    global $tabla;
    $sql = "SELECT * FROM ".$tabla." WHERE id_cat = '".$id."'";
    $result = mysql_query($sql);

    while($rec = mysql_fetch_array($result, MYSQL_ASSOC)){
    $tmp['id'] = 'hijo-'.$rec['id'];
    $tmp['text'] = htmlentities($rec['text']);
    $tmp['url'] = $rec['url'];
    $tmp['leaf'] = true;
    $tmp['cls'] = 'feed';
    $tmp['iconCls'] = 'feed-icon';
    $nodes = $tmp;
    }

    return $nodes;
    }


    I can insert inside the root node, but in other node, give me an error.
    I tried with this:

    this.asd = Ext.getCmp('feed-tree').getRootNode().getNodeById('cat1').appendChi ld(
    new Ext.tree.TreeNode({
    text:'ASD',
    id:'asd',
    cls:'feeds-node',
    expanded:true
    })
    );
    Firebug returns:
    Ext.getCmp("feed-tree").getRootNode().getNodeById is not a function


  • Any ideas? :(


  • You're adding it to the root, what did you expect to happen? You need to add the node to the parent.


    var node = tree.getNodeById('cat1');
    node.appendChild(newNode);


  • You should check the documentation before posting. getNodeById is not a method on the TreeNode. http://extjs.com/deploy/ext-2.0.2/docs/?class=Ext.tree.TreeNode


  • Please, Is to easy to solve my problem!!! Nobody? :(


  • In other words...

    I have this tree:

    -root (AsyncTreeNode)
    ---Node1(id: cat1)
    -----Node2
    -----Node3
    ---Node4 (id: cat2)
    -----Node5
    -----Node6

    This is what i do:


    this.root.appendChild(node);
    ..returns

    -root (AsyncTreeNode)
    ---Node1(id: cat1)
    -----Node2
    -----Node3
    ---Node4 (id: cat2)
    -----Node5
    -----Node6
    ---NEW NODE

    This is what i want:

    -root (AsyncTreeNode)
    ---Node1(id: cat1)
    -----Node2
    -----Node3
    -----NEW NODE
    ---Node4 (id: cat2)
    -----Node5
    -----Node6







  • #If you have any other info about this subject , Please add it free.#
    Your name:
    E-mail:
    Telphone:

    Your comments:


    If you have any other info about Insert Node inside another Node , Please add it free.

    Catalogado bajo homelessguestb.comedit