Search code examples
templatesextjsextjs5

open a url in a displayfield list template


In the example if I click on any of the template items the click event is triggered and always open a window and the url is loaded.

I need build a condition to select only the url; and only when the url is clicked the window should open and load de url .

var cars = 'car - Fiat, country - Italy, site - https://www.sencha.com/';

Ext.create('Ext.form.Panel', {
    renderTo: Ext.getBody(),
    width: 450,
    height: 200,
    bodyPadding: 10,
    title: 'Template',
    items: [{
        xtype: 'displayfield',
        fieldLabel: 'Cars',
        name: 'cars',
        renderer: function (value, field) {
                if (value && value.indexOf(',') > -1) {
                    this.rndTpl = this.rndTpl || new Ext.XTemplate('<div style = "line-height: 150%;' +
                        'margin-left: -25px; margin-top: -12px; cursor: pointer;">' +
                        '<ul><li>{[values.cars.replace(/,/g, "<li/>")]}</li></ul>' +
                    '</div>');

                return this.rndTpl.apply({
                    cars: value
                });
            } else {
                return value;
            }
        },
        listeners: {
            render: function(field, eOpts) {
                field.setValue('car - Fiat, country - Italy, site - https://www.sencha.com/')
            },
            change: function(field){
                   var stringValues = field.value;

                   if(stringValues && (stringValues !== '' ||  stringValues !== null)){

                       var arrayValues = stringValues.split(',');

                       arrayValues.map(function(item){
                         var typeValue =  item.indexOf('site');

                        if(typeValue > -1){
                             var value = item.substring(item.indexOf('-') + 1);

                            field.getEl().on('click', function () {
                                 window.open(value, '', false);
                            });

                        }else{
                            return false;
                        }
                    });
                }
           }
         }
     }]

  });

FIDDLE: https://fiddle.sencha.com/#view/editor&fiddle/2ii0


Solution

  • So i forked your fiddle and changed the click event function. I'm taking the content of the clicked element and checking if it is an url, here's the FIDDLE