简体   繁体   中英

Is it possible to combine two Ext.XTemplate together?

I have an xtype that accepts two Ext.XTemplates (one required & one optional) that will be applied to a set of values. Unfortunately I'm unable to achieve this as the last template run it will overwrite the values. Is it possible to append an XTemplate into another, or make it run on a set of values one after another without destroying what previous templates applied?

Here's my Ext.grid.Column extended class:

LinkColumn = Ext.extend(Ext.grid.Column,{

    href: '',
    additionalTemplate: '',
    forceApplyTemplate:false,
    dateFormat: 'm/d/y H:i',

    constructor: function(config){
        Ext.QuickTips.init();

        var tpl = new Ext.XTemplate(
            '<tpl if="this.hasValue('+config.dataIndex+') == true">',
                '<tpl if="this.isDate('+config.dataIndex+') == false">',
                    '<a href="'+config.href+'">{'+config.dataIndex+'}</a>',
                '</tpl>',
                '<tpl if="this.isDate('+config.dataIndex+') == true">',
                    '<a href="'+config.href+'">{[fm.date(values.'+config.dataIndex+',"'+this.dateFormat+'")]}</a>',
                '</tpl>',
            '</tpl>',
            {
                compiled: true,
                hasValue: function(value){
                    return !Ext.isEmpty(value);
                },
                isDate: function(value){
                    return Ext.isDate(value);
                }
            }
        );

        LinkColumn.superclass.constructor.call(this,config);
        if (Ext.isEmpty(this.href)) { throw new Ext.Error('LinkColumn: href property is required') };

        this.renderer = function(value, p, r){
            if (!Ext.isEmpty(this.additionalTemplate)){
                var moreTemplate = (!Ext.isPrimitive(this.additionalTemplate) && this.additionalTemplate.compile) ? this.additionalTemplate : new Ext.XTemplate(this.additionalTemplate);
                value = (!Ext.isEmpty(moreTemplate)) ? moreTemplate.apply(r.data) : value;
                value = moreTemplate.apply(r.data);
            };
            //here where value gets overwritten:
            value = tpl.apply(r.data);

            return value;

        };
    },
});

Value is the HTML string returned by the template.apply, correct? Can you not simply do:

value += tpl.apply(r.data);

Which would add the results of the second template to the results of the first?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM