Subversion Repositories eFlore/Applications.cel

Rev

Rev 1377 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

//+---------------------------------------------------------------------------------------------------------+
// AUTO-COMPLÉTION Noms Scientifiques

function ajouterAutocompletionCommunes() {
        $('#commune').autocomplete({
                source: function(requete, add){  
                        // la variable de requête doit être vidée car sinon le parametre "term" est ajouté
                        requete = "";
                        var url = getUrlAutocompletionCommunes()+"/"+$('#commune').val();
                        $.getJSON(url, requete, function(data) {  
                                var suggestions = traiterRetourCommune(data);
                                add(suggestions);  
            });
        },
        html: true
        });
        
        $( "#commune" ).bind("autocompleteselect", function(event, ui) {
                console.log(ui.item);
                $("#commune").data(ui.item.value);
                $("#dept").data(ui.item.code);
                $("#dept").val(ui.item.code);
        });
}

function separerCommuneDepartement(chaine) {
        var deptCommune = chaine.split(' (');
        if(deptCommune[1] != null && deptCommune[1] != undefined) {
                deptCommune[1] = deptCommune[1].replace(')', '');
        } else {
                deptCommune[1] = '';
        }
        return deptCommune;
}

function traiterRetourCommune(data) {
        var suggestions = [];  
        if (data != undefined) {
                $.each(data, function(i, val) {
                        var nom = {label : '', value : ''};
                        if (suggestions.length >= AUTOCOMPLETION_ELEMENTS_NBRE) {
                                nom.label = "...";
                                nom.value = val[0];
                                suggestions.push(nom);
                                return false;
                        } else {
                                nom.label = val[0];
                                var deptCommune = separerCommuneDepartement(val[0]);
                                nom.value = deptCommune[0];
                                nom.code = deptCommune[1];
                                suggestions.push(nom);
                        }                       
                });
        }
                
        return suggestions;
}

function getUrlAutocompletionCommunes() {
        var url = SERVICE_AUTOCOMPLETION_COMMUNE_URL;
        return url;
}

$(document).ready(function() {
        ajouterAutocompletionCommunes();
});