9

I am migrating my app to twitter-bootstrap and i would like to replace my jquery-ui autocomplete with typeahead.js.

It would be better to use the feature embedded in twitter-bootstrap but i am ok with the extra typeahead plugin if necessary.

My problem is that i have trouble reproducing the current behaviour especially with the absence of results.

How would you do something like that?

$("#search").autocomplete({
           source : myUrl,
            delay : 100,
            minLength : 2,
            select : function(event, ui) {
              // do whatever i want with the selected item
            },

            response : function(event, ui) {
                if (ui.content.length === 0) {
                    ui.content.push({
                        label : "No result",
                        value : customValue
                    });
                }
            }
        });

Basically, if there is no result, i want to display a custom message in the component.

Thanks!

1

2 Answers 2

5

The migration to Bootstrap typeahead would look something like..

$('.typeahead').typeahead({
  minLength:2,
  updater: function (item) {
     /* do whatever you want with the selected item */

    },
  source: function (typeahead, query) {
     /* put your ajax call here..
     return $.get('/typeahead', { query: query }, function (data) {
        return typeahead.process(data);
     });
     */      
    }
});

EDIT:

I've updated the demo to include a sorter and highlighter. I think this will get you the results you want..

  sorter: function(items) {
    if (items.length == 0) {
        var noResult = new Object();
        items.push(noResult);
    }
    return items;    
    },
  highlighter: function(item) {
    if (dataSource.indexOf(item) == -1) {
        return "<span>No Match Found.</span>";
    }
    else {
       return "<span>"+item+"</span>";
    }
  },

Bootstrap Typeahead Demo

I don't think the typeahead has an equivalent to delay, but there are some jquery workarounds for this.

Sign up to request clarification or add additional context in comments.

3 Comments

As I've thought about this more, I realized that this answer will not suffice. Getting the typeahead to display "no matches" is a whole different problem and I'm not sure if Bootstrap typeahead.js will work. There may be a way through "matches" function.
I worked with it some more and edited my answer.. check out the demo again: bootply.com/61459
thx you for the edit ;) I'll try to follow your lead as soon as possible.
4

With the latest version of Typeahead.js (0.10) it is now possible to specify an empty template to display when no results are found.

   $('.typeahead').typeahead({
     hint: true,
     highlight: true,
     minLength: 2
    },{
      name: 'examples',
      source: examples.ttAdapter(),
      templates: {
        empty: [
          '<div class="empty-message">',
          'unable to find any results that match the current query',
          '</div>'
        ].join('\n')
      }
    });

Comments

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.