alpha_full/admin/options/moyensL/grilleTarifaire/ajouterVille.js
2026-04-06 22:58:51 +02:00

73 lines
2.2 KiB
JavaScript
Executable File

function affecterVille(ville){
debugLocal=0;
if (debugLocal==1) alert('affecterVille => ' +ville);
document.getElementById('listPaysVille').value=document.getElementById('listPaysVille').value+ville;
}
function validerVilles(){
debugLocal=0;
if (debugLocal==1) alert('validerVilles => validerVilles ');
document.getElementById('codeOpe').value='upVille2Zone';
document.getElementById('retour').value=1;
document.ajouterVille.submit();
}
// --------------------- Liste des villes
function fetchCommune (cp){
debugLocal=0;
var tabVilleListe=new Array;
var url='https://apicarto.ign.fr/api/codes-postaux/communes/'+cp;
fetch(url,{
method: "GET",
headers: {
'Content-Type': 'application/json',
'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8',
'Access-Control-Allow-Origin':'123ventes.com',
}
// mode: "no-cors"
})
.then(rep =>
{
if (rep.ok === true) return rep.json();
// Ici je traite les erreurs HTTP
else {
alert('Pas de ville avec ce codePostal : '+cp);
return Promise.reject(`Erreur HTTP => ${rep.status}`);
}
}
)
.then(data =>{
if (debugLocal==1) console.log(data);
if (debugLocal==1) console.log(data.length);
var inner ='<ul>';
if(data.length>0){
for(i=0;i<data.length;i++){
console.log(data[i].nomCommune);
tabVilleListe[i]= data[i].nomCommune;
inner = inner + ' <li> ' + tabVilleListe[i] + ' ';
inner = inner + ' <input type="checkbox" name="ville" id="ville" value="" onClick="affecterVille(\'' + tabVilleListe[i] + '\');"> Sélectionner ';
inner = inner + '</li>';
}
inner = inner + ' </ul>';
if (document.getElementById('villeListe')) document.getElementById('villeListe').innerHTML=inner;
}
else{
alert('Pas de ville avec ce codePostal : '+cp);
}
// envoyer a php
if(tabVilleListe.length>0){
tabVilleListe = JSON.stringify(tabVilleListe);
if (document.getElementById('tabVilleListe')) document.getElementById('tabVilleListe').value=tabVilleListe;
//alert('cp='+cp);
document.testCp.submit();
}
})
.catch(err => console.log(err) )
console.log('Fin recherche ville');
}