twitter bootstrap autocomplete dropdown / combobox with Knockoutjs
Asked Answered
G

6

115

I have a requirement where I HAVE TO use bootstrap autocomplete dropdown, BUT user can have free form text in that dropdown if they wish. Before you think about TypeAhead, I could use Bootstrap TypeAhead textbox, but I need to have the dropdown becasue we want to give some default values as headstart options in case users dont know what to search for.

I am using this with MVC DropDownListFor as that creates a select control for us.

I found this article which does that for me.

https://github.com/danielfarrell/bootstrap-combobox/pull/20

All I had to do was take off the name from the select control and the control was letting me enter free form text. All good so far.

Now, I am using this in conjunction with Knockoutjs. I bind my options and selected value to the select control and then on row rendered of my template, I called (selector).combobox() which makes the select control a bootstrap comobobox and adds an input control and hides the select control in the scenes behind.

The problem now is when I try to get he values to post to server, since the value I put in input box is not a valid options from the options I gave to select control, it is always setting it to the first option by default. This is becasue, I set the binding of the selected value on select control and not on the input box which was created by bootstrap-combobox.js.

My question is how do I get the input box to data-bind to the same porperty as the the select control was bound to.

Any other options?? Let me know if you need more clarification or have questions. Please suggest.

Thanks.

Goyette answered 15/10, 2012 at 18:45 Comment(1)
I found a solution for my situation. I used TypeAhead text box instead of Autocomplete comobox and showed the options dropdown by default when users focused on the control or hit down button too. That way, they know what they can look for which was my primary requirement.Goyette
P
255

Have a look at Select2 for Bootstrap. It should be able to do everything you need.

Another good option is Selectize.js. It feels a bit more native to Bootstrap.

Precontract answered 16/10, 2012 at 12:31 Comment(5)
How did you use Select2 to allow text input that was not already included in the datasource?Bartie
As @Bartie correctly mentioned, Select2 DOES NOT ALLOW you to input anything that is not in the option list. Using (auto) tagging feature to simulate this is cumbersome, as it doesn't accept text with spaces inside.Diplomatist
@Bartie why don't you use the remote data source method?Kimes
Selectize.js not providing full sample code including html. Waste of time as you end up debugging their samples to find out why it doesn't work.Meilen
for selectize.js and bootstrap 4 I had to use css from github.com/const-se/selectize-bootstrap4-theme instead of the default css; then, when hitting backspace to clear the selection, sometimes the input field width was too small and the text was not visible when typing - I had to use this trick: github.com/selectize/selectize.js/issues/…Froissart
P
27

Does the basic HTML5 datalist work? It's clean and you don't have to play around with the messy third party code. W3SCHOOL tutorial

The MDN Documentation is very eloquent and features examples.

Pedestrianize answered 1/8, 2013 at 15:40 Comment(3)
The major problem with HTML 5 datalist is that it doesn't support any DOM events. So everytime you select a value, you have to tab out of the corresponding textboxShuster
@Shuster That is not true (anymore?). The input linked to the datalist fires change and inputs events, at least. Look here, both change and input events fire a console log.Oxpecker
Is this what you meant? tab out: (computing, graphical user interface) To press the Tab key and a modifier key to move between tabs or windows.Tomas
I
3

Select2 for Bootstrap 3 native plugin

https://fk.github.io/select2-bootstrap-css/index.html

this plugin uses select2 jquery plugin

nuget

PM> Install-Package Select2-Bootstrap

Insight answered 11/11, 2014 at 5:58 Comment(3)
Hello. Is there a difference with the first upvoted answer here? You duplicate the select 2 suggestion as far as I can see?Oxpecker
@FélixAdriyelGagnon-Grenier they belong to different repositories. github.com/select2/select2 and the one in this answer github.com/t0m/select2-bootstrap-css/tree/bootstrap3Tomas
@Tomas thanks. the latter seems to be a fork of the former however, and abandoned for the last 8 years.Oxpecker
D
1

Fuel UX combobox has all the features you would expect.

Diplomatist answered 14/8, 2013 at 13:9 Comment(1)
It does not have typeahead as far as I can see.Fixate
T
1

Can i suggest http://www.jqueryscript.net/form/Twitter-Like-Mentions-Auto-Suggesting-Plugin-with-jQuery-Bootstrap-Suggest.html, works more like the twitter post suggestion where it gives you a list of users or topics based on @ or # tags,

view demo here: http://www.jqueryscript.net/demo/Twitter-Like-Mentions-Auto-Suggesting-Plugin-with-jQuery-Bootstrap-Suggest/

in this one you can easily change the @ and # to anything you want

Tsunami answered 11/5, 2015 at 19:43 Comment(0)
S
0

Bootstrap Tokenfield seems good too: http://sliptree.github.io/bootstrap-tokenfield/

Sewing answered 21/1, 2016 at 18:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.