I use this:

                <input type="text" class="span2" data-
provide="typeahead" data-items="10" autocomplete="off" data-input-
type="N*">

                <script>
                var autocomplete = $('input[data-
provide="typeahead"]').typeahead()
                .on('keyup', function(ev){
                    $(this)
                    ev.stopPropagation();
                    ev.preventDefault();
                    if( $.inArray(ev.keyCode,[40,38,9,13,27]) === -1 )
{
                        var self = $(this);
                        self.data('typeahead').source = [];
                        if( !self.data('active') && self.val().length
> 0){
                            self.data('active', true);
                            $.ajax({
                                type:'post',
                                url: [url to your json data source],
                                data: [post paramters], /* ie {'TYPE':$
(this).attr('data-input-type'), 'VAR2','test2'} */
                                dataType: 'json',
                                cache: false,
                                success: function(data) {
                                    self.data('active',true);
                                    var arr = data; [],
                                    i=data.length;
                                    while(i--){
                                        arr[i] = data[i];
                                    }
                                    self.data('typeahead').source =
arr;
                                    self.trigger('keyup');
                                    self.data('active', false);
                                },
                                error: function(jqXHR, textStatus,
errorThrown) {
                                }
                            });
                            self.data('active', false);
                        }
                    }
                });
               </script>

additionally I add data-input-type="N*" to my input tag which is the
parameter I pass to the json php file that pulls the proper results,
then i use $(this).attr("data-input-type") to use it as a parameter in
the data section of the $.ajax that way this works for all typeahead
inputs across the entire app without having to check for each specific
select

On Feb 6, 8:48 am, Ric <[email protected]> wrote:
> hi does anyone know hot to connect a typeahead input to an external
> data source?
>
> i have an url with json data...

Reply via email to