From 476d5ace9af2ca35573bdb70390b9c53d043bf89 Mon Sep 17 00:00:00 2001 From: ryfeng Date: Fri, 17 Aug 2012 02:11:16 -0300 Subject: [PATCH] recursive match in local() --- select2.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/select2.js b/select2.js index 9aa0a540..08051d17 100755 --- a/select2.js +++ b/select2.js @@ -354,14 +354,29 @@ } return function (query) { - var t = query.term, filtered = {}; + var t = query.term, filtered = { results: [] }, process; if (t === "") { query.callback({results: data}); return; } - filtered.results = $(data) - .filter(function () {return query.matcher(t, text(this));}) - .get(); + + process = function(datum, collection) { + var group; + datum = datum[0]; + if (datum.children) { + group = { text: text(datum), children: [] }; + $(datum.children).each2(function(i, childDatum) { process(childDatum, group.children); }); + if (group.children.length) { + collection.push(group); + } + } else { + if (query.matcher(t, text(datum))) { + collection.push(datum); + } + } + }; + + $(data).each2(function(i, datum) { process(datum, filtered.results); }); query.callback(filtered); }; }