Permalink
Browse files

Duplicates are removed from dropdown when preventDuplicates setting i…

…s set to true
  • Loading branch information...
1 parent c21349a commit fc7aa67be8019d98613d6c63c79d8dabe5f24b7e @Nkburdick committed Sep 15, 2011
Showing with 43 additions and 18 deletions.
  1. +43 −18 src/jquery.tokeninput.js
@@ -674,41 +674,66 @@ $.TokenList = function (input, url_or_data, settings) {
})
.hide();
+ var noResults = true;
$.each(results, function(index, value) {
- var this_li = settings.resultsFormatter(value);
+ if(!settings.preventDuplicates || !isSelected(value[settings.propertyToSearch])){
- this_li = find_value_and_highlight_term(this_li ,value[settings.propertyToSearch], query);
+ var this_li = settings.resultsFormatter(value);
- this_li = $(this_li).appendTo(dropdown_ul);
+ this_li = find_value_and_highlight_term(this_li ,value[settings.propertyToSearch], query);
- if(index % 2) {
- this_li.addClass(settings.classes.dropdownItem);
- } else {
- this_li.addClass(settings.classes.dropdownItem2);
- }
+ this_li = $(this_li).appendTo(dropdown_ul);
- if(index === 0) {
- select_dropdown_item(this_li);
- }
+ if(index % 2) {
+ this_li.addClass(settings.classes.dropdownItem);
+ } else {
+ this_li.addClass(settings.classes.dropdownItem2);
+ }
+
+ if(noResults) {
+ select_dropdown_item(this_li);
+ }
+
+ noResults = false;
- $.data(this_li.get(0), "tokeninput", value);
+ $.data(this_li.get(0), "tokeninput", value);
+ }
});
- show_dropdown();
+ if(noResults){
+ show_no_results();
+ } else {
+ show_dropdown();
+ }
if(settings.animateDropdown) {
dropdown_ul.slideDown("fast");
} else {
dropdown_ul.show();
}
} else {
- if(settings.noResultsText) {
- dropdown.html("<p>"+settings.noResultsText+"</p>");
- show_dropdown();
- }
+ show_no_results();
}
}
+ function show_no_results() {
+ if(settings.noResultsText) {
+ dropdown.html("<p>"+settings.noResultsText+"</p>");
+ show_dropdown();
+ }
+ }
+
+ //returns true if the value is already selected
+ function isSelected(name) {
+ var itemSelected = false;
+ $.each(saved_tokens, function(saved_index, saved_value){
+ if(saved_value[settings.propertyToSearch] == name){
+ return itemSelected = true;
+ }
+ });
+ return itemSelected;
+ }
+
// Highlight an item in the results dropdown
function select_dropdown_item (item) {
if(item) {
@@ -854,4 +879,4 @@ $.TokenList.Cache = function (options) {
return data[query];
};
};
-}(jQuery));
+}(jQuery));

0 comments on commit fc7aa67

Please sign in to comment.