forked from robotis/hierSelect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.hierSelect.js
134 lines (123 loc) · 3.96 KB
/
jquery.hierSelect.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//[[
/*
* jQuery hierSelect Plugin
* @requires jQuery v1.4 or later
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* @version 1.0
* @author <[email protected]>
*/
//]]
(function($){
$.fn.hierSelect = function(tree, options)
{
options = $.extend({
starter: 'Veldu...',
select_class: 'hselect',
leaf_class: 'final',
node_class: 'more',
empty_value: '',
selected: []
}, options || {});
var getClean = function (name)
{
return name.replace(/_*$/, '');
};
var removeNested = function (name)
{
$("select[name^='"+ name + "']").remove();
};
var setValue = function(name, value)
{
$("input[name='" + getClean(name) + "']").val(value).change();
};
var prune = function(index)
{
var o = new Array();
for(i=0;i<options.selected.length;i++)
{
if(options.selected[i] != index)
o[i] = options.selected[i];
}
options.selected = o;
};
return this.each(function()
{
var name = $(this).attr('name') + "_";
removeNested(name);
if (typeof tree == "object")
{
var $select = $("<select>").attr('name',name).change(function()
{
var clean = getClean(name);
var set = options.empty_value;
setValue(name, set);
if (this.options[this.selectedIndex].value != '')
{
var opt = (typeof tree[this.options[this.selectedIndex].value] == 'object')
? tree[this.options[this.selectedIndex].value]
: this.options[this.selectedIndex].value;
set = this.options[this.selectedIndex].value;
setValue(name, set);
$(this).hierSelect(opt, options);
}
else
{
// Set selected from parent select
if(name != clean + '_') {
var parent = name.substr(0, name.length-1);
set = $("select[name="+parent+"] option:selected").val();
}
removeNested(name + '_');
setValue(name, set);
}
});
if ($(this).is('input'))
$select.insertBefore(this);
else
$select.insertAfter(this);
if (options.select_class)
$select.addClass(options.select_class);
if (options.starter)
$("<option>").html(options.starter).val('').appendTo($select);
$.each(tree, function(k, v)
{
if(k == 'name') return;
var o = null;
if(typeof v != 'object')
{
o = $("<option>").html(v)
.attr('value', k);
if (options.leaf_class)
{
o.addClass(options.leaf_class);
}
}
else
{
o = $("<option>").html(v['name'])
.attr('value', k);
if (options.node_class)
{
o.addClass(options.node_class);
}
}
$select.append(o);
if(jQuery.isArray(options.selected))
{
if(jQuery.inArray(k, options.selected) > -1)
{
o.get(0).selected = true;
prune(k);
$select.change();
}
}
});
}
});
// END
return this;
};})(jQuery);