YUI 3.x Home -

YUI Library Examples: DataTable Widget: Column Sorting

DataTable Widget: Column Sorting

The Y.Plugin.DataTableSort plugin adds column sorting functionality to a basic DataTable.

To add column sorting functionality to any basic datatable, simply call .plug(Y.Plugin.DataTableSort). The DataTableSort plugin is available in the datatable-sort submodule. To enable sorting, you must define sortable:true in each column definition.

  1. YUI().use("datatable-sort", function(Y) {
  2. var cols = [
  3. {key:"a", label:"Sortable Column A", sortable:true},
  4. {key:"b", label:"Not Sortable Column B"},
  5. {key:"c", label:"Sortable Column C", sortable:true}
  6. ],
  7. data = [
  8. {a:"1A", b:"1B", c:"1C"},
  9. {a:"2A", b:"2B", c:"2C"},
  10. {a:"3A", b:"3B", c:"3C"}
  11. ],
  12.  
  13. dt = new Y.DataTable.Base({columnset:cols, recordset:data}).
  14. plug(Y.Plugin.DataTableSort).render("#sort");
  15. });
YUI().use("datatable-sort", function(Y) {
    var cols = [
        {key:"a", label:"Sortable Column A", sortable:true},
        {key:"b", label:"Not Sortable Column B"},
        {key:"c", label:"Sortable Column C", sortable:true}
    ],
    data = [
        {a:"1A", b:"1B", c:"1C"},
        {a:"2A", b:"2B", c:"2C"},
        {a:"3A", b:"3B", c:"3C"}
    ],
 
    dt = new Y.DataTable.Base({columnset:cols, recordset:data}).
        plug(Y.Plugin.DataTableSort).render("#sort");
});
Configurable Trigger

By default, sorts will be triggered when the theadCellClick event is fired by the DataTable. You can set this to any other DataTable custom event via the trigger attribute of the DataTableSort constructor.

A related change worth making is to remove the link from the TH content, since the user will not be clicking to sort in this implementation. Simply set the template attribute to be the unadorned "{value}" string.

  1. YUI().use("datatable-sort", function(Y) {
  2. var cols = [
  3. {key:"a", label:"Mouseenter to Sort A", sortable:true},
  4. {key:"b", label:"Not Sortable Column B"},
  5. {key:"c", label:"Mouseenter to Sort C", sortable:true}
  6. ],
  7. data = [
  8. {a:"1A", b:"1B", c:"1C"},
  9. {a:"2A", b:"2B", c:"2C"},
  10. {a:"3A", b:"3B", c:"3C"}
  11. ],
  12.  
  13. dt = new Y.DataTable.Base({columnset:cols, recordset:data}).
  14. plug(Y.Plugin.DataTableSort, {trigger:"theadMouseenter", template:"{value}"}).
  15. render("#mouseenter");
  16. });
YUI().use("datatable-sort", function(Y) {
    var cols = [
        {key:"a", label:"Mouseenter to Sort A", sortable:true},
        {key:"b", label:"Not Sortable Column B"},
        {key:"c", label:"Mouseenter to Sort C", sortable:true}
    ],
    data = [
        {a:"1A", b:"1B", c:"1C"},
        {a:"2A", b:"2B", c:"2C"},
        {a:"3A", b:"3B", c:"3C"}
    ],
 
    dt = new Y.DataTable.Base({columnset:cols, recordset:data}).
        plug(Y.Plugin.DataTableSort, {trigger:"theadMouseenter", template:"{value}"}).
        render("#mouseenter");
});

Copyright © 2010 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings