What is the best practice to scale a select/dropdown - javascript

I have a long list (of an undetermined length, that can keep on growing) of values. For example:
<select>
<option value="1">1, some test</option>
<option value="2">2, some text</option>
<option value="3">3, some text</option>
<option value="4">4, some text</option>
<option ...... add 100 more options> ..
</select>
What would be the best practice/user friendly way to support such a long list? I imagine that a user might find it frustrating to scroll through a super long list.
I am trying to minimize the user's scroll time, by presenting the most recent values at the top of the drop down, but if she/he would like to go to the first element in the drop down it would require a lot of scrolling.
Thanks

Instead of using <select>, how about changing it to HTML5 <datalist>, which is like this:
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
By the way, you can type the first letter using the <select>, so my suggestion will be sort your data according to the first letter. Because it will help the user to go to the sub-option directly.

Maybe add a searchfield to the dropdown?
Something like this: http://jsearchdropdown.sourceforge.net/

What kind of data would you like to display ? Maybe there is another way than select to display them ? In fact, for dozens of items, select is not the best choice, unless you use by example a JQuery plugin like jsearchdropdown and in this case the user must know what he's looking for.

Related

Edit-text with Drop-down box in HTML5

I want edit text with a drop down box along with custom scrollbar so end user can add data explicitly or can add from drop down box. The final entered value by end user should be saved.I want this in UI development preferably angular js.
Well from what i understood , you need a text input as well as a dropdown input together.
To be frank , this is quite easy and i dont understand what caught you up in this.
So here is the code:
<input type="text"/>
<select style="width:20px;">
<option value=""></option>
<option value="banana">Banana</option>
<option value="apple">Apple</option>
<option value="orange">Orange</option>
</select>
Check out the jsfiddle here:
https://jsfiddle.net/kna3fj5t/

Dojo: How to disable a dijit.form.FilteringSelect option

I am trying to disable option items in a dijit/Form/FilteringSelect. Here is code
<select id="filtSelect" dojoType="dijit.form.FilteringSelect">
<option disabled="disabled">Select</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
Please help me.
You can do it this way: ( Vers. 1.9 )
<select data-dojo-type="dijit/form/" id="count" name="count">
<option disabled="disabled">Select</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
Here's the full fiddle for my Example above(Edited by Thomas Upton): http://jsfiddle.net/tupton/266C4/
and here the reference from dojo: http://dojotoolkit.org/reference-guide/1.9/dijit/form/FilteringSelect.html?highlight=filteringselect
In the 1.6 Vers it must look like:
<script>
dojo.require("dijit.form.FilteringSelect");
</script>
<body class="claro">
<select dojoType="dijit.form.FilteringSelect" id="fruit" name="fruit">
<option value="AP" disabled>
Apples
</option>
<option value="OR" selected>
Oranges
</option>
<option value="PE">
Pears
</option>
</select>
</body>
Here the fiddle for this Version: http://jsfiddle.net/Q4zw6/
It's important that you load the instance of dijit.form.filteringSelect to use it.
Regards, Miriam
I don't believe you can disable options with dijit/form/FilteringSelect because it is store-based and is supposed to let a user enter any text.
There is a property called displayedValue, which you can use to set whatever you want. However, with FilteringSelect, any text that isn't an option is marked as erroneous input, as seen by the following code and this jsfiddle.
<select data-dojo-type="dijit/form/FilteringSelect" data-dojo-props="displayedValue: 'Select'" id="count" name="count">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
You could use a dijit/form/ComboBox in exactly the same manner; the difference between FilteringSelect and ComboBox is that the latter allows any input. See the documentation for more information.
The issue is that FilteringSelect relies on dojo data store, as others have pointed out. So if you don't create that manually, it will happen behind the scenes and you just won't know how to reference it. But on second thought, looking at the API I notice FilteringSelect has a property named store.
So, you either need to create the data store yourself & initialize the FilteringSelect with it (http://dojotoolkit.org/reference-guide/1.9/dijit/form/FilteringSelect.html) or retrieve it by using dijit's byId and then accessing the "store" property.
Then, manipulate that data store. Maybe store a temporary copy before you remove the option if you want to restore it after. If you look up data store I'm sure removing an option is trivial. Sometimes with widgets it happens that you need to trigger a re-rendering method also to have it update after (you can usually find some method in the widget's API for this, sometimes "reset"), though I believe with stores it may watch them more intelligently.

Move selected items to the top of sorted multiple select (Javascript)

