From 4344fa054e6642cd5f0323a3335425c63e80a4ff Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Fri, 27 Jul 2012 07:53:21 -0400 Subject: [PATCH] Added support for limiting selection Fixes #59 Signed-off-by: Igor Vaynberg --- select2.css | 5 +++++ select2.js | 11 +++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/select2.css b/select2.css index 806f798e..ec021183 100755 --- a/select2.css +++ b/select2.css @@ -315,6 +315,11 @@ Version: @@ver@@ Timestamp: @@timestamp@@ display: list-item; } +.select2-results .select2-selection-limit { + background: #f4f4f4; + display: list-item; +} + /* disabled look for already selected choices in the results dropdown .select2-results .select2-disabled.select2-highlighted { diff --git a/select2.js b/select2.js index ddb49e33..96b88a07 100755 --- a/select2.js +++ b/select2.js @@ -1,4 +1,4 @@ -/* +/* Copyright 2012 Igor Vaynberg Version: @@ver@@ Timestamp: @@timestamp@@ @@ -1035,6 +1035,11 @@ results.html(escapeMarkup(html)); postRender(); } + + if (opts.selectionLimit && ("getVal" in this) && this.getVal().length >= opts.selectionLimit) { + render("
  • " + opts.formatLimitReached(opts.selectionLimit) + "
  • "); + return; + } if (search.val().length < opts.minimumInputLength) { render("
  • " + opts.formatInputTooShort(search.val(), opts.minimumInputLength) + "
  • "); @@ -1983,7 +1988,7 @@ $(val).each(function () { if (indexOf(this, unique) < 0) unique.push(this); }); - this.opts.element.val(unique.length === 0 ? "" : unique.join(",")); + this.opts.element.val(unique.length === 0 ? "" : unique.join(this.opts.separator)); } }, @@ -2138,9 +2143,11 @@ }, formatNoMatches: function () { return "No matches found"; }, formatInputTooShort: function (input, min) { return "Please enter " + (min - input.length) + " more characters"; }, + formatLimitReached: function (limit) { return "You can only select " + limit + " items"; }, formatLoadMore: function (pageNumber) { return "Loading more results..."; }, minimumResultsForSearch: 0, minimumInputLength: 0, + selectionLimit: 0, id: function (e) { return e.id; }, matcher: function(term, text) { return text.toUpperCase().indexOf(term.toUpperCase()) >= 0;