Code Monkey home page Code Monkey logo

jquery-flexselect's Introduction

flexselect: a jQuery plugin

FlexSelect is a jQuery plugin that turns select boxes into flex-matching incremental-finding controls.

Flex matching a few keystrokes against longer strings is a boon in productivity for typists. Applications like Quicksilver, LaunchBar, and Launchy have made this method of keyboard entry a popular one. It's time to bring this same functionality to web controls. FlexSelect does that for select boxes.

Usage

First, load jQuery, the LiquidMetal scoring algorithm, and the plugin:

<script src="jquery.min.js" type="text/javascript"></script>
<script src="liquidmetal.js" type="text/javascript"></script>
<script src="jquery.flexselect.js" type="text/javascript"></script>

Now, let's attach it to your select boxes on DOM ready:

<pre>
  jQuery(document).ready(function() {
    jQuery("select.flexselect).flexselect();
  });
</pre>

This will turn all select elements with a class of flexselect:

<select class="flexselect" id="president name="president">
  <option value="1">George Washington</option>
  <option value="2">John Adams</option>
  <option value="3">Thomas Jefferson</option>
  ...
</select>

into a bad-ass autocompleting text box with flex matching support.

For more usage and examples: http://rmm5t.github.io/jquery-flexselect/

Inspired by:

Todo

  • Review the "picked" logic
  • Add templating support for matched list output.
  • Add highlighting of matched characters in the results.
  • Consider support for optgroup tags

Author

Ryan McGeary (@rmm5t)

License

MIT License

jquery-flexselect's People

Contributors

awmichel avatar colbysargentcodes avatar dependabot[bot] avatar flavorjones avatar frankel avatar mark-slepkov avatar mitchsatchwell avatar rmm5t avatar schourode avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jquery-flexselect's Issues

this.hidden set to string instead of jquery object

Patch:

diff -u jquery.flexselect.js.1 jquery.flexselect.js
--- jquery.flexselect.js.1      2010-07-12 14:37:24.000000000 -0400
+++ jquery.flexselect.js        2010-07-12 14:32:15.000000000 -0400
@@ -55,7 +55,8 @@
     renderControls: function() {
       var selected = this.select.children("option:selected");

-      this.hidden = $("<input type='hidden'/>").attr({
+      this.hidden = $('<input type="hidden"/>');
+      this.hidden.attr({
         id: this.select.attr("id"),
         name: this.select.attr("name")
       }).val(selected.val());

Doesn't fire onChange

$('#dd').flexselect();
$('#dd').change(function() { alert('never shows.'); }; <== never fires.

Quick fix:
In the pickSelected function add .change() after hidden.val().
pickSelected: function() {
var selected = this.results[this.selectedIndex];
if (selected) {
this.input.val(selected.name);
this.hidden.val(selected.value).change(); //<==
this.picked = true;
} else if (this.settings.allowMismatch) {
this.hidden.val("").change(); //<==
} else {
this.reset(); //Maybe fire change here?
}
}

allowMismatch only works if user does not select an option first

When allowMismatch is enabled, if the user first selects an option and then enter a custom value the value of the first selected item is still associated to the field name.
It is therefore not possible to know if the user entered a custom value or picked from the list.

More info:
Option list looks like that B
$_POST['select'] => A (last option selected, even if custom value entered)
$_POST['new_select'] => B (literal value of the option, even if an option has been selected from the list)

Customization

Text highlighting?
Also is it possible to search by key or value?
Example-
key - value
bird - peacock
car - audi
car - bmw
bird - pigeon
sport - baseball

then when you type "b", it first displays baseball, then bmw and lastly the key bird arranged alphabetically by value.
Basically an option to choose b/w key or value to consider sorting and searching.

Will be so nice.

Ensure selected item in drop-down list is always visible

When a person uses keyboard navigation (up and down arrows) to navigate the drop-down list, when the selection goes outside the current scroll viewable area, the drop-down doesn't scroll with the selection, and the selected item isn't visible.

Make the list auto-scroll when navigating with the arrow keys.

Pasting valid value

When you paste a valid value, using the standard configuration and click with the mouse outside the box it will reset the value.

The demo shows the same behavior.

Steps to reproduce:

  1. Go to the demo page
  2. Click the pick an animal box
  3. Paste text "moose"
  4. With the mouse click outside the box

When I did this I expected to select the value if it is valid.

If you want I can make the change and pull.

[Feature require]Show "No match found" on mismatch & not to focus after selecting an item

  1. I think showing a friendly notice is better than showing a blank list.
    Maybe someone won't notice there's a blank list under the input(text) field.
  2. Another function I need is not to focus after selected an item. Because some website focus on the input field (especially text field) if the value which user typed is mismatch with the rule(like date format, or username which contains some symbols like ~?,;' )
  3. BTW, though the source code is easy to read, a documentation to the settings or a tutorial is needed.

Thx. : )

FlexSelect refresh does not work properly

Hi, I have 4 drop downs on a page, 2 of them defined as flexselect and 2 regular drop downs. When I populate them for the first time, they all work fine. When I select one of the entries from the 2 regular drop downs, I need to reset the flexselect dropdowns to blank. I do it using the statement below
$("#dropdwonName_flexselect").val("");
Then I repopulate all drop downs with new data. When I click on the flexselect dropdowns, the values are not displayed until I start typing in the drop down box. Am I doing something wrong?
Thanks for your help in advance.

Compatibility with windows 8

I'm having an issue in windows 8 in which the list only contains 2 elements instead of the usual 6~.

Here is a picture of how it looks on windows 7:
c3l

Here is a picture of how it looks on windows 8:
zgoicdp

These were both using google chrome. If you need any more info let me know.

Displaying dropdown only when input text changes

In the demo, I've select the item: George Washington

Then if I click the input again, the dropdown is displayed showing the only match of the list according with the input text, which ovbiulsly is also George Washington.

I think is redundant, and you display the dropdown only if the input text changes.

Feature request: pressing enter could submit the form

When the dropdown is open, enter chooses the current item. However, when the dropdown is closed, I'd like enter to submit the form.

However, I'm sure someone wouldn't want this to be the default (or only) option, so this could be a feature:

$(...).flexselect({ submitOnEnter: true });

'hello' appears in console

when selecting an option by pressing tab, 'hello' appears in the console.

this.input.keydown(function(event) {
    switch (event.keyCode) {
          case 9:  // tab
            console.log("hello");
            self.pickSelected();
            self.hide();

Dropdown list order is unstable

When using a long list (>10 items) in a browser such as Chromium, the order of items sometimes changes when displaying the entire list.

This happens because Array.sort is not guaranteed to be stable. It is in Firefox and Safari, but in Chrome/Chromium, Opera and some versions of IE it is not. See:
https://bugzilla.mozilla.org/show_bug.cgi?id=224128
http://code.google.com/p/v8/issues/detail?id=90
http://ofb.net/~sethml/is-sort-stable.html

The sortResults function (line 198 of jquery.flexselect.js) should not rely on Array.sort being stable. Perhaps only sort the results if the input box is not empty?

Links

Hello!

Is it possible to have link after choosing an option?
Or an anchor even?

Thanks ;)

Standalone Javascript?

I'd like to know if there's a standalone version of this, as a javascript only library, Thank you for reading.

Problem with Safari/Mac

The plugin works fine except that there is a problem with Safari on Mac. The problem is that, with the number of possibilities listed under a control limited to 10, there is no scroll-bar to access the other possibilities. (With the other browsers that I have tested there is a scroll bar and therefore no problem.)

This can be demonstrated by visiting the page https://rmm5t.github.io/jquery-flexselect/ using Safari on Mac (I'm using Safari version 14.1.1 and macOS version 11.4) On focussing the right-hand flexselect control for 'Pick a U.S. President' only the first 10 presidents are listed, and there is no scroll bar to allow the user to see the other possibilities. I realise that the user can access these other presidents by typing a few letters from their name, but in some situations the user may not be able to think of the name they want.

I would expect the same problem on Safari on iPhone but have not checked this.

When initialising on dynamic selects it does not populate

I load in my select dynamically like below, and it nearly works except the UL generated by flex select is empty

(Otherwise this awesome, I hate to point this out!! an ideas ?)

  • I even added a half-second delay to make sure it had populated first

function loadActivitySelect() {

var $activityContainer = $('#ActivitySelectContainer');

$.ajax({
    type: 'GET',
    url: '/MyGames?handler=ActivitySelect',
    contentType: 'application/json',
    dataType: 'html',
    success: function (response) {
    
    // Appends a select to this element

        $activityContainer.empty();
        $activityContainer.append(response);
        var $activitySelect = $activityContainer.find('select');

        setTimeout(function () {


            $activitySelect.flexselect();
        }, 500);

    },
    failure: function (response) {

        $activityContainer.empty();
    }
});

}

optgroups

Fails if optgroups are present.
In my situation, i only init flexselect on selects with 50 or more options. When there are optgroups the list doesnt get populated.

allowMismatch does not work as expected

Consider the following example:

<select id="myflex" class="flexselect">
  <option value="george">George Washington</option>
  <option value="john">John Adams</option>
  <option value="thomas">Thomas Jefferson</option>
</select>
<input type="button" id="test" value="Test" />

This select is then allowed to mismatch:

$("select.flexselect").flexselect({ allowMismatch: true });
$('#test').click(function() { alert($('#myflex').val(); });

The problem occurs when the following steps are followed:

  1. Start typing 'John' in the input box. That shows the 'John Adams' option. Select that.
  2. Click the Test button. The alert box will show 'john'.
  3. Click the input box again and clear it. Now type 'hello' in the input box.
  4. Click the Test button again. Because we now input a custom value, I expect val() to return null (preferably I would like to see 'hello', but I know that's not how it works). Instead, it shows 'john' again.

The selected value basically becomes invalid as soon as you start typing in the input box. That means it should be cleared. That is very easy to achieve:

@@ -153,6 +153,8 @@
       });
 
       this.input.keyup(function(event) {
+        self.select.val('');
+
         switch (event.keyCode) {
           case 13: // return
             event.preventDefault();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.