Skip to content

Commit

Permalink
Merge pull request #42 from kelvynmarte/feature/child-objects-sorting
Browse files Browse the repository at this point in the history
[FEATURE] Sort by child objects
  • Loading branch information
mariuszfoltak authored Oct 25, 2016
2 parents 7462ed9 + 9d6e6ae commit 06e9c1b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/DataTable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,27 @@ describe("DataTable directive tests", ()=> {
{name: 'Claire', age: 16}
]);
});

it("should sort data by child property value", ()=>{
let newData = [
{name: 'Claire', city: { zip: '51111'}},
{name: 'Anna', city: { zip: '31111'}},
{name: 'Claire', city: { zip: '41111'}},
{name: 'Claire', city: { zip: '11111'}},
{name: 'Anna', city: { zip: '21111'}}
];
datatable.ngOnChanges({inputData: new SimpleChange(datatable.inputData, newData)});
datatable.setSort("city.zip", "asc");
datatable.ngDoCheck();

expect(datatable.data).toEqual([
{name: 'Claire', city: { zip: '11111'}},
{name: 'Anna', city: { zip: '21111'}},
{name: 'Anna', city: { zip: '31111'}},
{name: 'Claire', city: { zip: '41111'}},
{name: 'Claire', city: { zip: '51111'}},
]);
});
});

describe("data change", ()=> {
Expand Down
5 changes: 4 additions & 1 deletion src/DataTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ export class DataTable implements OnChanges, DoCheck {

private caseInsensitiveIteratee(sortBy: string) {
return (row: any): any => {
var value = row[sortBy];
var value = row;
for (let sortByProperty of sortBy.split('.')){
value = value[sortByProperty];
}
if (value && typeof value === 'string' || value instanceof String) {
return value.toLowerCase();
}
Expand Down

0 comments on commit 06e9c1b

Please sign in to comment.