Skip to content

Commit fe9eda8

Browse files
authored
Merge pull request #207 from iamisti/feature-innovative-sorting
Feature innovative sorting
2 parents e205535 + 46901f3 commit fe9eda8

File tree

48 files changed

+1692
-672
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1692
-672
lines changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ http://www.google.com/design/spec/components/data-tables.html
4242
- virtual-repeat
4343
- delete-row-callback
4444
- selected-row-callback
45-
- sortable-columns
4645
- animate-sort-icon
4746
- ripple-effect
4847
- ! title-overflow-handler
@@ -60,9 +59,8 @@ http://www.google.com/design/spec/components/data-tables.html
6059
[Column attributes (`mdt-column`)](#column-attributes)
6160
- align-rule
6261
- column-definition
63-
- ! sortable-rows-default
64-
- sort-by
6562
- column-filter
63+
- excludeFromColumnSelector
6664

6765
[Row attributes (`mdt-row`)](#data-row-attributes)
6866
- table-row-id
@@ -94,7 +92,6 @@ http://www.google.com/design/spec/components/data-tables.html
9492

9593
| Available | Params | Type | Details |
9694
| ---------------- | ---------------------------------------------- | ------------- | ------------- |
97-
|:white_check_mark:| sortable-columns | Boolean | optional, if enabled, sort data and display a sorted state in the column header. If the user clicks on a column that is already sorted, reverse the sort order and rotate the sort icon. Use `sortable-rows-default` attribute directive on a column which intended to be the default sortable column |
9895
|:white_check_mark:| animate-sort-icon | Boolean | optional, if enabled, sort icon will be animated on change |
9996
|:white_check_mark:| ripple-effect | Boolean | optional, if enabled, ripple effect will be applied on the column names when clicked |
10097
![Table with an ascending sorted column](http://material-design.storage.googleapis.com/publish/material_v_4/material_ext_publish/0B3mOPoJlxiFhMW1haUJDRWJKLUk/components_datatables_interaction_sortedcolumn.png)
@@ -113,6 +110,7 @@ http://www.google.com/design/spec/components/data-tables.html
113110
|:white_check_mark:| | title | String | The title of the table card |
114111
|:x: | | actionIcons | Boolean | Card action icons (header and footer) |
115112
|:white_check_mark:| | visible | Boolean | The visibility of the table card |
113+
|:white_check_mark:| | columnSelector | Boolean | enables the column selection for the table (you can disable certain columns from the list selection, using `exclude-from-column-selector`, see the related docs) |
116114
![Table card with header and footer](http://material-design.storage.googleapis.com/publish/material_v_4/material_ext_publish/0B3mOPoJlxiFhUTEwa21JUEtza0k/components_datatables_cards_tablecard.png)
117115

118116
| Available | Params | ChildParams | Type | Details|
@@ -225,10 +223,12 @@ Html support is available for `mdt-row`, see more: [Custom cell content (`mdt-cu
225223
|:white_check_mark:| column-definition | String | if provided, display a tooltip on hover. If sorting is enabled, display a light sort icon upon hover, which indicates that the column is sortable. |
226224
![Column definition on hover](http://material-design.storage.googleapis.com/publish/material_v_4/material_ext_publish/0B3mOPoJlxiFhenh5SWhFdFlyajg/components_datatables_interaction_tooltip.png)
227225

228-
| Available | Params | Type | Details |
229-
| -----------------| ---------------------------------------------- | ------------- | --------------- |
230-
|:x: | sortable-rows-default | - | When sortable-columns is applied to the table, it marks the column as the default sorting column |
231-
|:white_check_mark:| sort-by | Function | if provided, used as the iteratee during sort operations to transform the cell value to a value that can be ranked in order. |
226+
| Available | Params | ChildPArams | Type | Details |
227+
| -----------------| ---------------------------------------------- | -------------- | -------------- | --------------- |
228+
|:x: | sortable-rows-default | - | | When column-sort is applied to the table, it marks the column as the default sorting column |
229+
|:white_check_mark:| column-sort | | Boolean|Object | if provided, sort data and display a sorted state in the header. Clicking on a column which is already sorted will reverse the sort order and rotate the sort icon. |
230+
|:white_check_mark:| column-sort | true|false | | in case of boolean, true value enables the feature, false disables it (default is disabled) |
231+
|:white_check_mark:| column-sort | comparator | | in case of object, specifying a 'comparator' property which is a function for sorting the column data's. As every compare function, it gets two parameters and return with the compared result (-1,1,0) |
232232

233233
| Available | Params | ChildPArams | Type | Details |
234234
| ---------------- | ---------------------------------------------- | -------------------------|------------- | --------------- |
@@ -237,6 +237,7 @@ Html support is available for `mdt-row`, see more: [Custom cell content (`mdt-cu
237237
|:white_check_mark:| | valuesTransformerCallback| Function | optional, function which transforms the provided objects into strings to be able to show it visually in the column filter.|
238238
|:white_check_mark:| | placeholderText | Text | optional, placeholder which will show up as a default text (available only for `chips` and `dropdown` filter types |
239239
|:white_check_mark:| | filterType | Text | optional, defines the type of the filter you want to use. Available options are: `chips`, `checkbox`, `dropdown`. If you don't specify it, the default will be `chips` |
240+
|:white_check_mark:| exclude-from-column-selector | | Boolean | optional, excludes the column from the column selection feature |
240241
> When filters are applied to the columns, a third parameter will be applied to the `mdt-row-paginator` callback function.
241242
242243
# Data-Row attributes

app/modules/main/directives/header/mdtColumnDirective.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,28 @@
1717
* - 'left'
1818
* - 'right'
1919
*
20-
* @param {function()=} sortBy compareFunction callback for sorting the column data's. As every compare function,
21-
* should get two parameters and return with the comapred result (-1,1,0)
20+
* @param {boolean=|object=} columnSort sort data and display a sorted state in the header. Clicking on a column
21+
* which is already sorted will reverse the sort order and rotate the sort icon.
22+
*
23+
* When object is passed, then compare function can be passed for sorting the column data's. As every compare
24+
* function, it gets two parameters and return with the compared result (-1,1,0)
25+
*
26+
* Assignable values:
27+
* - true or false
28+
* - { comparator: function(a,b)}
29+
*
30+
* @param {object=} columnFilter if provided, user can activate column filter feature on the selected column
31+
*
32+
* Assignable properties:
33+
* - {Function=} valuesProviderCallback required, function which provides the values into the column filter. It must return with a promise which resolves an array of strings/objects
34+
* - {Function=} valuesTransformerCallback optional, function which transforms the provided objects into strings to be able to show it visually in the column filter
35+
* - {string=} placeholderText optional, placeholder which will show up as a default text (available only for `chips` and `dropdown` filter types
36+
* - {string=} filterType defines the type of the filter you want to use. Available options are: `chips`, `checkbox`, `dropdown`. If you don't specify it, the default will be `chips`
2237
*
2338
* @param {string=} columnDefinition displays a tooltip on hover.
2439
*
40+
* @param {boolean=} excludeFromColumnSelector disables the column selection for the applied column for the column select feature.
41+
*
2542
* @example
2643
* <pre>
2744
* <mdt-table>
@@ -39,16 +56,17 @@
3956
* </mdt-table>
4057
* </pre>
4158
*/
42-
function mdtColumnDirective($interpolate, ColumnFilterFeature){
59+
function mdtColumnDirective($interpolate, ColumnFilterFeature, ColumnSortFeature, ColumnSelectorFeature){
4360
return {
4461
restrict: 'E',
4562
transclude: true,
4663
replace: true,
4764
scope: {
4865
alignRule: '@',
49-
sortBy: '=',
5066
columnDefinition: '@',
51-
columnFilter: '=?'
67+
columnSort: '=?',
68+
columnFilter: '=?',
69+
excludeFromColumnSelector: '=?'
5270
},
5371
require: ['^mdtTable'],
5472
link: function ($scope, element, attrs, ctrl, transclude) {
@@ -59,12 +77,13 @@
5977
var cellValue = $interpolate(clone.html())($scope.$parent);
6078
var cellDataToStore = {
6179
alignRule: $scope.alignRule,
62-
sortBy: $scope.sortBy,
6380
columnDefinition: $scope.columnDefinition,
6481
columnName: cellValue
6582
};
6683

67-
ColumnFilterFeature.appendHeaderCellData($scope, cellDataToStore, mdtTableCtrl.dataStorage, element);
84+
ColumnFilterFeature.appendHeaderCellData($scope, cellDataToStore, mdtTableCtrl.dataStorage);
85+
ColumnSortFeature.appendHeaderCellData(cellDataToStore, $scope.columnSort);
86+
ColumnSelectorFeature.appendHeaderCellData(cellDataToStore, mdtTableCtrl.columnSelectorFeature, $scope.excludeFromColumnSelector);
6887

6988
mdtTableCtrl.dataStorage.addHeaderCellData(cellDataToStore);
7089
});

app/modules/main/directives/header/mdtGeneratedHeaderCellContentDirective.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
(function(){
22
'use strict';
33

4-
function mdtGeneratedHeaderCellContentDirective(ColumnFilterFeature){
4+
function mdtGeneratedHeaderCellContentDirective(ColumnFilterFeature, ColumnSortFeature){
55
return {
66
restrict: 'E',
77
templateUrl: '/main/templates/mdtGeneratedHeaderCellContent.html',
88
replace: true,
99
scope: false,
1010
require: '^mdtTable',
1111
link: function($scope, element, attrs, ctrl){
12-
ColumnFilterFeature.initGeneratedHeaderCellContent($scope, $scope.headerRowData, ctrl);
12+
ColumnFilterFeature.initGeneratedHeaderCellContent($scope, $scope.headerRowData, ctrl.mdtPaginationHelper, ctrl.dataStorage);
1313

1414
$scope.columnClickHandler = function(){
1515
ColumnFilterFeature.generatedHeaderCellClickHandler($scope, $scope.headerRowData, element);
16+
ColumnSortFeature.columnClickHandler($scope.headerRowData, ctrl.dataStorage, ctrl.mdtPaginationHelper, attrs.index);
1617
};
1718
}
1819
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(function(){
2+
'use strict';
3+
4+
function mdtGeneratedHeaderRowDirective(){
5+
return {
6+
restrict: 'A',
7+
templateUrl: '/main/templates/mdtGeneratedHeaderRow.html'
8+
};
9+
}
10+
11+
angular
12+
.module('mdDataTable')
13+
.directive('mdtGeneratedHeaderRow', mdtGeneratedHeaderRowDirective);
14+
}());

app/modules/main/directives/helpers/mdtSortHandlerDirective.js

Lines changed: 0 additions & 38 deletions
This file was deleted.

app/modules/main/directives/mdtTableDirective.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*
1717
* - `{boolean=}` `visible` - enable/disable table card explicitly
1818
* - `{string}` `title` - the title of the card
19+
* - `{boolean=}` `columnSelector` - enables the column selection for the table (you can disable certain columns from the list selection, using `exclude-from-column-selector`, see the related docs)
1920
* - `{array=}` `actionIcons` - (not implemented yet)
2021
*
2122
* @param {boolean=} selectableRows when set each row will have a checkbox
@@ -32,11 +33,6 @@
3233
* change and it'll show the number of selected rows and a delete icon on the right.
3334
* - 'persistentActions' - (not implemented yet)
3435
*
35-
* @param {boolean=} sortableColumns sort data and display a sorted state in the header. Clicking on a column which
36-
* is already sorted will reverse the sort order and rotate the sort icon.
37-
* (not implemented yet: Use `sortable-rows-default` attribute directive on a column which intended to be the
38-
* default sortable column)
39-
*
4036
* @param {function(rows)=} deleteRowCallback callback function when deleting rows.
4137
* At default an array of the deleted row's data will be passed as the argument.
4238
* When `table-row-id` set for the deleted row then that value will be passed.
@@ -112,9 +108,10 @@
112108
* </pre>
113109
*/
114110
function mdtTableDirective(TableDataStorageFactory,
115-
EditRowFeature,
111+
EditCellFeature,
116112
SelectableRowsFeature,
117113
PaginationFeature,
114+
ColumnSelectorFeature,
118115
_){
119116
return {
120117
restrict: 'E',
@@ -124,7 +121,6 @@
124121
tableCard: '=',
125122
selectableRows: '=',
126123
alternateHeaders: '=',
127-
sortableColumns: '=',
128124
deleteRowCallback: '&',
129125
selectedRowCallback: '&',
130126
saveRowCallback: '&',
@@ -148,16 +144,16 @@
148144
};
149145

150146
_setDefaultTranslations();
151-
152147
_initTableStorage();
153148

154149
PaginationFeature.initFeature($scope, vm);
150+
ColumnSelectorFeature.initFeature($scope, vm);
155151

156152
_processData();
157153

158154
// initialization of the storage service
159155
function _initTableStorage(){
160-
vm.dataStorage = TableDataStorageFactory.getInstance(vm.virtualRepeat);
156+
vm.dataStorage = TableDataStorageFactory.getInstance();
161157
}
162158

163159
// set translations or fallback to a default value
@@ -216,10 +212,11 @@
216212

217213
_injectContentIntoTemplate();
218214

219-
_initEditRowFeature();
215+
_initEditCellFeature();
220216
_initSelectableRowsFeature();
221217

222218
PaginationFeature.startFeature(ctrl);
219+
ColumnSelectorFeature.initFeatureHeaderValues($scope.dataStorage.header, ctrl.columnSelectorFeature);
223220

224221
function _injectContentIntoTemplate(){
225222
transclude(function (clone) {
@@ -257,12 +254,12 @@
257254
});
258255
}
259256

260-
function _initEditRowFeature(){
257+
function _initEditCellFeature(){
261258
//TODO: make it possible to only register feature if there is at least
262259
// one column which requires it.
263260
// for that we need to change the place where we register edit-row.
264261
// Remove mdt-row attributes --> do it in mdt-row attribute directive on mdtTable
265-
EditRowFeature.addRequiredFunctions($scope, ctrl);
262+
EditCellFeature.addRequiredFunctions($scope, ctrl);
266263
}
267264

268265
function _initSelectableRowsFeature(){

app/modules/main/directives/tablecard/mdtCardHeaderDirective.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
link: function($scope){
1313
$scope.isTableCardEnabled = false;
1414

15+
//TODO: move it to the feature file
16+
$scope.handleColumnChooserButtonClick = function(){
17+
if($scope.columnSelectorFeature.isEnabled){
18+
$scope.columnSelectorFeature.isActive = !$scope.columnSelectorFeature.isActive
19+
}
20+
};
21+
1522
if($scope.tableCard && $scope.tableCard.visible !== false){
1623
$scope.isTableCardEnabled = true;
1724
}

app/modules/main/factories/TableDataStorageFactory.js

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@
33

44
function TableDataStorageFactory($log, _){
55

6-
function TableDataStorageService(isVirtualRepeatEnabled){
6+
function TableDataStorageService(){
77
this.storage = [];
88
this.header = [];
99
this.customCells = {};
10-
11-
this.sortByColumnLastIndex = null;
12-
this.orderByAscending = true;
13-
this.isVirtualRepeatEnabled = isVirtualRepeatEnabled;
1410
}
1511

1612
TableDataStorageService.prototype.addHeaderCellData = function(ops){
@@ -70,43 +66,6 @@
7066
});
7167
};
7268

73-
TableDataStorageService.prototype.reverseRows = function(){
74-
this.storage.reverse();
75-
};
76-
77-
TableDataStorageService.prototype.sortByColumn = function(columnIndex, iteratee){
78-
if(this.sortByColumnLastIndex === columnIndex){
79-
this.reverseRows();
80-
81-
this.orderByAscending = !this.orderByAscending;
82-
}else{
83-
this.sortByColumnIndex(columnIndex, iteratee);
84-
85-
this.sortByColumnLastIndex = columnIndex;
86-
this.orderByAscending = true;
87-
}
88-
89-
return this.orderByAscending ? -1 : 1;
90-
};
91-
92-
TableDataStorageService.prototype.sortByColumnIndex = function(index, iteratee){
93-
94-
var sortFunction;
95-
if (typeof iteratee === 'function') {
96-
sortFunction = function(rowData) {
97-
return iteratee(rowData.data[index].value, rowData, index);
98-
};
99-
} else {
100-
sortFunction = function (rowData) {
101-
return rowData.data[index].value;
102-
};
103-
}
104-
105-
var res = _.sortBy(this.storage, sortFunction);
106-
107-
this.storage = res;
108-
};
109-
11069
TableDataStorageService.prototype.isAnyRowSelected = function(){
11170
return _.some(this.storage, function(rowData){
11271
return rowData.optionList.selected === true && rowData.optionList.deleted === false;
@@ -172,8 +131,8 @@
172131
};
173132

174133
return {
175-
getInstance: function(isVirtualRepeatEnabled){
176-
return new TableDataStorageService(isVirtualRepeatEnabled);
134+
getInstance: function(){
135+
return new TableDataStorageService();
177136
}
178137
};
179138
}

0 commit comments

Comments
 (0)