Twitter Bootstrap: Typeahead function for no matches
Asked Answered
S

2

6

I have an 'add new thing' form that associates a 'user' to a 'thing'.

I'm using Twitter Bootstrap Typeahead to give a list of users as I type.
It works just fine, if a user is in the list, I can select it.

But if the user doesn't exist, I'd like to make a 'create new user' link appear.

So basically, I need some form of 'no matches' function somewhere in the .typeahead(). I can't work it out.

Synchronic answered 15/2, 2013 at 19:7 Comment(0)
G
4

Extend the the typeahead class:

var newusertext = 'New user';
var newuserlink = '/add/user/0';

$.fn.typeahead.Constructor.prototype.process = function (items) {
      var that = this

      items = $.grep(items, function (item) {
        return that.matcher(item)
      })

      items = this.sorter(items)

      if (!items.length) {
         return this.rendernonmatch().show()
      }

      return this.render(items.slice(0, this.options.items)).show()
    } 


$.fn.typeahead.Constructor.prototype.rendernonmatch = function () {
      var that = this
      var items = new Array();
      items.push(newusertext)
      items = $(items).map(function (i, item) {
        i = $(that.options.item).attr('data-value', item)
        i.find('a').html(that.highlighter(item))
        return i[0]
      })
      items.first().addClass('active')
      this.$menu.html(items)
      return this
    }

/* redirect to the registration page to add a new user */    
$.fn.typeahead.Constructor.prototype.updater = function (item) {
      if(item === newusertext){ window.location =  newuserlink; return}
      return item
    }      

See: http://bootply.com/66630

Goshawk answered 7/7, 2013 at 13:6 Comment(0)
T
0

I used the typeahead notFound 'template' option, from there i could set a link button which for me is more accesible. Typeahead settings:

$('#remote .typeahead').typeahead({
    hint: true,
    highlight: true,
    minLength: 2
  },
  {
    name: 'sn-tags',
    display: 'tag',
    source: oBusqTags, //Bloodhound object
    templates: {
      notFound: '<p>No matches<br><a id="btnAddNewTag" href="#">Add New Tag</a></p>'
    }
}).bind("typeahead:select", function(obj, datum, name) {
  //add selection as an option on a select element
  $('#lbxAddTag').append($('<option>', {
    value: datum.id,
    text: datum.tag
  }));
});

This is the listener for that link:

$('#remote').on('click', '#btnAddNewTag', function(e){
    $('#lbxAddTag').append($('<option>', {
        value: "0",
        // this is tricky, as there aren't any matches then 
        // $('#remote .typeahead').typeahead('val') contains '', 
        // so i had to set an id for the typeahead input text box and 
        // 'manually' get its value
        text: $('#tbxBusqTag').val()
    }));
    // as the text is already added as a select option
    // i clean the typeahead input box
    $('#remote .typeahead').typeahead('val','');
    e.preventDefault();
});
Tonga answered 12/5, 2017 at 9:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.