-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_options.js
33 lines (25 loc) · 958 Bytes
/
add_options.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
(function ($) {
var getValue = function(object, property) {
return (function roam(property, objeto) {
var hierarchy = property.split(".");
objeto = objeto[ hierarchy.shift() ];
return (hierarchy.length > 0 && typeof objeto !== "undefined")?
roam(hierarchy.join("."), objeto) : objeto
})(property, object);
};
$.fn.extend({
addOptions: function(collection, config) {
var self = $(this).html("");
var value = (config && config.value)? config.value : "id";
var text = (config && config.text)? config.text : "name";
if( config && typeof config.before === "function") config.before.call(this);
$.each(collection, function(index, item){
self.append(
$("<option>", { value : getValue(item, value) }).html( getValue(item, text) )
);
});
if( config && typeof config.after === "function") config.after.call(this);
return this;
}
})
})(jQuery);