Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap typeahead results into a div, possible?

I'm trying to fit typeahead results into a particular div on my page. I get the JSON callback data but I don't know how to use it in order to populate a particular div. The process function has the only effect of listing the results, whatever the length it takes, just under the search field.

Here is my code, do you know how to exploit the callback data in order to populate a particular div ?

$('#search').typeahead({
          source: function(query, process) {
              $.ajax({
                  url: '/action/search.php?action=autocomplete',
                  type: 'POST',
                  data: 'query=' + query,
                  dataType: 'JSON',
                  async: true,
                  success: function(data) {
                      //process(data);
                  },
                  minLength: 1
              });
          }
    });
like image 769
Xavier C. Avatar asked Feb 20 '13 16:02

Xavier C.


People also ask

What is typeahead bootstrap?

Typeahead. A basic, easily extended plugin for quickly creating elegant typeaheads with any form text input.

How do I get typeahead selected value?

typeahead({ itemSelected:function(data,value,text){ console. log(data) alert('value is'+value); alert('text is'+text); }, //your other stuffs }); You have to just pass itemSelected in the callback function and it will give you the selected item. Hope this will work for you.

What is type ahead functionality?

Typeahead is a feature of computers and software (and some typewriters) that enables users to continue typing regardless of program or computer operation—the user may type in whatever speed is desired, and if the receiving software is busy at the time it will be called to handle this later.

What is typeahead in angular?

A typeahead example that uses a custom template for results display, an object as the model, and the highlight directive to highlight the term inside the custom template.


1 Answers

There is actually a really simple way to get the results into a specific page element, however, I'm not sure it's actually documented.

Searching through the source code shows that the option menu can be passed in, which seems to be intended to allow you to define what the wrapping menu element will look like, however, you can pass in a selector, and it will use this as the target.

Given the html fragment:

<input id='#typeahead' type='text'>

<h2>Results</h2>
<ul id="typeahead-target"></ul>

You could use the following to get the results to appear within the ul element:

$('#typeahead').typeahead({menu: '#typeahead-target'});
like image 97
Matthew Schinckel Avatar answered Sep 17 '22 23:09

Matthew Schinckel