O'Reilly logo

Ext JS 4 Web Application Development Cookbook by Stuart Ashworth, Andrew Duncan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Sorting tree nodes

Sorting the data that's asynchronously loaded to a tree is straight-forward with Ext JS. This recipe, demonstrates how to sort a tree's data on the client-side.

How to do it...

  1. Create a store with the sorters configuration option defined with an object specifying property and direction values:
    var store = Ext.create('Ext.data.TreeStore', {
        proxy: {
            type: 'ajax',
            url: 'treeData.json'
        },
        root: {
            text: 'Countries',
            expanded: true
        },
        sorters: [{
            property: 'text',
            direction: 'ASC' //for descending change to 'DESC'
        }]
    });
  2. Create a tree to load the sorted data to.
    Ext.create('Ext.tree.Panel', {
        title: 'Countries',
        width: 500,
        height: 200,
        store: store,
        rootVisible: false,
        renderTo: Ext.getBody(),
        style: 'margin: 50px'
    });

How it works... ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required