Last active
October 15, 2019 14:00
-
-
Save NBoulfroy/53df4554290ef48e9a455cd2ed6b0de8 to your computer and use it in GitHub Desktop.
[Add data & clear old data with Materialize] Add data & clear old data in select input with Materialize #materialize #jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let element = $('#point_sale_city'); | |
let select = $('#point_sale_district'); | |
let url = '{{ path('route_ajax', {id: 0}) }}'; | |
$(document).ready(function() { | |
select.on('contentChanged', function() { | |
$(this).material_select(); | |
}); | |
ajax(element.val()); | |
}); | |
function ajax(value) | |
{ | |
$.ajax({ | |
url: url.replace(0, element.val()), | |
success: function(data) { | |
buildSelectDistrict(JSON.parse(data)); | |
} | |
}); | |
} | |
/** | |
* @param {array} data | |
*/ | |
function buildSelectDistrict(data) | |
{ | |
select.empty(); | |
for (let i = 0; i < data.length; ++i) { | |
select.append('<option value=' + data[i].Id + '>' + data[i].Name + '</option>'); | |
} | |
select.trigger('contentChanged'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment