使用DOJO对JSON进行排序并在Tree中显示

时间:2012-10-01 16:52:56

标签: dojo

我正在学习DOJO 1.6。

我有数据

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' }
    ];

我想根据姓氏或EntryDate对其进行排序,并以树格式显示。

先谢谢。


多个根数据

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' }
        ],

1 个答案:

答案 0 :(得分:3)

Javascript Array有一个原生函数,名为sort。这将按字母顺序对现成的值进行排序。为了排序非字符串值,我们需要提供一个排序函数。就像这样,关于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)
});

如果您按日期排序,则需要将_A_B初始化为Date

但是,如果您的目标是在data中代表dijit.Tree,那么可以使用内置方法对Store进行排序,将data包裹到dojo/data/ItemFileReadStore中并在树上显示。树将使用ItemFileWriteStore建立模型,以便可以修改项目:

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;
        }
});

在设置'sort'参数时使用'store.fetch()'API,您可以控制返回项的顺序。您必须使用Array.sort()创建函数的comperatorMap才能对EntryDate进行正确排序。请参阅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
});

好的,所有设置 - 但是.fetch的问题是,它运行回调(onComplete)并且难以以递归方式控制。相反,放入THIS FIDDLE的功能会复制商店数据,然后通过本机数组排序对其进行排序 - 而不是使用SimpleQueryEngine

这将证明可以提供更可靠的结果 - 但确实混淆了DnD控制器并持续存在标志..

了解商店如何对fetch此处返回的商品进行排序:fiddle。然而,这只能一次排序一个“级别”并且不执行深度排序 IMO: sort的正确实现是服务器端排序,直接在数据库查询中

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

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