Complete Me is a JavaScript library for autocompletes. It can suggest results (rather like Google search). And is DOM friendly - a maximum of 10 results are rendered in the DOM at once.
A demo of the library in action can be found here.
So far tested on Chrome, Firefox, Safari and Opera.
Nothing (not even jQuery)
$ git clone [email protected]:callum-hart/Complete-Me.git
$ cd Complete-Me
$ npm install
$ grunt watch
- Include CSS
- Include JavaScript
- Create an instance:
var instance = new CompleteMe(element, { options });
element
can be a selector or a DOM element.
- Details
- Data that should show in the results dropdown.
- Type
Array <String> | <Object>
- Required Yes
- Condition
- When array type is object, attributes are key and value:
[{"key": "", "value": ""}]
- When array type is object, attributes are key and value:
- Details
- Placeholder text
- Type
String
- Default
Type here…
- Details
- Select an existing result by its value.
- Type
String
- Details
- Select an existing result by its key.
- Type
String
- Details
- Text to show when there are no results.
- Type
String
- Default
Couldn't find [input value]
- Details
- Allow new records to be added to the list.
- Type
Boolean
- Default
false
- Details
- Text / DOM element to show when there are no results.
- Type
String
- Default
Hit <span class="cm-key">enter</span> to add
- Condition
- Option
canAddNewRecords
has to be set totrue
.
- Option
- Details
- Text to show when remote data is being fetched.
- Type
String
- Default
Fetching more results...
- Condition
- Callback onNoResults has to be present on initialization.
- Details
- Show suggestion in placeholder.
- Type
Boolean
- Default
false
- Details
- Specify what the data attribute
data-save-value
should use.
- Specify what the data attribute
- Type
Boolean
- Default
true
- Condition
- When
true
(and data contains objects)data-save-value
uses the attributevalue
. - When
false
(and data contains objects)data-save-value
uses the attributekey
.
- When
onSelect: (value) {}
- Details
- When a record in the results dropdown is selected.
- Arguments
(value)
|(value, key)
- Condition
- When data contains objects the object key is passed as the second parameter.
onAdd: (newRecord) {}
- Details
- When a new record is added to the list.
- Arguments
(newRecord)
- Condition
- Option
canAddNewRecords
has to be set totrue
.
- Option
onNoResults: (value) {}
- Details
- When the input value isn’t found in data.
- Arguments
(value)
onFocussed: {}
- Details
- When
focus
event is fired on complete me input.
- When
onBlurred: {}
- Details
- When
blur
event is fired on complete me input.
- When
onKeyedDown: {}
- Details
- When
keydown
event is fired on complete me input.
- When
onKeyedUp: {}
- Details
- When
keyup
event is fired on complete me input.
- When
onShowSuggestion: {}
- Details
- When a suggested result is shown.
- Condition
- Option suggestResult has to be set to
true
.
- Option suggestResult has to be set to
onClearSuggestion: {}
- Details
- When a suggested result is hidden.
- Condition
- Option suggestResult has to be set to
true
.
- Option suggestResult has to be set to
onSuggestionSelected: (value) {}
- Details
- When a suggested result is selected.
- Condition
- Option suggestResult has to be set to
true
.
- Option suggestResult has to be set to
updateData: (newData) {}
- Details
- Update the data in the list.
- Arguments
(newData)
The HTML generated by complete me is:
<div class="cm-container">
<div class="cm-input-wrap">
<input type="text" class="cm-input">
<input type="text" class="cm-suggestion">
</div>
<div class="cm-results-wrap">
<ul class="cm-results">
<li><a href="#"></a></li>
</ul>
</div>
</div>
Classes that are applied when a certain condition is true.
.results-showing
- Condition
- Applied when complete me dropdown list is showing.
- Element
- Applied to
.cm-container
- Applied to
.focussed
- Condition
- Applied when complete me input is focussed.
- Element
- Applied to
.cm-container
- Applied to
.cm-without-results
- Condition
- Applied when there aren’t any results for the given search query.
- Element
- Applied to
.cm-container .cm-results-wrap .cm-results li
- Applied to
.cm-highlight
- Condition
- Applied to the text matching the search query.
- Element
- Applied to
.cm-container .cm-results-wrap .cm-results a span
- Applied to
data-tabindex
- Details
- Set the tabindex for the input.
- Element
- Applied to
.cm-container
- Applied to
data-save-value
- Details
- Get the value of the input. This is handled internally by complete me.
- Element
- Applied to
.cm-container
- Applied to