'How to do a cascade filtering using javascript
I have a table a such
| Env | depot | articles |
|---|---|---|
| 100 | AAA | Capacitor |
| 100 | AbA | Diode |
| 100 | AAA | cable |
| 100 | AbA | mike |
| 100 | ABC | modem |
| 900 |
Im extracting my data from a postgres database using php and it's working fine
Now i'm wanting to do a cascade filter to show for a specific Env the depot associated and the articles under that articles depot
For example , if i select in my dropdownlist Env = 100 , dropdownlist dépot would show me AAA / AbA / ABC then when i select AAA dropdowlist Articles will show Capacitor / cable
if i select in my dropdownlist Env = 900 , dropdownlist dépot would show me nothing so on in dropdowlist Articles
This is a sample of Data but my table contains more values .
Here is my code that tried but couldn't reach desired output
var options = $('#env').val();
var selectOptions = '';
for (i = 0; i < options.length; i++) {
selectOptions += '<option value="' + options[i] + '">' + options[i] + '</option>';
}
$('#env').append(selectOptions).on('change', function() {
var selected = $(this).find('option:selected').val();
$('#depot, #articles').empty();
$.each(envs, function(i, v) {;
if (v.env == selected) {
$('#depot').append('<option value="' + v.depot + '">' + v.depot + '</option>');
$('#articles').append('<option value="' + v.articles + '">' + v.articles + '</option>');
}
});
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
