Skip to content

Commit 22bcd59

Browse files
committed
Autocomplete
1 parent c37804c commit 22bcd59

File tree

6 files changed

+33
-64
lines changed

6 files changed

+33
-64
lines changed

README.md

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,11 @@ Then, simply call the tagsInput function on any field that should be treated as
4545
$('.tagsinput').tagsInput();
4646
```
4747

48-
If you want to use jQuery.autocomplete, you can pass in a parameter with the autocomplete url.
48+
If you want to use jQuery.autocomplete, you can pass an object with autocomplete options (https://jqueryui.com/autocomplete/).
4949

5050
```
5151
$('.tagsinput').tagsInput({
52-
autocomplete_url: 'URL'
53-
});
54-
```
55-
56-
If you're using the bassistance jQuery.autocomplete, which takes extra parameters, you can also send in options to the autocomplete plugin, as described here.
57-
58-
```
59-
$('.tagsinput#tags').tagsInput({
60-
autocomplete_url: 'URL',
61-
autocomplete: {
62-
selectFirst: true,
63-
width: '100px',
64-
autoFill: true
65-
}
52+
autocomplete: {option: value, option: value}
6653
});
6754
```
6855

@@ -116,7 +103,6 @@ $('.tagsinput#tags').tagsInput({
116103
width: '300px', // standard option is 'auto'
117104
height: '100px', // standard option is 'auto'
118105
autocomplete: { option: value, option: value},
119-
autocomplete_url: 'URL',
120106
hide: true,
121107
delimiter: [',',';'], // or a string with a single delimiter. Ex: ';'
122108
unique: true,

dist/jquery.tagsinput-revisited.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example.html

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
<meta charset="UTF-8">
77
<meta name="viewport" content="width=device-width, initial-scale=1">
88

9-
<link rel="stylesheet" type="text/css" href="src/jquery.tagsinput-revisited.css" />
10-
119
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
1210
<script src="http://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>
11+
<link rel="stylesheet" href="http://code.jquery.com/ui/jquery-ui-git.css">
1312

1413
<script src="src/jquery.tagsinput-revisited.js"></script>
14+
<link rel="stylesheet" href="src/jquery.tagsinput-revisited.css" />
1515

1616
<style>
1717
body, html {
@@ -70,7 +70,14 @@
7070
});
7171

7272
$('#form-tags-4').tagsInput({
73-
'autocomplete_url': 'test/fake_json_endpoint.html'
73+
'autocomplete': {
74+
source: [
75+
'Apple',
76+
'Banana',
77+
'Orange',
78+
'Pizza'
79+
]
80+
}
7481
});
7582

7683
$('#form-tags-5').tagsInput({

src/jquery.tagsinput-revisited.js

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@
113113
validationPattern: null,
114114
width: 'auto',
115115
height: 'auto',
116-
autocomplete: {selectFirst: false},
117-
autocomplete_url: null,
116+
autocomplete: null,
118117
hide: true,
119118
delimiter: ',',
120119
unique: true,
@@ -190,42 +189,24 @@
190189
$(data.holder).removeClass('focus');
191190
});
192191

193-
if (settings.autocomplete_url !== null) {
194-
var autocomplete_options = {source: settings.autocomplete_url};
195-
196-
for (attrname in settings.autocomplete) {
197-
autocomplete_options[attrname] = settings.autocomplete[attrname];
198-
}
199-
200-
if (jQuery.Autocompleter !== undefined) {
201-
$(data.fake_input).autocomplete(settings.autocomplete_url, settings.autocomplete);
202-
$(data.fake_input).on('result', data, function(event, data, formatted) {
203-
if (data) {
204-
$('#' + id).addTag(data[0] + "", {
205-
focus: true,
206-
unique: settings.unique,
207-
minChars: settings.minChars,
208-
maxChars: settings.maxChars,
209-
limit: settings.limit,
210-
validationPattern: settings.validationPattern
211-
});
212-
}
213-
});
214-
} else if (jQuery.ui.autocomplete !== undefined) {
215-
$(data.fake_input).autocomplete(autocomplete_options);
216-
$(data.fake_input).on('autocompleteselect', data, function(event, ui) {
217-
$(event.data.real_input).addTag(ui.item.value, {
218-
focus: true,
219-
unique: settings.unique,
220-
minChars: settings.minChars,
221-
maxChars: settings.maxChars,
222-
limit: settings.limit,
223-
validationPattern: settings.validationPattern
224-
});
225-
226-
return false;
192+
if (settings.autocomplete !== null && jQuery.ui.autocomplete !== undefined) {
193+
$(data.fake_input).autocomplete(settings.autocomplete);
194+
$(data.fake_input).on('autocompleteselect', data, function(event, ui) {
195+
$(event.data.real_input).addTag(ui.item.value, {
196+
focus: true,
197+
unique: settings.unique,
198+
minChars: settings.minChars,
199+
maxChars: settings.maxChars,
200+
limit: settings.limit,
201+
validationPattern: settings.validationPattern
227202
});
228-
}
203+
204+
return false;
205+
});
206+
207+
$(data.fake_input).on('keypress', data, function(event) {
208+
$(this).autocomplete("close");
209+
});
229210
} else {
230211
$(data.fake_input).on('blur', data, function(event) {
231212
$(event.data.real_input).addTag($(event.data.fake_input).val(), {
@@ -305,8 +286,8 @@
305286

306287
// Removes the error class when user changes the value of the fake input
307288
$(data.fake_input).keydown(function(event) {
308-
// alt, shift, esc, ctrl and arrows keys are ignored
309-
if (jQuery.inArray(event.keyCode, [37, 38, 39, 40, 27, 16, 17, 18, 225]) === -1) {
289+
// enter, alt, shift, esc, ctrl and arrows keys are ignored
290+
if (jQuery.inArray(event.keyCode, [13, 37, 38, 39, 40, 27, 16, 17, 18, 225]) === -1) {
310291
$(this).removeClass('error');
311292
}
312293
});

test/fake_json_endpoint.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/fake_plaintext_endpoint.html

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)