[GIS] sorted ItemFileReadStore list not pushed to dropdown menu // Dojo, ArcGIS

arcgis-javascript-apidojojavascriptkey-value-store

this may be more a Dojo question than ArcGIS, but I am not sure if the Javascript API is related to this issue.

I am trying to alphabetically sort a Dojo store and show these items in a dropdown list, based on this example: http://dojotoolkit.org/reference-guide/1.8/dojo/data/ItemFileReadStore.html

I can see in Firebug that my list is being sorted, but the sorted list is not displayed in the dropdown menu; the items there are still sorted by ObjectID.
I am not sure if I need to run my 'set' function differently, or if I am missing some code to replace the store list with the newly sorted list…ideas? // Jason

code:

    var dataItems = {
        identifier: 'name',
        label: 'name',
        items: values,

    };


var menuStore = new dojo.data.ItemFileReadStore({ data: dataItems });  
menuStore.comparatorMap = {};

menuStore.comparatorMap['name']=function(a,b){
    if(a<b) return -1;
    if(a>b) return 1;
    return 0;
};

 var sortAttributes = [{attribute: "name", ascending: true}];
function completed(items, findResult){
    for(var i = 0; i < items.length; i++)
    {
        var value = menuStore.getValue(items[i], "name");
        console.log("Item ID: [" + menuStore.getValue(items[i], "name"));
        //// this console statement produces a sorted list in Firebug //// 
    }

}
function error(errData, request){
    console.log("Failed in sorting data.");
}

menuStore.fetch({onComplete: completed, onError: error, sort: sortAttributes});     
//  dijit.byId("schoolMenu").store = menuStore;   // may need this if an older version of API is used 

  dijit.byId("schoolMenu").set ("store", menuStore); // schoolMenu is dropdown menu     
} // end function

Best Answer

Okay, since you're using the using the ComboBox, I'm reworking my answer so that it fits your question. Since you need to sort your data, we're going to modify the oncomplete command to load the sorted data into the menuStore, then create your combobox.

var dataItems = {
    identifier: 'name',
    label: 'name',
    items: values
};

var menuStore = new dojo.data.ItemFileReadStore({ data: dataItems, clearOnClose: true});  
menuStore.comparatorMap = {};

menuStore.comparatorMap['name']=function(a,b){
    if(a<b) return -1;
    if(a>b) return 1;
    return 0;
};

var sortAttributes = [{attribute: "name", ascending: true}];

function completed(items, findResult){
    // change the ItemFileReadStore data items to the sorted list
    menuStore.data = {
        identifier: 'name',
        label: 'name',
        items: items
    };
    // close the data request on menuStore so that it's data can be reset
    menuStore.close();
    // close does not destroy data, but closes connection. Bringing up the combobox dropdown will reopen the connection.
}

function error(errData, request){
    console.log("Failed in sorting data.");
}

//  dijit.byId("schoolMenu").store = menuStore;   // may need this if an older version of API is used 

dijit.byId("schoolMenu").set ("store", menuStore); // schoolMenu is dropdown menu  

menuStore.fetch({onComplete: completed, onError: error, sort: sortAttributes});    
} // end function