Skip to content
This repository was archived by the owner on Sep 10, 2023. It is now read-only.

Commit 9d06bde

Browse files
committed
Merge pull request #643 from toneplex/master
Added the ability to exclude current tokens from the autocomplete
2 parents 8134937 + 01885b0 commit 9d06bde

File tree

2 files changed

+80
-9
lines changed

2 files changed

+80
-9
lines changed

demo.html

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</script>
1616
</head>
1717

18-
<body>
18+
<body style="margin-bottom: 200px;">
1919
<h1>jQuery Tokeninput Demos</h1>
2020

2121
<h2>Simple Server-Backed Search</h2>
@@ -235,9 +235,9 @@ <h2 id="onadd-ondelete">Using onAdd and onDelete Callbacks</h2>
235235

236236
<h2 id="plugin-methods">Using the add, remove, clear and toggleDisabled Methods</h2>
237237
<div>
238-
<a href="#" id="plugin-methods-add">Add Token</a> |
239-
<a href="#" id="plugin-methods-remove">Remove Token</a> |
240-
<a href="#" id="plugin-methods-clear">Clear Tokens</a> |
238+
<a href="#" id="plugin-methods-add">Add Token</a> |
239+
<a href="#" id="plugin-methods-remove">Remove Token</a> |
240+
<a href="#" id="plugin-methods-clear">Clear Tokens</a> |
241241
<a href="#" id="plugin-methods-toggle-disable">Toggle Disabled</a><br />
242242
<input type="text" id="demo-input-plugin-methods" name="blah" />
243243
<input type="button" value="Submit" />
@@ -272,7 +272,7 @@ <h2 id="plugin-methods">Using the add, remove, clear and toggleDisabled Methods<
272272
});
273273
</script>
274274
</div>
275-
275+
276276
<h2>Local Data Search with custom propertyToSearch, resultsFormatter and tokenFormatter</h2>
277277
<div>
278278
<input type="text" id="demo-input-local-custom-formatters" name="blah" />
@@ -674,7 +674,7 @@ <h2>Change propertyToSearch anytime</h2>
674674
});
675675
</script>
676676
</div>
677-
677+
678678
<h2>Start disabled</h2>
679679
<div>
680680
<input type="text" id="demo-input-disabled" name="blah" />
@@ -720,5 +720,31 @@ <h2>Free Tagging</h2>
720720
</script>
721721
</div>
722722

723+
<h2>Exclude Current Tokens From Autocomplete</h2>
724+
<div>
725+
<input type="text" id="demo-input-local-exclude" name="blah" />
726+
<input type="button" value="Submit" />
727+
<script type="text/javascript">
728+
$(document).ready(function() {
729+
$("#demo-input-local-exclude").tokenInput([
730+
{id: 7, name: "Ruby"},
731+
{id: 11, name: "Python"},
732+
{id: 13, name: "JavaScript"},
733+
{id: 17, name: "ActionScript"},
734+
{id: 19, name: "Scheme"},
735+
{id: 23, name: "Lisp"},
736+
{id: 29, name: "C#"},
737+
{id: 31, name: "Fortran"},
738+
{id: 37, name: "Visual Basic"},
739+
{id: 41, name: "C"},
740+
{id: 43, name: "C++"},
741+
{id: 47, name: "Java"}
742+
], {
743+
excludeCurrent: true
744+
});
745+
});
746+
</script>
747+
</div>
748+
723749
</body>
724750
</html>

src/jquery.tokeninput.js

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ var DEFAULT_SETTINGS = {
1919
propertyToSearch: "name",
2020
jsonContainer: null,
2121
contentType: "json",
22+
excludeCurrent: false,
23+
excludeCurrentParameter: "x",
2224

2325
// Prepopulation settings
2426
prePopulate: null,
@@ -839,8 +841,37 @@ $.TokenList = function (input, url_or_data, settings) {
839841
return template.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + regexp_escape(value) + ")(?![^<>]*>)(?![^&;]+;)", "g"), highlight_term(value, term));
840842
}
841843

844+
// exclude existing tokens from dropdown, so the list is clearer
845+
function excludeCurrent(results) {
846+
if ($(input).data("settings").excludeCurrent) {
847+
var currentTokens = $(input).data("tokenInputObject").getTokens(),
848+
trimmedList = [];
849+
if (currentTokens.length) {
850+
$.each(results, function(index, value) {
851+
var notFound = true;
852+
$.each(currentTokens, function(cIndex, cValue) {
853+
if (value[$(input).data("settings").propertyToSearch] == cValue[$(input).data("settings").propertyToSearch]) {
854+
notFound = false;
855+
return false;
856+
}
857+
});
858+
859+
if (notFound) {
860+
trimmedList.push(value);
861+
}
862+
});
863+
results = trimmedList;
864+
}
865+
}
866+
867+
return results;
868+
}
869+
842870
// Populate the results dropdown with some results
843871
function populate_dropdown (query, results) {
872+
// exclude current tokens if configured
873+
results = excludeCurrent(results);
874+
844875
if(results && results.length) {
845876
dropdown.empty();
846877
var dropdown_ul = $("<ul/>")
@@ -939,7 +970,7 @@ $.TokenList = function (input, url_or_data, settings) {
939970
function run_search(query) {
940971
var cache_key = query + computeURL();
941972
var cached_results = cache.get(cache_key);
942-
if(cached_results) {
973+
if (cached_results) {
943974
if ($.isFunction($(input).data("settings").onCachedResult)) {
944975
cached_results = $(input).data("settings").onCachedResult.call(hidden_input, cached_results);
945976
}
@@ -948,7 +979,7 @@ $.TokenList = function (input, url_or_data, settings) {
948979
// Are we doing an ajax search or local data search?
949980
if($(input).data("settings").url) {
950981
var url = computeURL();
951-
// Extract exisiting get params
982+
// Extract existing get params
952983
var ajax_params = {};
953984
ajax_params.data = {};
954985
if(url.indexOf("?") > -1) {
@@ -968,10 +999,24 @@ $.TokenList = function (input, url_or_data, settings) {
968999
ajax_params.data[$(input).data("settings").queryParam] = query;
9691000
ajax_params.type = $(input).data("settings").method;
9701001
ajax_params.dataType = $(input).data("settings").contentType;
971-
if($(input).data("settings").crossDomain) {
1002+
if ($(input).data("settings").crossDomain) {
9721003
ajax_params.dataType = "jsonp";
9731004
}
9741005

1006+
// exclude current tokens?
1007+
// send exclude list to the server, so it can also exclude existing tokens
1008+
if ($(input).data("settings").excludeCurrent) {
1009+
var currentTokens = $(input).data("tokenInputObject").getTokens();
1010+
var tokenList = $.map(currentTokens, function (el) {
1011+
if(typeof $(input).data("settings").tokenValue == 'function')
1012+
return $(input).data("settings").tokenValue.call(this, el);
1013+
1014+
return el[$(input).data("settings").tokenValue];
1015+
});
1016+
1017+
ajax_params.data[$(input).data("settings").excludeCurrentParameter] = tokenList.join($(input).data("settings").tokenDelimiter);
1018+
}
1019+
9751020
// Attach the success callback
9761021
ajax_params.success = function(results) {
9771022
cache.add(cache_key, $(input).data("settings").jsonContainer ? results[$(input).data("settings").jsonContainer] : results);

0 commit comments

Comments
 (0)