admin管理员组

文章数量:1406942

Looking around I found no solution for Sencha Touch (while I managed to get this working with ExtJS) so I am turning to you.

New to Sencha touch I learned how to correctly bind a stor to a List ponent.

My problem is that I need to work on a particular record before rending it to template.

My record coordinate is like this :

2.356095,48.879154,0.000000

With ExtJS I created a function that split it and render a link to GMaps.

My Questions :

Can I access my record as in extJS (record.data.coordinates) ?

How can I add new variables to use in itemTpl ?

MyAPP = new Ext.Application({
    name: 'ListDemo',
    launch: function() {

        MyAPP.listPanel = new Ext.List({
            id: 'indexlist',
            store: MyAPP.ListStore,
            itemTpl: '<div>{name}<br>{coordinates}</div>'

        }); // end of MyAPP.listPanel

        function renderMap(value, p, record) {
            var split = record.data.coordinates.split(',');
            var lat = split[0];
            var lon = split[1];
            return Ext.String.format(
                '<b><a href="={2}+{1}+({3})" target="_blank">Google Map</a>',
                value,
                lat,
                lon,
                record.data.Adresse
            );
        }
    }
});

Thanks for your help,

Julius.

Looking around I found no solution for Sencha Touch (while I managed to get this working with ExtJS) so I am turning to you.

New to Sencha touch I learned how to correctly bind a stor to a List ponent.

My problem is that I need to work on a particular record before rending it to template.

My record coordinate is like this :

2.356095,48.879154,0.000000

With ExtJS I created a function that split it and render a link to GMaps.

My Questions :

Can I access my record as in extJS (record.data.coordinates) ?

How can I add new variables to use in itemTpl ?

MyAPP = new Ext.Application({
    name: 'ListDemo',
    launch: function() {

        MyAPP.listPanel = new Ext.List({
            id: 'indexlist',
            store: MyAPP.ListStore,
            itemTpl: '<div>{name}<br>{coordinates}</div>'

        }); // end of MyAPP.listPanel

        function renderMap(value, p, record) {
            var split = record.data.coordinates.split(',');
            var lat = split[0];
            var lon = split[1];
            return Ext.String.format(
                '<b><a href="http://maps.google./maps?q={2}+{1}+({3})" target="_blank">Google Map</a>',
                value,
                lat,
                lon,
                record.data.Adresse
            );
        }
    }
});

Thanks for your help,

Julius.

Share Improve this question edited Apr 14, 2018 at 6:29 Narendra Jadhav 10.3k15 gold badges35 silver badges44 bronze badges asked Nov 16, 2011 at 15:56 MathieuMathieu 1,08212 silver badges23 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You can have inline javascript code in your template that will split the coordinates variable. Here is example from the docs:

var tpl = new Ext.XTemplate(
    '<p>Name: {name}</p>',
    '<p>Company: {[values.pany.toUpperCase() + ", " + values.title]}</p>',
    '<p>Kids: ',
    '<tpl for="kids">',
        '<div class="{[xindex % 2 === 0 ? "even" : "odd"]}">',
        '{name}',
        '</div>',
    '</tpl></p>'
 );

Notice how the values.pany.toUpperCase() is handled. So to get to your variable you can do values.coordinates.

Another solution is to have a template member function. Here is another example from the sencha docs.

var tpl = new Ext.XTemplate(
    '<p>Name: {name}</p>',
    '<p>Kids: ',
    '<tpl for="kids">',
        '<tpl if="this.isGirl(name)">',
            '<p>Girl: {name} - {age}</p>',
        '</tpl>',
         // use opposite if statement to simulate 'else' processing:
        '<tpl if="this.isGirl(name) == false">',
            '<p>Boy: {name} - {age}</p>',
        '</tpl>',
        '<tpl if="this.isBaby(age)">',
            '<p>{name} is a baby!</p>',
        '</tpl>',
    '</tpl></p>',
    {
        // XTemplate configuration:
        piled: true,
        // member functions:
        isGirl: function(name){
           return name == 'Sara Grace';
        },
        isBaby: function(age){
           return age < 1;
        }
    }
);

Check the docs here http://docs.sencha./touch/1-1/#!/api/Ext.XTemplate if you need more help.

Define the tpl object before this code.

 MyAPP.listPanel = new Ext.List({
        id: 'indexlist',
        store: MyAPP.ListStore,
        itemTpl: tpl  // use the tpl object like this

    }); 

Because you mentioned you know how to make it work in ExtJS, I assume that you know the functionality of Ext.XTemplate. So, for itemTpl just use XTemplate. Something like this:

var tpl = new Ext.XTemplate('<div>{name}<br>{coordinates}</div>', {
               // XTemplate methods here
          });

And use this tpl in itemTPl:

MyAPP.listPanel = new Ext.List({
        id: 'indexlist',
        store: MyAPP.ListStore,
        itemTpl: tpl
    });

本文标签: javascriptsencha touch add variables to templatesList etcStack Overflow