Giter Club home page Giter Club logo

selectorablium's Introduction

Selectorablium Build Status Bower version

Make huge <select> elements lighter by caching out all the options to localStorage, and easier to browse with options filtering.

Features:

  • Loads data as JSON via an XHR call and caches it inside localStorage
  • Makes extra XHR calls if queries are over a threshhold value in length
  • Filters options via an input element
  • Matches option's text
  • Matching ignores accented characters
  • Sorts matched options
  • Highlights query in option's text
  • Keyboard navigation works as expected
  • Fast

Installation

$ npm install @skroutz/selectorablium

or

$ bower install selectorablium

Warning: Bower is deprecated!

Dependencies

  • jQuery

Compatibility

Selectorabllium depends on the localStorage object. If localStorage is not supported in the browser, it uses an included shim which does not persist. So if you run it in an old browser, the JSON data will be fetched every time.

Setup

Include jQuery, the plugin and its css:

<link rel="stylesheet" href="css/selectorablium.css">
<script src="jquery.js"></script>
<script src="selectorablium.js"></script>

Then initialize the plugin:

<select class="selectorablium" name="my_name"></select>

<script>
  $('.selectorablium').selectorablium({
      app_name: 'my_app', //required, for namespacing purposes
      url: 'some_url',    //required, where the XHR will be fetched from
      query: 'q',         //required, GET param name for the XHR queries
      name: 'shops'       //required, name of specific instance
  })
</script>

The <select> element should include no options.

Usage

The plugin can be configured by passing it some options. See next chapter for detailed options listing.

The options can be passed with two ways:

Options object on initialization:

$('.selectorablium').selectorablium({
    app_name: 'my_app',
    url: 'some_url',
    query: 'q',
    name: 'shops',
    default_value: -1,
    default_text: 'some_text'
})

Data-attributes on <select> element:

<select class="selectorablium" name="my_name" data-app_name="test_app" data-url="some_url"></select>

Options passed on initialization have precedence over the data-attribute options.

Options

Name Required Value Default Description
app_name yes string - The name of the app. It is used for namespacing the data inside the localStorage.
url yes string - The url where the XHRs will point to.
query yes string - the GET param key for the query URL.
name yes string - the name of the specific Selectorablium instance. It is used for namespacing the data inside the localStorage.
default_value no string|number 0 The value for the default <option>.
default_text no string|number 'Please select an option' The text for the default <option>.
selected_id no string|number null On initialization, search the localStorage and create an <option> with this as value and the text the localStorage returns.
minCharsForRemoteSearch no number 3 When the query is longer than this number make query XHRs.
localCacheTimeout no number 604800000 (one week) Milliseconds after which the localStorage is considered as invalid and needs refreshing.
XHRTimeout no number 650 Milliseconds to wait before making an XHR query call.
maxResultsNum no number 10 The number of results printed
list_of_replacable_chars no array [['ά', 'α'],...] An array with accented characters and their unaccented counterpart. This is used to make smart matching on accented names even if non accented characters are used in the query

XHR Communications

The response JSON must be an array with objects, with each object having an id and a name key-value:

[{
    "id": "text_or_number1",
    "name":"text_or_number1"
},{
    "id": "text_or_number2",
    "name":"text_or_number2"
}]

On first initialization or when the localStorage needs to be refreshed, an XHR will be made to the url option. The response is expected to be the full list of <option> data.

As the user searches for results by typing a query string on an input, and after the query is longer than the minCharsForRemoteSearch option, an XHR will be made.

If, for example, the url option is 'http://some_url', the query option is 'q', and the query the user has typed in is 'testtest' an XHR will be made to: http://some_url?q=testtest

API

The instance object of each initialized <select> element can be referenced through .data('Selectorablium'):

var instance = $('.selectorablium').first().data('Selectorablium')
.then(success_callback, error_callback)

A then method is exposed so that the instance can be seen as a thenable.

The success_callback is executed after:

  • the localStorage gets initialized
  • any selected_item gets activated.

On error, the error_callback is executed.

.add(value, text)

Persist to localStorage the passed data. Subsequent searches will find and show that data If value is already present in the localStorage, then the text is not updated.

.set(value[, text])

Manually create an <option> element with the params passed for value and text.

If only the value param is passed, the text is searched in the localStorage.

.reset()

Reset the <select>'s element option to the default value and text.

.cleanup()

Resets the <select> element the the previous state. It completely removes the plugins HTML elements, its internal event handlers and stops any XHR activity.

Updating Bower package

Just execute

$ grunt publish:<type>

with type being the semver values:

  • patch
  • minor
  • major

Authors

Bill Trikalinos (billtrik)

License

This software is released under the MIT License.

selectorablium's People

Contributors

billtrik avatar zorbash avatar pharlez avatar mkousta avatar avgerin0s avatar ctrochalakis avatar vrinek avatar

Stargazers

Lazarus Lazaridis avatar George Papanikolaou avatar STAVROS avatar Chris Nitsas avatar Kostas Bariotis avatar George Raptis avatar  avatar Spyros Mandekis avatar Mpampis Kostas avatar  avatar Giorgos Tsiftsis avatar  avatar  avatar Nikos Anagnostou avatar Giorgos Avramidis avatar Andreas Loupasakis avatar  avatar George Chatzigeorgiou avatar

Watchers

George Chatzigeorgiou avatar Giorgos Tsiftsis avatar Vasilis Dimos avatar  avatar George Lamprianidis avatar Stratos Paraskevaidis avatar Yiannis Tsiouris avatar Dimitris Karteris avatar  avatar Kostis Fardelas avatar George Papanikolaou avatar Michalis Zabaras avatar James Cloos avatar Mpampis Kostas avatar George Kyrgiazos avatar George Psarakis avatar Nikos Maounis avatar Nikos Fertakis avatar Nikos Tsaganos avatar  avatar Apostolis Taxidaridis avatar Dimitris Krestos avatar Otto Antoniou avatar le0nidas avatar Giorgos Mitsis avatar Evangelos Tzortzis avatar Konstantinos Kostis avatar Dimitris Kotsakos avatar Panagiotis Papantonakis avatar Filippia Zikou avatar Dimitris Fousteris avatar Tassos Maurides avatar ChrisMpitzios avatar Harlock avatar Alkis Kalogeris avatar John Torakis avatar Stathis Aliprandis avatar Michalis Despotopoulos avatar delxen avatar  avatar Alexis Argiriou avatar Maria Sdraka avatar Sofia Kritikou avatar Christos Vrachas avatar gokussj avatar Alvaro Chysi avatar  avatar Alex Bratsos avatar John Kapantzakis avatar Konstantina Panagiotopoulou avatar  avatar Ioannis Tholoenos avatar Nikos Manoloudis avatar Leonidas Palyvas avatar Kostas Kolivas avatar Christos Melas avatar Mike avatar  avatar

selectorablium's Issues

data-selected_id cannot be string

If a string value is passed to data-selected_id the resulting value is undefined. This behaviour has its root probably inside the setSelectItem method; a check is performed for the key of the data dictionary to be either of type 'object' or of type 'number', hence when the type is 'string' the value remains unset.

Not working second instance

Second instance of plugin with same namespace name, which is inside a display:none container,
when the container is shown, the plugin doesn't work.

It will show the reset button on the right, but on click the item menu won't show / will shown empty

Pre-selected value is ignored when not present in local storage

This may be a case of client-side cache miss, if for example the value corresponds to a new record on the server. However, it doesn't trigger a remote query or invalidate the cache.

Another option (which is easier to implement) would be to allow passing arbitrary key/value pairs to be pre-selected. In that case, they should be added to local storage as well.

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.