2 * jQuery Select2 Sortable |
2 * jQuery Select2 Sortable |
3 * - enable select2 to be sortable via normal select element |
3 * - enable select2 to be sortable via normal select element |
4 * |
4 * |
5 * author : Vafour |
5 * author : Vafour |
6 * modified : Kevin Provance (kprovance) |
6 * modified : Kevin Provance (kprovance) |
|
7 * modified : Thierry Florac (tflorac@ulthar.net) |
|
8 * Enable sorting for Select2 based on hidden inputs |
7 * inspired by : jQuery Chosen Sortable (https://github.com/mrhenry/jquery-chosen-sortable) |
9 * inspired by : jQuery Chosen Sortable (https://github.com/mrhenry/jquery-chosen-sortable) |
8 * License : GPL |
10 * License : GPL |
9 */ |
11 */ |
10 |
12 |
11 (function ($) { |
13 (function ($) { |
22 } |
24 } |
23 |
25 |
24 var $select2 = $select.siblings('.select2-container'); |
26 var $select2 = $select.siblings('.select2-container'); |
25 var sorted; |
27 var sorted; |
26 |
28 |
27 // Opt group names |
29 if ($select.attr('type') === 'hidden') { |
28 var optArr = []; |
|
29 |
|
30 $select.find('optgroup').each(function(idx, val) { |
|
31 optArr.push (val); |
|
32 }); |
|
33 |
|
34 $select.find('option').each(function(idx, val) { |
|
35 var groupName = $(this).parent('optgroup').prop('label'); |
|
36 var optVal = this; |
|
37 |
|
38 if (groupName === undefined) { |
|
39 if (this.value !== '' && !this.selected) { |
|
40 optArr.push (optVal); |
|
41 } |
|
42 } |
|
43 }); |
|
44 |
|
45 sorted = $($select2.find('.select2-choices li[class!="select2-search-field"]').map(function () { |
|
46 if (!this) { |
|
47 return undefined; |
|
48 } |
|
49 |
|
50 var id = $(this).data('select2Data').id; |
|
51 |
30 |
52 return $select.find('option[value="' + id + '"]')[0]; |
31 // Update hidden field value |
53 })); |
32 var plugin = $select2.data('select2'); |
54 |
33 var separator = plugin.opts.separator; |
55 sorted.push.apply(sorted, optArr); |
34 var values = []; |
56 |
35 $.each(plugin.data(), function() { |
57 $select.children().remove(); |
36 values.push(this.id); |
58 $select.append(sorted); |
37 }); |
|
38 $select.val(values.join(separator)); |
|
39 |
|
40 } else { |
|
41 |
|
42 // Opt group names |
|
43 var optArr = []; |
|
44 |
|
45 $select.find('optgroup').each(function(idx, val) { |
|
46 optArr.push (val); |
|
47 }); |
|
48 |
|
49 $select.find('option').each(function(idx, val) { |
|
50 var groupName = $(this).parent('optgroup').prop('label'); |
|
51 var optVal = this; |
|
52 |
|
53 if (groupName === undefined) { |
|
54 if (this.value !== '' && !this.selected) { |
|
55 optArr.push (optVal); |
|
56 } |
|
57 } |
|
58 }); |
|
59 |
|
60 sorted = $($select2.find('.select2-choices li[class!="select2-search-field"]').map(function () { |
|
61 if (!this) { |
|
62 return undefined; |
|
63 } |
|
64 |
|
65 var id = $(this).data('select2Data').id; |
|
66 |
|
67 return $select.find('option[value="' + id + '"]')[0]; |
|
68 })); |
|
69 |
|
70 sorted.push.apply(sorted, optArr); |
|
71 |
|
72 $select.children().remove(); |
|
73 $select.append(sorted); |
|
74 } |
59 }); |
75 }); |
60 |
76 |
61 return $this; |
77 return $this; |
62 }, |
78 }, |
63 |
79 |