0

I have written a simple ExtJS grid with a column renderer that returns an HTML hyperlink for an onclick to call a simple JavaScript function.

Unfortunately, when I click it shows function undefined in the browser console. Any Help is Appreciated.

function myALert() {
    alert("yo");
}

function columnRenderer(val) {
    return '<a href="JavaScript:void(0);" onclick="myALert()">View</a>'
}
Ext.create('Ext.data.Store', {
    storeId: 'simpsonsStore',
    fields: ['name', 'email', 'phone'],
    data: [{
        name: 'Lisa',
        email: '[email protected]',
        phone: '555-111-1224'
    }, {
        name: 'Bart',
        email: '[email protected]',
        phone: '555-222-1234'
    }, {
        name: 'Homer',
        email: '[email protected]',
        phone: '555-222-1244'
    }, {
        name: 'Marge',
        email: '[email protected]',
        phone: '555-222-1254'
    }]
});

Ext.create('Ext.grid.Panel', {
    title: 'Simpsons',
    store: Ext.data.StoreManager.lookup('simpsonsStore'),
    columns: [{
        text: 'Name',
        dataIndex: 'name'
    }, {
        text: 'Email',
        dataIndex: 'email',
        flex: 1
    }, {
        text: 'Phone',
        dataIndex: 'phone',
        renderer: columnRenderer
    }],
    height: 200,
    width: 400,
    renderTo: Ext.getBody()
});
2
  • Is that function global.By global I mean that when your page loads is that function initialized.If its global then you can check in this way: just type name of function in browser console & there function gets displayed.If function is not initialized then it prints undefined. So check this and reply. Commented Sep 26, 2018 at 12:32
  • console shows myALert is not defined at HTMLAnchorElement.onclick Found this link: stackoverflow.com/questions/17378199/… Can someone help me in how this can be incorporated in my situation? Commented Sep 26, 2018 at 13:04

1 Answer 1

2

The use of global functions in this case where it will be used in only one column is not recommended.

I suggest using Action Column, which has the handler property that performs a function on the click. See this documentation here.

If it is necessary to use a link (tag a), I suggest using Template Column, where you can create the template you prefer using HTML markup and other resources. See this documentation here.

Take a look in this forked fiddle from Akrion. There is one grid with Action Column and another with Template Column.

Ext.application({
    name: 'Fiddle',
    launch: function () {
        Ext.create('Ext.data.Store', {
            storeId: 'simpsonsStore',
            fields: ['name', 'email', 'phone'],
            data: [{
                name: 'Lisa',
                email: '[email protected]',
                phone: '555-111-1224'
            }, {
                name: 'Bart',
                email: '[email protected]',
                phone: '555-222-1234'
            }, {
                name: 'Homer',
                email: '[email protected]',
                phone: '555-222-1244'
            }, {
                name: 'Marge',
                email: '[email protected]',
                phone: '555-222-1254'
            }]
        });

        Ext.create('Ext.grid.Panel', {
            title: 'Simpsons (Grid with Action Column)',
            store: Ext.data.StoreManager.lookup('simpsonsStore'),
            columns: [{
                text: 'Name',
                dataIndex: 'name'
            }, {
                text: 'Email',
                dataIndex: 'email',
                flex: 1
            }, {
                xtype: 'actioncolumn',
                text: 'Phone',
                dataIndex: 'phone',
                align: 'center',
                icon: 'https://cdn2.iconfinder.com/data/icons/ledicons/eye.png',
                getTip: function(value) {
                    return value;
                },
                handler: function(grid, rowIndex, colIndex, item, e, record) {
                    alert("Yo! " + record.get('phone'));
                }
            }],
            height: 200,
            width: 400,
            renderTo: Ext.getBody()
        });

        Ext.create('Ext.grid.Panel', {
            title: 'Simpsons (Grid with Template Column)',
            store: Ext.data.StoreManager.lookup('simpsonsStore'),
            columns: [{
                text: 'Name',
                dataIndex: 'name'
            }, {
                text: 'Email',
                dataIndex: 'email',
                flex: 1
            }, {
                xtype: 'templatecolumn',
                text: 'Phone',
                dataIndex: 'phone',
                align: 'center',
                tpl: new Ext.XTemplate(
                    '<a href="{[this.myAlert(values)]}">{phone}</a>',
                    {
                        myAlert: function(values){
                           return "javascript:alert('Yo! "+values.phone+"')";
                        }
                    }
                )
            }],
            height: 200,
            width: 400,
            renderTo: Ext.getBody()
        });
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

This is a much better way to go.
yeah this is the best thing I guess. Only one more requirement that I have is that the button should look like a hyperlink. Is that possible?
You can do it through css.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.