From d6f506e16bbec859bb9fc39e1467112ed422ec0c Mon Sep 17 00:00:00 2001 From: Igor Vaynberg Date: Mon, 16 Jul 2012 00:22:44 +0300 Subject: [PATCH] more flexible format helpers. fixes #217 --- select2.js | 92 +++++++++++++++++++++++++++--------------------------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/select2.js b/select2.js index 981f1d85..ee36ce04 100755 --- a/select2.js +++ b/select2.js @@ -551,58 +551,53 @@ populateResults: function(container, results, query) { var uidToData={}, populate, markup=[], uid, data, result, children, formatted, id=this.opts.id; - populate=function(results, depth) { + populate=function(results, container, depth) { - var i, l, uid, result, selectable, compound; + var i, l, uid, result, selectable, compound, node, label, innerContainer; for (i = 0, l = results.length; i < l; i = i + 1) { result=results[i]; selectable=id(result) !== undefined; compound=("children" in result) && result.children.length > 0; - markup.push("
  • "); + label.addClass("select2-result-label"); - uid=nextUid(); - markup.push(" id='select2-result-"+uid+"'"); - uidToData[uid]=result; - - markup.push(">
    "); - formatted=opts.formatResult(result, query, markup); - // for backwards compat with <3.0 versions + var formatted=opts.formatResult(result, label, query); if (formatted!==undefined) { - markup.push(formatted); + label.html(formatted); } - markup.push("
    "); + + node.append(label); if (compound) { - markup.push(""); + + innerContainer=$(""); + innerContainer.addClass("select2-result-sub"); + populate(result.children, innerContainer, depth+1); + node.append(innerContainer); } - markup.push("
  • "); - } + node.data("select2-data", result); + container.append(node); + } }; - populate(results, 0); - - container.append(markup.join("")); - - for (uid in uidToData) { - $("#select2-result-"+uid, container).data("select2-data", uidToData[uid]); - } - + populate(results, container, 0); }, - formatResult: function(result, query, markup) { + formatResult: function(result, container, query) { + var markup=[]; markMatch(result.text, query.term, markup); + return markup.join(""); }, - formatSelection: function (data) { - return data.fullText || data.text; + formatSelection: function (data, container) { + return data.text; }, formatNoMatches: function () { return "No matches found"; }, formatInputTooShort: function (input, min) { return "Please enter " + (min - input.length) + " more characters"; }, @@ -1380,11 +1375,15 @@ // single updateSelection: function (data) { + var container=this.selection.find("span"), formatted; + this.selection.data("select2-data", data); - this.selection - .find("span") - .empty().append(this.opts.formatSelection(data)); + container.empty(); + formatted=this.opts.formatSelection(data, container); + if (formatted !== undefined) { + container.append(formatted); + } this.selection.removeClass("select2-default"); @@ -1710,19 +1709,20 @@ // multi addSelectedChoice: function (data) { - var choice, - id = this.id(data), - - parts = ["
  • ", - "", - "", - "
  • " - ], - val = this.getVal(); - - choice = $(parts.join("")); + var choice=$("
  • "), + id = this.id(data), + val = this.getVal(), + formatted; choice.find('.select2-tmp').replaceWith(this.opts.formatSelection(data)); + + formatted=this.opts.formatSelection(data, choice); + if (formatted !== undefined) { + choice.append(formatted); + } + + choice.append(""); + choice.find(".select2-search-choice-close") .bind("click dblclick", this.bind(function (e) { if (!this.enabled) return;