5

When loading a dgrid from a dojo store, is there a way to specify a column to be sorted by default.

Say I have 2 columns, Name and Email, I want the name column sorted by default when the grid is first loaded. What I want is the equivalent of the user clicking on the 'Name' header (complete with the sort arrow indicating the sort direction).

Thanks, John

JohnB
  • 345
  • 3
  • 9

2 Answers2

9

You can do something like this :

var mygrid = new OnDemandGrid({
    store : someStore,
    queryOptions: {
        sort: [{ attribute: "name" }]
    }
    // rest of your grid properties
}, "someNode");
Philippe
  • 6,703
  • 3
  • 30
  • 50
  • of course the attribute would accept column name and direction. for example: [{attribute: 'id DESC'}] – Tariq Jan 10 '14 at 00:24
  • 1
    For dGrid, that would be [{ attribute: "id", descending: true }]. If you omit descending : true, then it's assumed the sort is ascending. See http://dojofoundation.org/packages/dgrid/tutorials/grids_and_stores/ – Philippe Jan 10 '14 at 19:41
  • 2
    The only drawback of this is the arrow (up or down - depending on your descending value) isn't displayed. It's only displayed when the user actually clicks on the table header. – maxxyme Jun 04 '14 at 17:32
  • This appears to set the sort arrow for me so maybe the grid has been updated grid.set('sort', [{ attribute: this.defaultSortField, descending: this.defaultSortDirDesc }] ) – David Wilton Jun 19 '15 at 05:53
0

dgrid 1.1.0 - set initial/default sort order

    var TrackableRest = declare([Rest, SimpleQuery, Trackable]);
    var store = new TrackableRest({target: apiUrl, useRangeHeaders: true, idProperty: 'id'});
    var grid = new (declare([OnDemandGrid, Selection, Editor]))({
        collection: store,
        sort: [{"property":"name", "descending": false}],
        className: "dgrid-autoheight",
        columns: {
            id: {
                label: core.id
            },
            category_text: {
                label: asset.category
            },
            name: {
                label: asset.model,
            },
user2182349
  • 9,569
  • 3
  • 29
  • 41
  • The proper value for sort attribute is an array of objects: [ { property: ''name", descending: true } ] – keemor Feb 02 '17 at 11:13