GitHub - mattiash/angular-tablesort: Sort angularjs tables easily (original) (raw)
AngularJS Tablesort
Allow tables to be sorted by clicking their headings.
Web site: http://mattiash.github.io/angular-tablesort
Background
When you use jquery to build your web-pages, it is very easy to add sorting functionality to your tables - include tablesorter and annotate your column headings slightly to tell it what type of data your table contains.
The goal with this module is to make it just as easy to add sorting to AngularJS tables, but with proper use of angular features and not jQuery.
Click once on a heading to sort ascending, twice for descending. Use shift-click to sort on more than one column.
Additionally, these directives also make it easy to add a default row that is shown in empty tables to make it explicit that the table is intentionally empty and not just broken.
Installation
bower install angular-tablesort
or
npm install angular-tablesort
Usage
Include the script in your markup
Include the module in your app
angular.module('myApp', ['tableSort']);
The following code generates a table that can be sorted by clicking on the table headings:
Id | Name | Price | Quantity | Date Ordered |
---|---|---|---|---|
#{{item.Id}} | {{item.Name}} | {{item.Price | currency}} | {{item.Quantity}} | {{item.OrderDate | date:"medium"}} |
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 three filters parseInt
, parseFloat
, and parseDate
, but any filter can be used.
The ts-name
attribute can be set to declare a unique name for an expression which will be used in the event fired when sorting has changed.
The ts-default
attribute can be set on one or more columns to sort on them in ascending order 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.
Alternatively, ts-repeat-start
and ts-repeat-end
may be used to compliment the ng-repeat-start
and ng-repeat-end
directives.
By default, the sorting will be done as the last operation in the ng-repeat expression. To override this behavior, use an explicit tablesort
directive as part of your ng-repeat expression. E.g.
This will first select the first 10 items in items
and then sort them. Alternatively, you can insert an explicit tablesort in the pipe:
This will first sort the rows according to your specification and then only show the first 10 rows.
If the ng-repeat
expression contains a track by
statement (which is generally a good idea), that expression will be used to provide a stable sort result.
Events
When changing sorting in the table, an event named tablesort:sortOrder
will be emitted which contains an array of all current sorting definitions. These sorting definitions could be used to set up sorted data retrieval when using other directives that handle things like pagination or filtering.
scope.scope.scope.on('tablesort:sortOrder', (event, sortOrder) => {
self.sortOrder = sortOrder.map(o => {
return ${o.name} ${o.order ? 'ASC' : 'DESC'}
;
});
});
CSS
All table headings that can be sorted on is styled with the 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.
Empty Tables
By default, the content for the empty table cell is set to "No Items"
, however it can be changed via the noDataText
configuration option (see below). It is inserted as one <td>
spanning all columns and placed inside a <tr class="showIfLast">
, which is placed at the top of each table.
The message can be customized for each table by specifying the ts-no-data-text
attribute on the same element as the ts-wrapper
.
Property | Type | Default | Description |
---|---|---|---|
filterTemplate | string | "" | HTML string template for filtering the table. This will be included before the element with ts-wrapper specified on it. See example above. |
filterFunction | function | undefined | A function that will be called for every item being iterated over in the table. This function will be passed the object being iterated over as the first parameter. It should return a boolean value as to include the item or not. (This can be overridden per-table) |
itemNameSingular | string | "item" | The default singular version of the name for the items being iterated over. (This can be overridden per-table) |
itemNamePlural | string | itemNameSingular + "s" | The default plural version of the name for the items being iterated over. This just appends "s" to the singular name, which should work for most words in English. (This can be overridden per-table) |
noDataText | string | "No " + itemNamePlural | The text that displays in the .showIfLast cell shown when a table is empty. (This can be overridden per-table) |
wrappingElementClass | string | "" | Wrap the |
paginationTemplate | string | "" | HTML string template for paging the table. This will be included after the element with ts-wrapper specified on it. See example above. |
perPageOptions | array of number | [10, 25, 50, 100] | The options for how many items to show on each page of results. (This can be overridden per-table) |
perPageDefault | number | perPageOptions[0] | The default number of items for show on each page of results. By default, it picks the first item in the perPageOptions array. (This can be overridden per-table) |
Here's an example of how to change an option
angular .module('myApp') .config(['tableSortConfigProvider', function(tableSortConfigProvider){ tableSortConfigProvider.noDataText = "This table has nothing to show!"; tableSortConfigProvider.wrappingElementClass = "table-reponsive"; } ]);
Filtering & Pagination Templates
By default, table filtering & pagination are supported, but not enabled so that you may use any UI and any 3rd party angular code to do these types of things.
To set up these features, you must provide some configuration HTML string templates. These will be the default templates for filtering & pagination for all tables use in the same app unless that feature is specifically disabled on a per-table basis.
Here is an example of one way to set up the templates for an app that uses bootstrap and the Angular-UI Bootstrap pagination directive
angular .module('myApp') .config(['tableSortConfigProvider', function(tableSortConfigProvider){ var filterString = "
var pagerString = "<div class='text-right'>";
pagerString += "<small class='text-muted'>Showing {{CURRENT_PAGE_RANGE}} {{FILTERED_COUNT === 0 ? '' : 'of'}} ";
pagerString += "<span ng-if='FILTERED_COUNT === TOTAL_COUNT'>{{TOTAL_COUNT | number}} {{TOTAL_COUNT === 1 ? ITEM_NAME_SINGULAR : ITEM_NAME_PLURAL}}</span>";
pagerString += "<span ng-if='FILTERED_COUNT !== TOTAL_COUNT'>{{FILTERED_COUNT | number}} {{FILTERED_COUNT === 1 ? ITEM_NAME_SINGULAR : ITEM_NAME_PLURAL}} (filtered from {{TOTAL_COUNT | number}})</span>";
pagerString += "</small> ";
pagerString += "<uib-pagination style='vertical-align:middle;' ng-if='ITEMS_PER_PAGE < TOTAL_COUNT' ng-model='CURRENT_PAGE_NUMBER' ";
pagerString += "total-items='FILTERED_COUNT' items-per-page='ITEMS_PER_PAGE' max-size='5' force-ellipses='true'></uib-pagination> ";
pagerString += "<div class='form-group' style='display:inline-block;'>";
pagerString += "<select class='form-control' ng-model='ITEMS_PER_PAGE' ng-options='opt as (opt + \" per page\") for opt in PER_PAGE_OPTIONS'></select>";
pagerString += "</div>";
pagerString += "</div>";
tableSortConfigProvider.paginationTemplate = pagerString;
}
]);
There are several tokens that can be used in the templates which will be replaced with the proper Angular expressions.
Token | Description |
---|---|
TOTAL_COUNT | The number for the total count of items in the table |
FILTERED_COUNT | The number for the total count of items in the table after the filter has been applied |
FILTER_STRING | The string used for the ng-model of the text filter |
PER_PAGE_OPTIONS | The array of numbers for the various page size options |
ITEMS_PER_PAGE | The number for the selected number of items to display per page (the selected item from PER_PAGE_OPTIONS) |
CURRENT_PAGE_NUMBER | The number for the page that is currently being viewed |
CURRENT_PAGE_RANGE | The number for the current viewable range of pages |
ITEM_NAME_SINGULAR | The singular version of the name of the items being iterated over |
ITEM_NAME_PLURAL | The plural version of the name of the items being iterated over |
Item Names
The name of the things listed in the table can be displayed in the filtering and pagination templates. They are named "items"
collectively and "item"
individually by default, but this can be customized in the global config, and per-table to be more specific as to what is being listed.
On a table just set the ts-item-name
attribute on the same element as ts-wrapper
. Set this as the singular version of the word, not the plural.
Id | Name | Price | Quantity | Date Ordered |
---|
Id | Name | enabled |
---|---|---|
{{item.id}} | {{item.name}} | {{item.enabled}} |
Wrapping The Table Element
Certain libraries like Bootstrap allow for tables to become responsive when at a smaller screen size by wrapping it in a table-reponsive
class. If this is desired on a table using angular-tablesort this can become and issue if the filtering or pagination are used since they will also be inside of this wrapping element, which will cause some display issues.
This can be configured globally with the wrappingElementClass
configuration option, or per-table with the ts-wrapping-element-class
attribute
Name |
---|