Skip to content

Commit

Permalink
refactor(header.html): revert commit 'allow reordering of columns'
Browse files Browse the repository at this point in the history
Reordering of columns was already supported by ngTableDynamic without the need to add a 'position'
field to column objects

BREAKING CHANGE: anyone who relied on a specific 'position' field to order table columns will now
need to use change the order items's in the column array

 Previously:
````
 cols[1].position = 2;
 cols[2].position = 1;
````

 Now:
 ````
var swappedCol = cols[2];
cols[2] = cols[1];
cols[1] = swappedCol;

 ````
  • Loading branch information
ccrowhurstram committed Jul 11, 2015
1 parent 066e76e commit 6bb2aba
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 27 deletions.
22 changes: 9 additions & 13 deletions examples/demo20.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ <h1>Dynamic columns</h1>
<div ng-controller="DemoCtrl">

Columns:
<div ng-repeat="column in cols | orderBy:'position'">
<span class="glyphicon glyphicon-arrow-up" ng-click="move(column,-1)"></span>
<span class="glyphicon glyphicon-arrow-down" ng-click="move(column,1)"></span>
<div ng-repeat="column in cols">
<span class="glyphicon glyphicon-arrow-up" ng-click="move(column,$index,-1)"></span>
<span class="glyphicon glyphicon-arrow-down" ng-click="move(column,$index, 1)"></span>

<label>
<input type="checkbox" ng-model="column.show" /> {{column.title}}
Expand All @@ -34,7 +34,7 @@ <h1>Dynamic columns</h1>

<table ng-table-dynamic="tableParams with cols" show-filter="true" class="table table-bordered table-striped">
<tr ng-repeat="user in $data">
<td ng-repeat="col in $columns | orderBy:'position'">{{user[col.field]}}</td>
<td ng-repeat="col in $columns">{{user[col.field]}}</td>
</tr>
</table>

Expand Down Expand Up @@ -94,20 +94,16 @@ <h1>Dynamic columns</h1>
};
$scope.cols = [usernameCol, ageCol];

$scope.move = function(column, value){
var newPosition = column.position+value;
$scope.move = function(column, currentIdx, value){

var newPosition = currentIdx+value;

if (newPosition >= $scope.cols.length || newPosition < 0) {
return;
}

angular.forEach($scope.cols, function(col){
if (col.position == newPosition) {
col.position = column.position;
}
});

column.position = newPosition;
$scope.cols[currentIdx] = $scope.cols[newPosition];
$scope.cols[newPosition] = column;
};

function names() {
Expand Down
4 changes: 2 additions & 2 deletions src/ng-table/header.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<tr>
<th title="{{$column.headerTitle(this)}}"
ng-repeat="$column in $columns | orderBy:'position'"
ng-repeat="$column in $columns"
ng-class="{
'sortable': $column.sortable(this),
'sort-asc': params.sorting()[$column.sortable(this)]=='asc',
Expand All @@ -17,7 +17,7 @@
</th>
</tr>
<tr ng-show="show_filter" class="ng-table-filters">
<th data-title-text="{{$column.titleAlt(this) || $column.title(this)}}" ng-repeat="$column in $columns | orderBy:'position'" ng-show="$column.show(this)" class="filter">
<th data-title-text="{{$column.titleAlt(this) || $column.title(this)}}" ng-repeat="$column in $columns" ng-show="$column.show(this)" class="filter">
<div ng-repeat="(name, filter) in $column.filter(this)">
<div ng-if="filter.indexOf('/') !== -1" ng-include="filter"></div>
<div ng-if="filter.indexOf('/') === -1" ng-include="'ng-table/filters/' + filter + '.html'"></div>
Expand Down
42 changes: 30 additions & 12 deletions test/tableDynamicSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ describe('ng-table-dynamic', function() {
});
});

describe('sortable columns', function() {
describe('reorder columns', function() {
var elm;
var getTitles = function () {
var thead = elm.find('thead');
Expand All @@ -446,7 +446,7 @@ describe('ng-table-dynamic', function() {
'<div>' +
'<table ng-table-dynamic="tableParams with cols" show-filter="true">' +
'<tr ng-repeat="user in $data">' +
"<td ng-repeat=\"col in $columns | orderBy:'position'\">{{user[col.field]}}</td>" +
"<td ng-repeat=\"col in $columns\">{{user[col.field]}}</td>" +
'</tr>' +
'</table>' +
'</div>');
Expand All @@ -455,33 +455,51 @@ describe('ng-table-dynamic', function() {
scope.cols = [
{
field: 'name',
title: 'Name',
position: 0
title: 'Name'
},
{
field: 'age',
title: 'Age',
position: 1
title: 'Age'
},
{
field: 'money',
title: 'Money',
position: 2
title: 'Money'
}
];

$compile(elm)(scope);
scope.$digest();
}));

it('should sort table header ordered by position', function () {
it('"in place" switch of columns within array should reorder html table columns', function () {
expect(getTitles()).toEqual([ 'Name', 'Age', 'Money' ]);

var colToSwap = scope.cols[2];
scope.cols[2] = scope.cols[1];
scope.cols[1] = colToSwap;
scope.$digest();

expect(getTitles()).toEqual([ 'Name', 'Money', 'Age' ]);
});

it('"in place" reverse of column array should reorder html table columns', function () {
expect(getTitles()).toEqual([ 'Name', 'Age', 'Money' ]);

scope.cols.reverse();
scope.$digest();

expect(getTitles()).toEqual([ 'Money', 'Age', 'Name' ]);
});

it('html table columns should reflect order of columns in replacement array', function () {
expect(getTitles()).toEqual([ 'Name', 'Age', 'Money' ]);

scope.cols[0].position = 1;
scope.cols[1].position = 0;
var newArray = scope.cols.map(angular.identity);
newArray.reverse();
scope.cols = newArray;
scope.$digest();

expect(getTitles()).toEqual([ 'Age', 'Name', 'Money' ]);
expect(getTitles()).toEqual([ 'Money', 'Age', 'Name' ]);
});
});
});

0 comments on commit 6bb2aba

Please sign in to comment.