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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With