Skip to content

Commit d90d4e5

Browse files
committed
Fixed #140
1 parent 1662599 commit d90d4e5

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

src/component/table.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,17 @@ angular.module('ngTasty.component.table', [
299299
if ($scope.theadDirective && $scope.header.columns.length) {
300300
reverse = $scope.header.sortOrder === 'asc' ? false : true;
301301
listSortBy = [function(item) {
302-
return item[$scope.header.sortBy];
302+
return $scope.header.sortBy.split('.')
303+
.reduce(function (previousValue, currentValue) {
304+
return previousValue[currentValue];
305+
}, item);
303306
}];
304307
if ($scope.header.columns[0].key !== $scope.header.sortBy) {
305308
listSortBy.push(function(item) {
306-
return item[$scope.header.columns[0].key];
309+
return $scope.header.columns[0].key.split('.')
310+
.reduce(function (previousValue, currentValue) {
311+
return previousValue[currentValue];
312+
}, item);
307313
});
308314
}
309315
if ($scope.header.sortBy) {
@@ -548,6 +554,13 @@ angular.module('ngTasty.component.table', [
548554
});
549555
}
550556

557+
function cleanSortBy (sortBy) {
558+
if (sortBy) {
559+
return $filter('cleanFieldName')(sortBy);
560+
}
561+
return undefined;
562+
}
563+
551564
scope.setColumns = function () {
552565
var width, i, active, sortable, sort,
553566
isSorted, isSortedCaret;
@@ -589,14 +602,14 @@ angular.module('ngTasty.component.table', [
589602
'each column table header');
590603
}
591604
sort = $filter('cleanFieldName')(column.key);
592-
if (scope.header.sortBy === '-' + sort) {
605+
if (cleanSortBy(scope.header.sortBy) === '-' + sort) {
593606
if (tastyTable.config.bootstrapIcon) {
594607
isSorted = '';
595608
isSortedCaret = 'caret';
596609
} else {
597610
isSorted = scope.iconDown;
598611
}
599-
} else if (scope.header.sortBy === sort) {
612+
} else if (cleanSortBy(scope.header.sortBy) === sort) {
600613
if (tastyTable.config.bootstrapIcon) {
601614
isSorted = 'dropup';
602615
isSortedCaret = 'caret';
@@ -627,7 +640,7 @@ angular.module('ngTasty.component.table', [
627640
}
628641
var columnName, sortOrder;
629642
columnName = $filter('cleanFieldName')(column.key);
630-
if (scope.header.sortBy === columnName) {
643+
if (cleanSortBy(scope.header.sortBy) === columnName) {
631644
sortOrder = 'dsc';
632645
} else {
633646
sortOrder = 'asc';

src/component/test/fixture.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,24 @@ angular.module('mockedAPIResponse',[])
137137
],
138138
'rows': [
139139
{
140-
'name': 'Andytown Coffee Roasters',
140+
'name': 'Blue Bottle',
141+
'metadata': {
142+
'star': '★★★★★',
143+
'sf-location': 'Hayes Valley'
144+
}
145+
},
146+
{
147+
'name': 'Biscoff Coffee Corner',
141148
'metadata': {
142149
'star': '★★★',
143-
'sf-location': 'Outer Sunset'
150+
'sf-location': 'Fisherman’s Wharf'
151+
}
152+
},
153+
{
154+
'name': 'Flywheel Coffee Roasters',
155+
'metadata': {
156+
'star': '★★★★★',
157+
'sf-Location': 'Upper Haight'
144158
}
145159
},
146160
{
@@ -151,17 +165,17 @@ angular.module('mockedAPIResponse',[])
151165
}
152166
},
153167
{
154-
'name': 'Biscoff Coffee Corner',
168+
'name': 'Andytown Coffee Roasters',
155169
'metadata': {
156170
'star': '★★★',
157-
'sf-location': 'Fisherman’s Wharf'
171+
'sf-location': 'Outer Sunset'
158172
}
159173
},
160174
{
161-
'name': 'Blue Bottle',
175+
'name': 'CoffeeShop',
162176
'metadata': {
163-
'star': '★★★★★',
164-
'sf-location': 'Hayes Valley'
177+
'star': '★★★',
178+
'sf-location': 'Bernal Heights'
165179
}
166180
},
167181
{

src/component/test/table.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,8 +521,8 @@ describe('Component: table', function () {
521521
' <tbody>'+
522522
' <tr ng-repeat="row in rows">'+
523523
' <td>{{ row.name }}</td>'+
524-
' <td>{{ row.star }}</td>'+
525-
' <td>{{ row[\'sf-Location\'] }}</td>'+
524+
' <td>{{ row.metadata.star }}</td>'+
525+
' <td>{{ row.metadata[\'sf-Location\'] }}</td>'+
526526
' </tr>'+
527527
' </tbody>'+
528528
'</table>');
@@ -543,7 +543,7 @@ describe('Component: table', function () {
543543
expect(element.scope().header.columns[1].key).toEqual('metadata.star');
544544
expect(element.scope().header.columns[2].key).toEqual('metadata.sf-location');
545545
expect(element.scope().header.columns.length).toEqual(3);
546-
expect(element.scope().rows.length).toEqual(5);
546+
expect(element.scope().rows.length).toEqual(7);
547547
expect(element.scope().pagination.count).toEqual(5);
548548
expect(element.scope().pagination.page).toEqual(1);
549549
expect(element.scope().pagination.pages).toEqual(1);

0 commit comments

Comments
 (0)