typeahead.js icon indicating copy to clipboard operation
typeahead.js copied to clipboard

Example of typeahead:selected event

Open andy3rdworld opened this issue 13 years ago • 41 comments

For anyone interested until an example is added to documentation for custom events...

Assuming there's an input on your page called 'search_input':


$('#search_input').bind('typeahead:selected', function(obj, datum, name) {      
        alert(JSON.stringify(obj)); // object
        // outputs, e.g., {"type":"typeahead:selected","timeStamp":1371822938628,"jQuery19105037956037711017":true,"isTrigger":true,"namespace":"","namespace_re":null,"target":{"jQuery19105037956037711017":46},"delegateTarget":{"jQuery19105037956037711017":46},"currentTarget":
        alert(JSON.stringify(datum)); // contains datum value, tokens and custom fields
        // outputs, e.g., {"redirect_url":"http://localhost/test/topic/test_topic","image_url":"http://localhost/test/upload/images/t_FWnYhhqd.jpg","description":"A test description","value":"A test value","tokens":["A","test","value"]}
        // in this case I created custom fields called 'redirect_url', 'image_url', 'description'   

        alert(JSON.stringify(name)); // contains dataset name
        // outputs, e.g., "my_dataset"

});

https://github.com/twitter/typeahead.js#custom-events

andy3rdworld avatar Jun 21 '13 14:06 andy3rdworld

What would be even better would be a pull request that added this to the README :smile: see #174.

jharding avatar Jun 26 '13 01:06 jharding

Agreed, this should be in the README

Cahl-Dee avatar Aug 08 '13 02:08 Cahl-Dee

Yup, please add this to the documentation.

aDaneInSpain avatar Sep 10 '13 08:09 aDaneInSpain

Thanks, been looking for this :)

digz6666 avatar Sep 11 '13 10:09 digz6666

Yea, thanks, found this via google after finding docs insufficient.

missinglink avatar Sep 12 '13 15:09 missinglink

Nice.... why this is not in the documentation yet?

brunohnq avatar Oct 22 '13 15:10 brunohnq

Wow, been fighting with this thing, thanks A LOT for the info!

:+1: to add it to the readme

pouipouidesign avatar Oct 24 '13 10:10 pouipouidesign

@pouipouidesign I've forgotten what this was about aready ;) Can you please spend 5 mins to edit the README & any examples and do a PR?

missinglink avatar Oct 24 '13 11:10 missinglink

I will, but over this w-e as I'm in the middle of a sprint of a sort (hence my relief at findings this). I'll just have to pester my husband for info about doing a PR, as I've never done it before :)

pouipouidesign avatar Oct 24 '13 11:10 pouipouidesign

I understand that when jharding and I first commented this example didn't have enough attention to make its way to the README, but it's about time it does so, no?

Cahl-Dee avatar Oct 24 '13 12:10 Cahl-Dee

@pouipouidesign I'd be happy to help, I just forgot about the context & don't feel I could correctly represent the issue any more. If you need any help with the PR let me know. I'm insertcoffee on twitter.

Alternatively can people put some info in the comments here and I will compile it in to a PR.

missinglink avatar Oct 24 '13 12:10 missinglink

Lot of things will be appreciated in the README and in the exemples like valueKey or tokens usage

etiennechabert avatar Oct 27 '13 18:10 etiennechabert

There is one more parameter in callback function - dataset name. Can you add it to your example?

antimony avatar Oct 31 '13 14:10 antimony

Thanks so much! Just what I needed.

ptoly avatar Oct 31 '13 19:10 ptoly

@antimony Added dataset name parameter, thanks.

andy3rdworld avatar Nov 02 '13 19:11 andy3rdworld

I haven't forgotten to do this, just been swamped. Have good hope of doing it this w-e. Sorry for the delay. If anyone wants to do it faster, please do :)

pouipouidesign avatar Nov 06 '13 10:11 pouipouidesign

As a note, I have not been able to find documentation on this and had to add a .off('blur') to the stack to make sure the value does not revert to the selected value.

 $('.user-typeahead').typeahead({
    remote: '/ajax_user_typeahead?query=%QUERY''
}).bind('typeahead:selected', function(obj, selected, name) {
    if (selected.value.toString().split('-').length > 1) { /* "name - [email protected]" */
        $(this).val(selected.value.toString().split('-')[0].replace(/^\s+|\s+$/g, '')); /* split, select, trim */
    }
    return false; /* didn't do anything, maybe not necessary */
}).off('blur');

wallter avatar Feb 11 '14 01:02 wallter

@wallter if you went to set the value of the typeahead input control, use jQuery#typeahead('val', yourVal). See https://github.com/twitter/typeahead.js/blob/master/doc/jquery_typeahead.md#jquerytypeaheadval-val

jharding avatar Feb 11 '14 01:02 jharding

I'm trying to get the val of the typeahead instance on selected but I keep getting undefined

$(scope.conf.field).on('typeahead:selected' , function(e,s,d)
{ console.log($(scope.conf.field).typeahead('val'); });

This keeps returning undefined. Anything else inside that event happens fine, just returns undefined for the value of typeahead. Not sure what I've done wrong...

rohan-deshpande avatar Feb 26 '14 03:02 rohan-deshpande

Any reason why you're trying to get the value that way rather than getting it from the suggestion object passed to the event handler i.e. the s argument in your sample code?

jharding avatar Mar 07 '14 06:03 jharding