Recently I was asked to implement search in long dropdowns. Our customer got stressed each time when he had had to find a product in the loooong list.
I tried to search for a solution in the Internet and there are at least two fancy ones. But one of them is very slow for long lists (well, we do need to search in long lists first of all) and the other one is simple but doesn't allow to select items manually.
So I ended up with very easy solution
(jQuery plugin should be included first):
<script type="text/javascript"> $(document).ready(function() { $('#searchProduct').keyup(function() { if ($('#searchProduct').val().length >= 3) { //search only after 3 chars are entered. search = $('#searchProduct').val(); $('#selectProduct option').each(function(){ if ($(this).text().indexOf(search) > -1) { $(this).attr('selected', 'yes'); } }); } }); }); </script> <input type="text" size="20" id="searchProduct" value="" name="search"> <select id="selectProduct" name="whatever"> <option value="1">-options here-</option> ..... </select>
And here's what I got:
search:
Sea Shell Glass Vessel Sink (N-30)
Shell Glass Vessel Sink / Bowl (ED-A73)
Sheven Short Single Hole Faucet (ED8322-35)
Shining Fastening Double Hanger (OM33011B3-2)
Shining Fastening Double Parallel Towel Rod (OM33011B-13-600)
Shining Fastening Glass Shelf With Rail (OM33011B-A)
Shining Fastening Single Cup Rack (OM33011B-8)
Shining Fastening Single Hanger (OM33011B3-1)
Shining Fastening Single Towel Rod 600 mm (OM33011B12-600)
Shining Fastening Soap Holder (OM33011B-6)
Shining Fastening Toilet Brush Rack (OM33011B10-A)
Shining Fastening Toilet Paper Holder W/Cover (OM33011B11-C)
Shining Fastening Towel Ring (OM33011B5-1)
Shower Arm 16" (G0001)
Shower Arm 17" (G0002)
Shower Arm 350 mm (G0040)
Shower Holder D14100 (D14100)
Shower Panel DreamLine 2648-SP (DL-2648-SP)
Shower Panel DreamLine 2728-SP (DL-2728-SP)
Shower Panel DreamLine-2033-SP (DL-2033-SP)
Shower Panel DreamLine-2050-SP (DL-2050-SP)
Shower Panel DreamLine-2358-SP (DL-2358-SP)
Shower Panel DreamLine-2578-SP (DL-2578-SP)
Shower Panel DreamLine-2718-SP (DL-2718-SP)
Shut Off Valve (EDV005)
Shut Off Valve Set (EDV006)
Siena 12" Tall Single Handle Faucet (ED-146)
Siena 8" Tall 3 Piece Faucet (ED-142)
Siena 9" Tall Single Hole Faucet (ED-145)
Siena Wall Mount Faucet (ED-037)
Sienna Spiral Glass Vessel Sink (12mm) (N-310B)
Sienna Spiral Glass Vessel Sink (19mm) (N310-19MM)
Sierra Bathroom Shower Panel (EDBSH-1)
Silver Bell Glass Vessel Sink (N-212-F)
Silver Fortune Glass Vessel Sink/Bowl (ED-8531)
Silver Persuasion Glass Vessel Sink (N-212)
Simply Stainless Collection Kitchen Faucet A-19A (A-19A)
Simply Stainless Collection Kitchen Faucet A-19B (A-19B)
Simply Stainless Collection Kitchen Faucet A-20A (A-20A)
Simply Stainless Collection Kitchen Faucet A-3 (A-3)
Simply Stainless Collection Kitchen Faucet A27B (A-27B)
Simply Stainless Collection Kitchen Faucet H-1A (H-1A)
Simply Stainless Collection Kitchen Faucet H2-B (H-2B)
...but what if I have multiselect dropdown and want to select several items from search? Piece of cake :)
<script type="text/javascript"> $(document).ready(function() { $('#searchProduct2').keyup(function() { if ($('#searchProduct2').val().length >= 3) { search = $('#searchProduct2').val(); $('#selectProduct2 option:selected').each(function(id){ $(this).removeAttr('selected'); }); $('#selectProduct2 option').each(function(){ if ($(this).text().indexOf(search) > -1) { $(this).attr('selected', 'yes'); } }); } }); }); </script> <input type="text" size="20" id="searchProduct2" value="" name="search"> <select id="selectProduct2" name="current_products[]" multiple="yes" size="10"> <option value="1">-options here-</option> ..... </select>
search:
Sea Shell Glass Vessel Sink (N-30)
Shell Glass Vessel Sink / Bowl (ED-A73)
Sheven Short Single Hole Faucet (ED8322-35)
Shining Fastening Double Hanger (OM33011B3-2)
Shining Fastening Double Parallel Towel Rod (OM33011B-13-600)
Shining Fastening Glass Shelf With Rail (OM33011B-A)
Shining Fastening Single Cup Rack (OM33011B-8)
Shining Fastening Single Hanger (OM33011B3-1)
Shining Fastening Single Towel Rod 600 mm (OM33011B12-600)
Shining Fastening Soap Holder (OM33011B-6)
Shining Fastening Toilet Brush Rack (OM33011B10-A)
Shining Fastening Toilet Paper Holder W/Cover (OM33011B11-C)
Shining Fastening Towel Ring (OM33011B5-1)
Shower Arm 16" (G0001)
Shower Arm 17" (G0002)
Shower Arm 350 mm (G0040)
Shower Holder D14100 (D14100)
Shower Panel DreamLine 2648-SP (DL-2648-SP)
Shower Panel DreamLine 2728-SP (DL-2728-SP)
Shower Panel DreamLine-2033-SP (DL-2033-SP)
Shower Panel DreamLine-2050-SP (DL-2050-SP)
Shower Panel DreamLine-2358-SP (DL-2358-SP)
Shower Panel DreamLine-2578-SP (DL-2578-SP)
Shower Panel DreamLine-2718-SP (DL-2718-SP)
Shut Off Valve (EDV005)
Shut Off Valve Set (EDV006)
Siena 12" Tall Single Handle Faucet (ED-146)
Siena 8" Tall 3 Piece Faucet (ED-142)
Siena 9" Tall Single Hole Faucet (ED-145)
Siena Wall Mount Faucet (ED-037)
Sienna Spiral Glass Vessel Sink (12mm) (N-310B)
Sienna Spiral Glass Vessel Sink (19mm) (N310-19MM)
Sierra Bathroom Shower Panel (EDBSH-1)
Silver Bell Glass Vessel Sink (N-212-F)
Silver Fortune Glass Vessel Sink/Bowl (ED-8531)
Silver Persuasion Glass Vessel Sink (N-212)
Simply Stainless Collection Kitchen Faucet A-19A (A-19A)
Simply Stainless Collection Kitchen Faucet A-19B (A-19B)
Simply Stainless Collection Kitchen Faucet A-20A (A-20A)
Simply Stainless Collection Kitchen Faucet A-3 (A-3)
Simply Stainless Collection Kitchen Faucet A27B (A-27B)
Simply Stainless Collection Kitchen Faucet H-1A (H-1A)
Simply Stainless Collection Kitchen Faucet H2-B (H-2B)
Comments
Post new comment