forked from foowie/dependentselectbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.nette.dependentselectbox.js
More file actions
68 lines (59 loc) · 1.81 KB
/
jquery.nette.dependentselectbox.js
File metadata and controls
68 lines (59 loc) · 1.81 KB
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
/**
* @author Daniel Robenek
* @license MIT
*/
/**
* $(document).ready(function() {
* $.dependentselectbox.initialize();
* });
*
* Add to jquery.nette.js at the end of $.nette.success:
* $.dependentselectbox.hideSubmits();
* or use livequery
*/
jQuery.extend({
dependentselectbox: {
controlClass: 'dependentControl',
buttonSuffix: '_submit',
hideSubmits: function() {
// Here hide all you want. Default is to hide <tr> of button
$('.'+$.dependentselectbox.controlClass+$.dependentselectbox.buttonSuffix).parent().parent().hide();
},
initialize: function() {
$.dependentselectbox.hideSubmits();
$('.'+$.dependentselectbox.controlClass).live('change', function() {
// Nette form validation
button = document.getElementById(($(this).attr('id'))+$.dependentselectbox.buttonSuffix);
button.form["nette-submittedBy"] = button;
// ----
$('#'+($(this).attr('id'))+$.dependentselectbox.buttonSuffix).ajaxSubmit($.dependentselectbox.jsonResponse);
});
},
updateSelectBox: function(id, selectedKey, items) {
$("#" + id + " option").remove();
var select = $("#" + id);
for(var i in items) {
var item = $("<option></option>").attr("value", i).html(items[i]);
if(i == selectedKey)
item.attr("selected", "selected");
if(i == "")
select.prepend(item);
else
select.append(item);
}
},
jsonResponse: function(payload) {
if(!(payload["type"] && payload["type"] == "JsonDependentSelectBoxResponse")) {
$.nette.success(payload);
return;
}
var items = payload["items"];
for(var i in items) {
$.dependentselectbox.updateSelectBox(i, items[i]["selected"], items[i]["items"]);
}
}
}
});
$(document).ready(function() {
$.dependentselectbox.initialize();
});