Recherchez simplement avec Limopro !
${address.properties.context}
` ); return $address; }; const adresseApiUrl = `https://api-adresse.data.gouv.fr/search/`; const $adressSelect = $('.address-select'); const $addressSelect2 = $adressSelect.select2({ minimumInputLength: 3, ajax: { url: adresseApiUrl, dataType: 'json', data: function(params) { var query = { q: params.term, limit: '15' } // Query parameters will be ?q=[term]&limit=15 return query; }, processResults: function(data) { // Transforms the top-level key of the response object from 'items' to 'results' return { results: data.features.map(function(obj) { obj.id = obj.id || obj.properties.id; obj.text = obj.text || obj.properties.label; obj.city = obj.city || obj.properties.city; return obj; }) }; }, }, templateResult: formatAddress }); const geoIdsExcludeList = [ "75056", "69123", "13055", ]; let currentQuery; let $geoSearchSubmitBt = $('#geo-search-submit'); let $unavailableChoiceText = $('.unavailableChoiceText'); $unavailableChoiceText.hide(); $adressSelect.on('select2:select', function(e) { const data = e.params.data; $('.address-type').val(data.properties.type); $('.address-context').val([data.properties.district ? data.properties.district : data.properties.city, ...data.properties.context.split(',').slice(1)].join(',')); $('.address-dep').val(data.properties.context.split(',').shift()); /*if (data.properties.type == 'housenumber') { $('.address-coordinates').val( data.geometry.coordinates.join(',')); }*/ $('.address-coordinates').val(data.geometry.coordinates.join(',')); if (geoIdsExcludeList.includes(data.id)) { $unavailableChoiceText.show(); $geoSearchSubmitBt.attr('disabled', true); } else { $unavailableChoiceText.hide(); $geoSearchSubmitBt.attr('disabled', false); } }); $adressSelect.on('select2:close', function(e) { // Preserve typed value currentQuery = e.params.originalSelect2Event.data.text; }).on('select2:open', function(e) { // Fill preserved value back into Select2 input field and trigger the AJAX loading (if any) // todo target only the good input search $('.select2-search input').val(currentQuery).trigger('change').trigger("input"); console.log(e.params.data); }); //set language to french $.fn.select2.defaults.set("language", "fr"); }); }(window.jQuery, window, document));