Example: Column Sorting

NOTICE: This example is for the deprecated version of DataTable. The components referred to here will be removed in future versions of YUI. If you are unable to upgrade to the latest DataTable version due to unresolvable feature conflicts from version 3.4.1 or prior, please file a ticket.

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

Implementing Sortable Columns

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

YUI().use("datatable-sort-deprecated", function(Y) {
    var cols = [
        {key:"Company", label:"Click to Sort Column A", sortable:true},
        {key:"Phone", label:"Not Sortable Column B"},
        {key:"Contact", label:"Click to Sort Column C", sortable:true}
    ],
    data = [
        {Company:"Company Bee", Phone:"415-555-1234", Contact:"Sally Spencer"},
        {Company:"Acme Company", Phone:"650-555-4444", Contact:"John Jones"},
        {Company:"Industrial Industries", Phone:"408-555-5678", Contact:"Robin Smith"}
    ],
    table = new Y.DataTable.Base({
        columnset: cols,
        recordset: data,
        summary: "Contacts list",
        caption: "Table with simple column sorting"
    }).plug(Y.Plugin.DataTableSort)
      .render("#sort");
});
Pre-sorted Data

Often data is already sorted when it loads, and we want the data to reverse-sort the first time the user clicks on the column. In that case, define the lastSortedBy value in the DataTableSort plugin to indicate the field that is already sorted and whether it is sorted in "asc" or "desc" order.

YUI().use("datatable-sort-deprecated", function(Y) {
    var cols = [
        {key:"Company", label:"Click to Sort Column A", sortable:true},
        {key:"Phone", label:"Not Sortable Column B"},
        {key:"Contact", label:"Click to Sort Column C", sortable:true}
    ],
    presortedData = [
        {Company:"Acme Company", Phone:"415-555-1234", Contact:"John Jones"},
        {Company:"Company Bee", Phone:"650-555-4444", Contact:"Sally Spencer"},
        {Company:"Industrial Industries", Phone:"408-555-5678", Contact:"Robin Smith"}
    ],
    table = new Y.DataTable.Base({
        columnset: cols,
        recordset: presortedData,
        summary: "Contacts list",
        caption: "This table loads with presorted data"
    }).plug(Y.Plugin.DataTableSort, {
            lastSortedBy: {
                key: "Company",
                dir: "asc"
            }
        })
      .render("#presorted");
});
Configurable Trigger

By default, sorts will be triggered when the user clicks on the TH element of a sortable column, which fires a theadCellClick DataTable event. You can set this to be any other DataTable custom event by setting the trigger attribute in the DataTableSort plugin constructor. Note: Since the trigger attribute is initOnly, this value can only be set in the plugin constructor and not after the plugin has been instantiated.

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.

Note: touch devices don't support the dblclick event, so the last table won't be sortable for all users. Be mindful of your audience when configuring triggers.

YUI().use("datatable-sort-deprecated", function(Y) {
    var cols = [
        {key:"Company", label:"Dblclick to Sort A", sortable:true},
        {key:"Phone", label:"Not Sortable Column B"},
        {key:"Contact", label:"Dblclick to Sort C", sortable:true}
    ],
    data = [
        {Company:"Company Bee", Phone:"415-555-1234", Contact:"Sally Spencer"},
        {Company:"Acme Company", Phone:"650-555-4444", Contact:"John Jones"},
        {Company:"Industrial Industries", Phone:"408-555-5678", Contact:"Robin Smith"}
    ],
    table = new Y.DataTable.Base({
        columnset: cols,
        recordset: data,
        summary: "Contacts list",
        caption: "This table sorts on doubleclick"
    }).plug(Y.Plugin.DataTableSort, {
            trigger: "theadCellDblclick",
            template: "{value}"
        })
      .render("#dblclick");
});