simplified support for local array data
This commit is contained in:
parent
f4e0089b1f
commit
d7f0c0a8b1
28
select2.js
28
select2.js
@ -298,7 +298,8 @@
|
|||||||
query.callback(data);
|
query.callback(data);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
if (!("query" in opts) && opts.ajax) {
|
if (!("query" in opts)) {
|
||||||
|
if ("ajax" in opts) {
|
||||||
opts.query = (function () {
|
opts.query = (function () {
|
||||||
var timeout, // current scheduled but not yet executed request
|
var timeout, // current scheduled but not yet executed request
|
||||||
requestSequence = 0, // sequence used to drop out-of-order responses
|
requestSequence = 0, // sequence used to drop out-of-order responses
|
||||||
@ -329,6 +330,31 @@
|
|||||||
}, quietMillis);
|
}, quietMillis);
|
||||||
};
|
};
|
||||||
}());
|
}());
|
||||||
|
} else if ("data" in opts) {
|
||||||
|
opts.query = (function () {
|
||||||
|
var data = opts.data, // data elements
|
||||||
|
text = function (item) { return item.text; }; // function used to retrieve the text portion of a data item that is matched against the search
|
||||||
|
|
||||||
|
if (!$.isArray(data)) {
|
||||||
|
text = data.text;
|
||||||
|
// if text is not a function we assume it to be a key name
|
||||||
|
if (!$.isFunction(text)) text = function (item) { return item[data.text]; };
|
||||||
|
data = data.results;
|
||||||
|
}
|
||||||
|
|
||||||
|
return function (query) {
|
||||||
|
var t = query.term.toUpperCase(), filtered = {};
|
||||||
|
if (t === "") {
|
||||||
|
query.callback({results: data});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
filtered.result = $(data)
|
||||||
|
.filter(function () {return text(this).toUpperCase().indexOf(t) >= 0;})
|
||||||
|
.get();
|
||||||
|
query.callback(filtered);
|
||||||
|
};
|
||||||
|
}());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (typeof(opts.query) !== "function") {
|
if (typeof(opts.query) !== "function") {
|
||||||
|
Loading…
Reference in New Issue
Block a user