Unified
Split
Showing
with
46 additions
and 2 deletions.
- +15 −1 misc/tutorial/102_sorting.ngdoc
- +1 −1 src/js/core/factories/Grid.js
- +30 −0 test/unit/core/factories/Grid.spec.js
| @@ -22,6 +22,9 @@ The sort is automatically recalculated when you edit a field (the edit feature c | ||
| you change the data "behind the scenes" and want the sort to be recalculated, you can notify the grid that your | ||
| data has changed by calling `gridApi.core.notifyDataChange( grid, uiGridUtils.dataChange.EDIT )` | ||
|
|
||
| If you set a default sort, you can prevent the user from removing that sort by setting `suppressRemoveSort: true` | ||
| for that column. This will let the user change the direction of the sort, but take away the option to remove the sort. | ||
|
|
||
| <example module="app"> | ||
| <file name="app.js"> | ||
| var app = angular.module('app', ['ngAnimate', 'ngTouch', 'ui.grid']); | ||
| @@ -67,7 +70,7 @@ data has changed by calling `gridApi.core.notifyDataChange( grid, uiGridUtils.da | ||
| direction: uiGridConstants.ASC, | ||
| priority: 0, | ||
| }, | ||
|
|
||
| suppressRemoveSort: true, | ||
| sortingAlgorithm: function(a, b) { | ||
| var nulls = $scope.grid2Api.core.sortHandleNulls(a, b); | ||
| if( nulls !== null ) { | ||
| @@ -235,6 +238,17 @@ data has changed by calling `gridApi.core.notifyDataChange( grid, uiGridUtils.da | ||
| gridTestUtils.expectCellValueMatch( 'grid2', 0, 0, 'Yvonne Parsons' ); | ||
| gridTestUtils.expectCellValueMatch( 'grid2', 1, 0, 'Velma Fry' ); | ||
| }); | ||
|
|
||
| it('sort on second column can\'t be removed when cycle through header clicks', function () { | ||
| gridTestUtils.clickHeaderCell( 'grid2', 0 ); | ||
| gridTestUtils.expectCellValueMatch( 'grid2', 0, 0, 'Cora Chase' ); | ||
|
|
||
| gridTestUtils.clickHeaderCell( 'grid2', 1 ); | ||
| gridTestUtils.expectCellValueMatch( 'grid2', 0, 0, 'Herring Pierce' ); | ||
|
|
||
| gridTestUtils.clickHeaderCell( 'grid2', 1 ); | ||
| gridTestUtils.expectCellValueMatch( 'grid2', 0, 0, 'Cora Chase' ); | ||
| }); | ||
| }); | ||
|
|
||
| </file> | ||
| @@ -1496,7 +1496,7 @@ angular.module('ui.grid') | ||
| var self = this; | ||
|
|
||
| self.columns.forEach(function (col) { | ||
| if (col !== excludeCol) { | ||
| if (col !== excludeCol && !col.colDef.suppressRemoveSort) { | ||
| col.sort = {}; | ||
| } | ||
| }); | ||
| @@ -686,6 +686,36 @@ describe('Grid factory', function () { | ||
|
|
||
| expect( column.sort.direction ).toEqual(uiGridConstants.ASC); | ||
| }); | ||
|
|
||
| it( 'if another column has a sort, that sort should be removed', function() { | ||
| var priorColumn = new GridColumn({ name: 'b', sort: { direction: uiGridConstants.ASC } }, 0, grid); | ||
| grid.columns.push( priorColumn ); | ||
|
|
||
| grid.sortColumn( column, false ); | ||
|
|
||
| expect( column.sort.direction ).toEqual(uiGridConstants.ASC); | ||
| expect( priorColumn.sort ).toEqual({}); | ||
| }); | ||
|
|
||
| it( 'if another column has a sort, and add is set to true, then that sort should not be removed', function() { | ||
| var priorColumn = new GridColumn({ name: 'b', sort: { direction: uiGridConstants.ASC } }, 0, grid); | ||
| grid.columns.push( priorColumn ); | ||
|
|
||
| grid.sortColumn( column, true ); | ||
|
|
||
| expect( column.sort.direction ).toEqual(uiGridConstants.ASC); | ||
| expect( priorColumn.sort ).toEqual({ direction: uiGridConstants.ASC }); | ||
| }); | ||
|
|
||
| it( 'if another column has a sort, and add is set to false, but that other column has suppressRemoveSort, then it shouldn\'t be removed', function() { | ||
| var priorColumn = new GridColumn({ name: 'b', sort: { direction: uiGridConstants.ASC }, suppressRemoveSort: true }, 0, grid); | ||
| grid.columns.push( priorColumn ); | ||
|
|
||
| grid.sortColumn( column, false ); | ||
|
|
||
| expect( column.sort.direction ).toEqual(uiGridConstants.ASC); | ||
| expect( priorColumn.sort ).toEqual({ direction: uiGridConstants.ASC }); | ||
| }); | ||
| }); | ||
|
|
||
|
|
||