doktoreas
doktoreas

Reputation: 243

ExtJs ComboBox unique values

I have a ComboBox with data coming from a JsonStore.

A sample of data is this:

var datiCombo = [
        {"Fiume":"Chiascio","Comune":"Gubbio"},
        {"Fiume":"Tevere","Comune":"Perugia"},
        {"Fiume":"Tevere","Comune":"Roma"},
];

If I use the column "Fiume" for my ComboBox, I have got 2 entries "Tevere". Is there a way to populate ComboBox and show only unique values?

Thank you very much

Upvotes: 4

Views: 2281

Answers (2)

Miriam
Miriam

Reputation: 969

http://jsfiddle.net/KXPQn/ Hope this helps.

var datiCombo = [
        {"Fiume":"Chiascio","Comune":"Gubbio"},
        {"Fiume":"Tevere","Comune":"Perugia"},
        {"Fiume":"Tevere","Comune":"Roma"},
];

contains = function(array, entry){
    for (var j = 0; j<array.length; j++){
        if (array[j]["Fiume"]==entry){
            return true;
        };    
    };
    return false;
}

var comboData = [];

for (var i = 0; i < datiCombo.length; i++){
    if (contains(comboData, datiCombo[i]["Fiume"])== false){
        comboData.push(datiCombo[i]);        
    };
};
console.log(comboData);

Upvotes: 1

nscrob
nscrob

Reputation: 4493

Ok, so i wouldn't suggest to filter the duplicates, instead redefine the data you are getting from json. I guess you will also use the "comune" data for other combobox or something so by filtering the duplicates the only bond between the "fiume" and the "comune" will be by searching by the name.

I propose you redifine the data in something like

var fiume = [{"id":1,"name":"chiascio"},
             {"id":2,"name":"Tevere"}];
var comune =[{"name":"Gubbio", "fiumeId":1},
             {"name":"Perugia", "fiumeId":2},
             {"name":"Roma", "fiumeId":2)];

It's just a suggestion, I think it would be more easy to manage a change in selection.

Upvotes: 3

Related Questions