AngularJS tablesort made easy

Make it possible to sort your AngularJS tables without writing any javascript code.

Introduction

I use tables a lot to present data to the user. Most of the time, I want to allow the user to sort the table on the data in the columns by clicking on the table headers. When I used jQuery as the basis for my web-pages, I used jQuery tablesorter to make most of the tables sortable. For simple tables, this meant adding one line of javascript to tell jQuery tablesorter which tables to sort.

With AngularJS, it is fairly easy to make tables sortable, but I wanted something as simple as jQuery tablesorter. This is why I wrote angular-tablesort.

Features

  • Sort by a column by clicking the header for that column
  • Toggle between ascending and descending order by clicking the header again
  • Sort by multiple columns by holding shift while clicking
  • The sort expression is decoupled from the content of the cells. This allows you to sort for example dates in correct order
  • Add a default row to empty tables to signal that the table shall be empty

Sample table

Id Name Price Quantity
{{item.Id}} {{item.Name}} {{item.Price | currency}} {{item.Quantity}}

Usage

The table shown above was generated by the following code:

<table class="table" ts-wrapper>
  <thead>
    <tr>
      <th ts-criteria="Id">Id</th>
      <th ts-criteria="Name|lowercase" ts-default>Name</th>
      <th ts-criteria="Price|parseFloat">Price</th>
      <th ts-criteria="Quantity|parseInt">Quantity</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="item in items track by item.Id" ts-repeat>
      <td>{{item.Name}}</td>
      <td>{{item.Price | currency}}</td>
      <td>{{item.Quantity}}</td>
    </tr>
  </tbody>
</table>

The ts-wrapper attribute must be set on element that surrounds both the headings and the ng-repeat statement.

The ts-criteria attribute tells tablesort which expression it should sort on when that element is clicked. Normally, the ts-criteria is the same as the expression that is shown in the column, but it doesn't have to be. The ts-criteria can also be filtered using the normal AngularJS filter syntax. Tablesort includes two filters parseInt and parseFloat that use the javascript functions of the same name, but any filter can be used.

The ts-default attribute can be set on one or more columns to sort on them by default. To sort in descending order, set ts-default to "descending"

The ts-repeat attribute must be set on the element with ng-repeat.

You must also make sure that your app depends on tableSort:

var myApp = angular.module( 'myApp', ['tableSort' ] );

CSS

All table headings that can be sorted on is styled with css-class tablesort-sortable. The table headings that the table is currently sorted on is styled with tablesort-asc or tablesort-desc classes depending on the sort-direction. A stylesheet is included to show that it works, but you probably want to build your own.

The content and look of the default data for empty tables is controlled via css. It is inserted as one empty <td> spanning all columns and placed inside a <tr> with class showIfLast. The <tr> is placed at the top of each table. A sample style for showIfLast can be found in tablesort.css

License

The MIT License

Copyright (c) 2013 Mattias Holmlund, http://www.holmlund.se/mattias

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Download

The code is available on github.

bower

bower install angular-tablesort