1. #1
    Sencha User
    Join Date
    May 2011
    Posts
    39
    Vote Rating
    0
    rTankersley is on a distinguished road

      0  

    Default Answered: Listener on a hyperlink

    Answered: Listener on a hyperlink


    Hey everybody,

    I'm creating something like a forum. The list of threads is in a grid. I want the user to be able to click on the title (not the cell the title is in, just the title) to open up the list of responses for that thread. The list of responses are on another card (the panel is in card layout).

    So it's similar to a hyperlink, and if there is a way to attach listeners to those that'd be great. But if you know another way, that's fine too.

    Thanks for your help

  2. You can use a column renderer and return a wrapped text for the title:

    Code:
    renderer: function(title) {
        return '<span class="thread-title">' + title + '</span>';
    }
    Now, you can add a listener to the grid for the click event on that <span> tag. There are two ways to do this:

    Code:
    Ext.create('Ext.grid.Panel', {
        //....
    
        listeners : {
            el : {
                delegate : 'span.thread-title',
                click    : function(e) {
                    var view = grid.getView(),
                        el   = e.getTarget('tr', null, true),
                        rec  = view.getRecord(el);
                }
            }
        }
    });
    or

    Code:
    grid.mon(grid.el, {
        delegate : 'span.thread-title',
        click    : function(e) {
            var view = grid.getView(),
                el   = e.getTarget('tr', null, true),
                rec  = view.getRecord(el);
        }
    });
    Now, only when you click on the <span class="thread-title"> will this fire and you have the record in the rec variable.

    To finish it off, I would style the <span> a little... I would color it and add a cursor:

    Code:
    span.thread-title {
        color  : #00f;
        cursor : pointer;
    }

  3. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,330
    Answers
    3541
    Vote Rating
    847
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    You can use a column renderer and return a wrapped text for the title:

    Code:
    renderer: function(title) {
        return '<span class="thread-title">' + title + '</span>';
    }
    Now, you can add a listener to the grid for the click event on that <span> tag. There are two ways to do this:

    Code:
    Ext.create('Ext.grid.Panel', {
        //....
    
        listeners : {
            el : {
                delegate : 'span.thread-title',
                click    : function(e) {
                    var view = grid.getView(),
                        el   = e.getTarget('tr', null, true),
                        rec  = view.getRecord(el);
                }
            }
        }
    });
    or

    Code:
    grid.mon(grid.el, {
        delegate : 'span.thread-title',
        click    : function(e) {
            var view = grid.getView(),
                el   = e.getTarget('tr', null, true),
                rec  = view.getRecord(el);
        }
    });
    Now, only when you click on the <span class="thread-title"> will this fire and you have the record in the rec variable.

    To finish it off, I would style the <span> a little... I would color it and add a cursor:

    Code:
    span.thread-title {
        color  : #00f;
        cursor : pointer;
    }
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  4. #3
    Sencha User
    Join Date
    May 2011
    Posts
    39
    Vote Rating
    0
    rTankersley is on a distinguished road

      0  

    Default


    That worked perfectly! Thanks a lot.

Thread Participants: 1