Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extjs how to make a nested child using xTemplate when we don't know how deep is it?

Tags:

extjs

first, sorry if my english bad,....

in my script, variable tplData below is dynamic,... (lets say it generates from database)
so, every chid, can have another child. and so on,.... now, i'm stack how to iteration it,..

var tplData = [{
            name  : 'Naomi White'
        },{
            name : 'Yoko Ono'
        },{
            name : 'John Smith',
            child : [{
                    name:'Michael (John\'s son)',
                    child: [{
                            name : 'Brad (Michael\'s son,John\'s grand son)'
                    },{
                            name : 'Brid (Michael\'s son,John\'s grand son)',
                            child: [{
                                name:'Buddy (Brid\'s son,Michael\'s grand son)'
                            }]
                    },{
                        name : 'Brud (Michael\'s son,John\'s grand son)'
                    }]
                }]
        }];

        var myTpl = new Ext.XTemplate(
            '<tpl for=".">',
                '<div style="background-color: {color}; margin: 10px;">',
                    '<b> Name :</b> {name}<br />',
                        // how to make this over and over every child (while it has )
                        '<tpl if="typeof child !=\'undefined\'">',
                            '<b> Child : </b>',
                                '<tpl for="child">',
                                '{name} <br />',
                            '</tpl>',
                       '</tpl>',
                       ///////////////////////////////////////
                '</div>',
             '</tpl>'
        );
        myTpl.compile();

        myTpl.overwrite(document.body, tplData);
like image 218
Egy Mohammad Erdin Avatar asked Nov 29 '25 21:11

Egy Mohammad Erdin


1 Answers

Something like this:

    var myTpl = new Ext.XTemplate(
        '<tpl for=".">',
            '<div style="background-color: {color}; margin: 10px;">',
                '<b> Name :</b> {name}<br />',
                    '<tpl if="typeof child !=\'undefined\'">',
                        '<b> Child : </b>',
                          '{[ this.recurse(values) ]}',
                        '</tpl>',
                   '</tpl>',
            '</div>',
         '</tpl>',
      {
        recurse: function(values) {
          return this.apply(values.child);
        }
      }
    );

Untested since Ext Core doesn't include XTemplate and that is what can be included in jsbin and jsfiddle

I'm sure the output is nowhere near what you need, but it shows how to recurse. You may need to break your template into two to get the output you are going for. Check out this slide show for more: http://www.slideshare.net/senchainc/advanced-templates-5971877

like image 198
Hemlock Avatar answered Dec 01 '25 22:12

Hemlock