Permalink
Browse files

feat(project): Add sorting

  • Loading branch information...
jeremyvergnas committed May 11, 2016
1 parent b26395d commit 0fec60eee72a5ce3b1399ff6c588d01067e07571
Showing with 43 additions and 9 deletions.
  1. +4 −1 src/data-table.html
  2. +39 −8 src/data-table.js
@@ -3,7 +3,10 @@
<thead>
<tr>
<!-- Labels -->
<th repeat.for="columnLabel of columnLabels" t="${columnLabel.label}"></th>
<th click.delegate="doSort(columnLabel)" repeat.for="columnLabel of columnLabels">
<span if.bind="sortable === null || isObject(columnLabel.column)" t="${columnLabel.label}"></span>
<a if.bind="sortable !== null && !isObject(columnLabel.column)">${columnLabel.label} <i class="${sortingCriteria[columnLabel.column] ? (sortingCriteria[columnLabel.column] === 'desc' ? 'fa fa-caret-down' : 'fa fa-caret-up') : 'fa fa-sort'}"></i></a>
</th>
<!-- Actions -->
<th if.bind="destroy != null || update != null" t="Actions"></th>
@@ -18,9 +18,10 @@ export class DataTable {
@bindable update = null;
count = 0;
@bindable sortable = null;
@computedFrom('count', 'limit')
count = 0;
sortingCriteria = {};
@computedFrom('columns')
get columnLabels () {
@@ -111,13 +112,43 @@ export class DataTable {
}
load () {
this.repository.find({populate: false}, true)
.then(result => {
this.data = result;
})
.catch(error => {
let criteria = this.buildCriteria();
this.repository.find(criteria, true).then(result => {
this.data = result;
})
.catch(error => {
console.error('Something went wrong.', error);
});
});
}
buildCriteria() {
let criteria = {};
if (this.sortable !== null && Object.keys(this.sortingCriteria).length ) {
let propertyName = Object.keys(this.sortingCriteria)[0];
if (this.sortingCriteria[propertyName]) {
criteria['sort'] = propertyName + ' ' + this.sortingCriteria[propertyName];
}
}
return criteria;
}
doSort(columnLabel) {
if (this.sortable === null || this.isObject(columnLabel.column)) {
return;
}
if (this.sortingCriteria[columnLabel.column]) {
this.sortingCriteria[columnLabel.column] = (this.sortingCriteria[columnLabel.column] === 'asc' ? 'desc' : 'asc');
}
else {
this.sortingCriteria = {};
this.sortingCriteria[columnLabel.column] = 'asc';
}
this.load();
}
displayValue (row, propertyName) {
if (row[propertyName]) {
return row[propertyName];

0 comments on commit 0fec60e

Please sign in to comment.