-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
97 lines (94 loc) · 3.02 KB
/
index.html
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
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Select2Cascade</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />
<style>
.container {
margin: 0 auto;
max-width: 600px;
}
.form-group {
margin: 15px 0;
}
select {
width: 100%;
}
label{
margin: 10px 0;
display: block;
}
</style>
</head>
<body>
<div class="container">
<div class="form-group">
<label for="provider_id">Fornecedor</label>
<select name="provider_id" id="provider_id">
<option></option>
<option value="provider-1">Fornecedor 1</option>
<option value="provider-2">Fornecedor 2</option>
<option value="provider-3">Fornecedor 3</option>
</select>
</div>
<div class="form-group">
<label for="category_id">Categoria</label>
<select name="category_id" id="category_id">
<option></option>
</select>
</div>
<div class="form-group">
<label for="product_id">Produto</label>
<select name="product_id" id="product_id">
<option></option>
</select>
</div>
</div>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"
integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg=="
crossorigin="anonymous"
></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>
<script src="/select2cascade.js"></script>
<script>
$(document).ready(function () {
$('#provider_id').select2({
placeholder: 'Escolha...',
});
$('#category_id').select2({
placeholder: 'Escolha...',
});
$('#product_id').select2({
placeholder: 'Escolha...',
});
const selectProvider = new Select2Cascade(
$('#provider_id'),
$('#category_id'),
{
url: '/categories.json',
id: 'text', // qual campo vindo do ajax vai ir para o value do option
value: 'text', // qual campo vai ser usado como texto no option
callback: () => {
$('#product_id').empty().prepend('<option></option>').trigger('change');
},
}
);
selectProvider.init();
const selectCategory = new Select2Cascade(
$('#category_id'),
$('#product_id'),
{
url: '/products.json',
id: 'text', // qual campo vindo do ajax vai ir para o value do option
value: 'text', // qual campo vai ser usado como texto no option
callback: () => {},
}
);
selectCategory.init();
});
</script>
</body>
</html>