GameBuilder
GameBuilder

Reputation: 1189

Sorting JSON and displaying in Tree using DOJO

I am learning DOJO 1.6.

I have data

var data = [
        { FirstName: 'xyz', Lastname: 'QSD', rollNo: '1', EntryDate: '2012-09-11T17:35:31.835+02:00' },
        { FirstName: 'abc', Lastname: 'qgr', rollNo: '2', EntryDate: '2012-08-11T17:35:31.835+02:00' }
        { FirstName: 'ert', Lastname: 'fgd', rollNo: '3', EntryDate: '2012-18-11T17:35:31.835+02:00' }
    ];

I want to sort it with respect to Last name or EntryDate and display in a tree format.

Thanks in Advance.


Multiple root data

data: [
            { id: 'world', name:'The earth', type:'planet', population: '6 billion'},
            { id: 'AF', name:'Africa', type:'continent', population:'900 million', area: '30,221,532 sq km',
                    timezone: '-1 UTC to +4 UTC', parent: 'world'},
                { id: 'EG', name:'Egypt', type:'country', parent: 'AF' },
                { id: 'KE', name:'Kenya', type:'country', parent: 'AF' },
                    { id: 'Nairobi', name:'Nairobi', type:'city', parent: 'KE' },
                    { id: 'Mombasa', name:'Mombasa', type:'city', parent: 'KE' },
                { id: 'SD', name:'Sudan', type:'country', parent: 'AF' },
                    { id: 'Khartoum', name:'Khartoum', type:'city', parent: 'SD' },
            { id: 'AS', name:'Asia', type:'continent', parent: 'world' },
                { id: 'CN', name:'China', type:'country', parent: 'AS' },
                { id: 'IN', name:'India', type:'country', parent: 'AS' },
                { id: 'RU', name:'Russia', type:'country', parent: 'AS' },
                { id: 'MN', name:'Mongolia', type:'country', parent: 'AS' },
            { id: 'OC', name:'Oceania', type:'continent', population:'21 million', parent: 'world'},
            { id: 'EU', name:'Europe', type:'continent', parent: 'world' },
                { id: 'DE', name:'Germany', type:'country', parent: 'EU' },
                { id: 'FR', name:'France', type:'country', parent: 'EU' },
                { id: 'ES', name:'Spain', type:'country', parent: 'EU' },
                { id: 'IT', name:'Italy', type:'country', parent: 'EU' },
            { id: 'NA', name:'North America', type:'continent', parent: 'world' },
            { id: 'SA', name:'South America', type:'continent', parent: 'world' }
        ],

Upvotes: 2

Views: 2125

Answers (1)

mschr
mschr

Reputation: 8641

Javascript Array has a native function, called sort. This will off-the-shelf sort the values alphabetically. For the purpose of sorting non-string-values, we need to supply a sortingfunction. Like so, in regards to Lastname:

data.sort(function(a,b) {
    var _A=a.Lastname.toLowerCase(), 
        _B=b.Lastname.toLowerCase();

    if (_A < _B) //sort string ascending
      return -1 
    if (_A > _B)
      return 1
    return 0 //default return value (no sorting)
});

If youre sorting against a date, you would need to initialize _A and _B to a Date.

However, if youre aiming to represent the data in a dijit.Tree, there's inbuilt method for sorting the Store, lets wrap data into a dojo/data/ItemFileReadStore and show in a tree. Tree will have a model, using ItemFileWriteStore - so that items can be modified:

var sortableStore = new dojo.data.ItemFileReadStore({
    data: {
        identifier: 'rollNo',
        items: data
    },
    comperatorMap: {
        'EntryDate' : function(a,b) {
            var _A = new Date(a), _B = new Date(b);
            if(_A > _B) return 1;
            else if(_A == _B) return 0;
            else return -1;
        }
});

Using 'store.fetch()' API while setting the 'sort' parameter, you control the order of returned items. The EntryDate you will have to create a comperatorMap of functions, as with Array.sort() in order to sort it properly. See the documentation.

var model = new dijit.tree.ForestStoreModel({
    rootLabel: 'Names',
    store: new dojo.data.ItemFileWriteStore({
        data: {
            identifier: 'rollNo',
            items: data, 
  // blank, initially - can fill in with 'data' to show non-sorted items until sort is called
            label: 'FirstName'
        }
    }) // blank itemsstore
});

var tree = new dijit.Tree({
    model: model
});

OK, All set - but problem with .fetch is, it runs with callbacks (onComplete) and is difficult to control in a recursive manner. Instead, the functionality put in THIS FIDDLE duplicates the store data and then sorts it via native array sort - as opposed to using SimpleQueryEngine.

This will prove to give more reliable results - but does mess with DnD controllers and persist flag..

See how store can sort its items returned by fetch here: fiddle. This however only sorts one 'level' at a time and does not perform deep sorts.
IMO: The proper implementation of sort is a serverside sort, directly in the database query.

http://dojotoolkit.org/reference-guide/1.8/dojo/data/ItemFileReadStore.html#custom-sorting

http://dojotoolkit.org/reference-guide/1.8/dijit/Tree.html

Upvotes: 3

Related Questions