Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

afterRender option + css animation example #819

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ $(selector).autocomplete(options);
| `preventBadQueries` | `true` | Boolean value indicating if it should prevent future Ajax requests for queries with the same root if no results were returned. E.g. if `Jam` returns no suggestions, it will not fire for any future query that starts with `Jam` |
| `autoSelectFirst` | `false` | If set to `true`, first item will be selected when showing suggestions |
| `beforeRender` | optional | `function (container, suggestions) {}` called before displaying the suggestions. You may manipulate suggestions DOM before it is displayed |
| `afterRender` | optional | `function (container, suggestions) {}` called after displaying the suggestions. You may manipulate suggestions DOM after it is displayed. Useful when for example you need add class for CSS transition animation. |
| `formatResult` | optional | `function (suggestion, currentValue) {}` custom function to format suggestion entry inside suggestions container |
| `formatGroup` | optional | `function (suggestion, category) {}` custom function to format group header |
| `groupBy` | optional | property name of the suggestion `data` object, by which results should be grouped |
Expand Down Expand Up @@ -187,6 +188,36 @@ Style sample:
.autocomplete-group strong { display: block; border-bottom: 1px solid #000; }
```

CSS Animation:

```css
.autocomplete-suggestions {
-webkit-transform-origin: 50% 0;
-ms-transform-origin: 50% 0;
transform-origin: 50% 0;
-webkit-transform: scale(0.75) translateY(-21px);
-ms-transform: scale(0.75) translateY(-21px);
transform: scale(0.75) translateY(-21px);
-webkit-transition: all 0.15s ease-out;
transition: all 0.15s ease-out;
}
.autocomplete-suggestions.open {
-webkit-transform: scale(1) translateY(0);
-ms-transform: scale(1) translateY(0);
transform: scale(1) translateY(0);
}
```

```javascript
$('#autocomplete').autocomplete({
beforeRender: function(container) {
container.removeClass('open');
},
afterRender: function(container) {
container.addClass('open');
}
})
```

## Response Format

Expand Down
11 changes: 11 additions & 0 deletions src/jquery.autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@
container = $(that.suggestionsContainer),
noSuggestionsContainer = $(that.noSuggestionsContainer),
beforeRender = options.beforeRender,
afterRender = options.afterRender,
html = '',
category,
formatGroup = function (suggestion, index) {
Expand Down Expand Up @@ -696,6 +697,10 @@
that.fixPosition();
container.show();

if ($.isFunction(afterRender)) {
afterRender.call(that.element, container, that.suggestions);
}

// Select first value by default:
if (options.autoSelectFirst) {
that.selectedIndex = 0;
Expand All @@ -710,6 +715,7 @@
noSuggestions: function() {
var that = this,
beforeRender = that.options.beforeRender,
afterRender = that.options.afterRender,
container = $(that.suggestionsContainer),
noSuggestionsContainer = $(that.noSuggestionsContainer);

Expand All @@ -730,6 +736,11 @@
that.fixPosition();

container.show();

if ($.isFunction(afterRender)) {
afterRender.call(that.element, container, that.suggestions);
}

that.visible = true;
},

Expand Down