Skip to content

Commit

Permalink
[FEATURE] Sort by child objects
Browse files Browse the repository at this point in the history
  • Loading branch information
KOM committed Oct 24, 2016
1 parent 08b2fe7 commit 9d6e6ae
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 @@ -144,6 +144,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 9d6e6ae

Please sign in to comment.