I'm using jQueryUI multi-select Widget that draws a nice dropdown menu widget for multi-selects.
I have an already sorted (on username) multiselect like that:
<select multiple="multiple" name="users" id="id_users">
<option value="1" selected="selected">aaa</option>
<option value="44">bbb</option>
<option value="21" selected="selected">ccc</option>
<option value="50">ddd</option>
<option value="16">eee</option>
<option value="25" selected="selected">fff</option>
</select>
And I want to keep the username sorting, but moving the selected items to the top of the list, everytime the user opens the drop down menu. So in this example the new order must be:
<select multiple="multiple" name="users" id="id_users">
<option value="1" selected="selected">aaa</option>
<option value="21" selected="selected">ccc</option>
<option value="25" selected="selected">fff</option>
<option value="44">bbb</option>
<option value="50">ddd</option>
<option value="16">eee</option>
</select>
From the website of the widget, I see that it has a method that returns an array of selected items, and it has an event beforeopen. So I think I can manage the sorting (using that array) by handling the event beforeopen, but I'm rather new to javascript, anyone can point me in the right direction please?
I searched here, and all the solution I found solve the problem "sort on text" (which is not what I need).
EDIT: I solved in this way:
$('#id_users').multiselect({
beforeopen: function(event, ui) {
$('#id_users option:selected').prependTo('#id_users');
$('#id_users').multiselect('refresh');
}
});
So in initialization of the widget, I bind those two lines of code to the beforeopen event.
In the handling of beforeopen I prepend the selected items to the others, and refresh the widget (because it reads the multi-select only once, and if you don't refresh the order remains the same, even if the HTML changes).
I can't upvote the answer because I don't have enough reputation. But thanks!
Try this.
$('#id_users option:selected').prependTo('#id_users')
→

how to use a different value from that selected in a drop down

I'm trying to figure out how (if possible, which I'm sure I can) to use a different value to that selected in a drop down box in HTML. Happy to use jQuery or JavaScript. So for example I have a series of dropdowns as follows:
<select id="country" title="Country">
<option>Argentina ARG</option>
<option>Australia AUS</option>
<option>Austria AUT</option>
<option>Austria AUT</option>
<option>Belgium BEL</option>
<option>Brazil BRA</option>
<option>Canada CAN</option>
</select>
Naturally when a user chooses say 'Brazil' the option displays 'Brazil BRA'. However, I would like to instead use the value 'BRA' is it possible to do this? Am I just being dumb and it can be done in plain HTML?
<option value="BRA">Brazil BRA</option>
Side note: I believe IE6 needs that value or things get funky onsubmit.
Use the value attribute: http://www.w3schools.com/tags/att_option_value.asp
<select id="country" title="Country">
<option value="ARG">Argentina</option>
<option value="AUS">Australia</option>
</select>
You can use the options value-attribute. This way the text that is shown to the user is Argentina ARG, but the value that is posted with the form is just ARG.
<select id="country" title="Country">
<option value="ARG">Argentina ARG</option>
<option value="AUS">Australia AUS</option>
...
</select>

dynamic dropdown list ( select boxes )

Here is the issue.
I have a select dropdown list.
<select name="listingtype" id="speedD" style="width:210px;">
<option>For Sale</option>
<option>For Rent</option>
</select>
And another select drop down list where the prices appear , which on page load it is empty..
So if user clicks For Sale: then the other select drop down list, loads price list like so:
<select name="valueA" id="speedF" style="width:200px;">
<option value="Any" selected="selected">Any</option>
<option value="50000">$50,000</option>
<option value="100000">$100,000</option>
<option value="150000">$150,000</option>
<option value="200000">$200,000</option>
<option value="250000">$250,000</option>
And if they choose For Rent. Select drop down is propagated like so:
<select name="valueA" id="speedF" style="width:200px;">
<option value="Any" selected="selected">Any</option>
<option value="100">$100</option>
<option value="150">$150</option>
<option value="200">$200</option>
<option value="250">$250</option>
<option value="300">$300</option>
</select>
I need this code to be client side, no need for server side. And just wanted to know what the cleanest method for doing this is.
Cheers.
First of all I recommend setting the value attribute in your option elements.
Example:
<option value="sale">For sale</option>
<option value="rent">For rent</option>
If you have not already heard of or seen the JavaScript library known as jQuery I strongly recommend checking it out! It can be very helpful when creating a dynamic site like this using minimal JavaScript.
I would do something like the following:
<html>
...
<body>
<div id="fillme"></div>
<script type="text/javascript">
if (document.yourformname.listingtype.value == "sale") {
//it is for sale
$('#fillme').html('<select name="valueA" id="speedF" style="width:200px;"><option value="Any" selected="selected">Any</option><option value="50000">$50,000</option><option value="100000">$100,000</option><option value="150000">$150,000</option><option value="200000">$200,000</option><option value="250000">$250,000</option></select>');
} else {
//fill it with the other elements
}
</script>
</body>
</html>
Now of course you could load it more dynamically with JSON or XML but that is up to you. I really recommend checking out the library:
http://jQuery.com
Use JavaScript to fill the empty select with options when the user selects an option (either onchange or onselect, forget which) in the For Sale/For Rent select.
EDIT: More specifically, have that second box be empty when the page loads. Store the options in arrays. Use a loop to create new OPTION elements based on each array item.

Categories