Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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
share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

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
share|improve this answer
    
Thanks for reply..I am using 3.1, which I beleive is also Dojo v1.8....the last line in the code above (I believe) sets the store? dijit.byId("schoolMenu").set ("store", menuStore); Is this method different than setStore? –  JasonBK Feb 21 '13 at 21:24
    
I've updated my answer. Please take a look at it. –  raykendo Feb 21 '13 at 22:10
    
Thanks, I will try that! –  JasonBK Feb 27 '13 at 13:51
    
Hello, I am finally coming back to this after many months! I am using ComboBoxes; I tried your combobox code, but I get an error saying "combobox.store.close is not a function" in Firebug. I do not get an error if I comment out this one line...again, the list is sorted (I can see the sorted list in Firebug), but the combobox still displays the unsorted list. Thoughts? –  JasonBK Jul 9 '13 at 20:22
1  
I've updated my answer again. Hope this helps. –  raykendo Jul 10 '13 at 18:16
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.