Skip to content

Commit

Permalink
fix #165
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzofox3 committed Sep 11, 2014
1 parent 984e657 commit 03185a6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 28 deletions.
4 changes: 2 additions & 2 deletions dist/smart-table.debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@
var index = rows.indexOf(row);
if (index !== -1) {
if (mode === 'single') {
row.isSelected = true;
row.isSelected = row.isSelected !== true;
if (lastSelected) {
lastSelected.isSelected = false;
}
lastSelected = row;
lastSelected = row.isSelected === true ? row : undefined;
} else {
rows[index].isSelected = !rows[index].isSelected;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/smart-table.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/stTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@
var index = rows.indexOf(row);
if (index !== -1) {
if (mode === 'single') {
row.isSelected = true;
row.isSelected = row.isSelected !== true;
if (lastSelected) {
lastSelected.isSelected = false;
}
lastSelected = row;
lastSelected = row.isSelected === true ? row : undefined;
} else {
rows[index].isSelected = !rows[index].isSelected;
}
Expand Down
58 changes: 35 additions & 23 deletions test/spec/stTable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('st table Controller', function () {
{name: 'Faivre', firstname: 'Blandine', age: 44}
]);

ctrl.search('','name');
ctrl.search('', 'name');

expect(scope.data).toEqual([
{name: null, firstname: 'Laurent', age: 66},
Expand Down Expand Up @@ -206,64 +206,76 @@ describe('st table Controller', function () {
});

describe('select', function () {
it('should select only a single row at the tiem', function () {
ctrl.select(scope.data[3], 'single');
var selected = scope.data.filter(function (value) {
return value.isSelected === true;

function getSelected(array) {
return array.filter(function (val) {
return val.isSelected === true;
});
}


it('should select only a single row at the time', function () {
ctrl.select(scope.data[3], 'single');
var selected = getSelected(scope.data);
expect(selected.length).toBe(1);
expect(selected[0]).toEqual(scope.data[3]);

ctrl.select(scope.data[2], 'single');

selected = scope.data.filter(function (value) {
return value.isSelected === true;
});
selected = getSelected(scope.data);

expect(selected.length).toBe(1);
expect(selected[0]).toEqual(scope.data[2]);
});

it('should select a row multiple times in single mode (#165)', function () {
ctrl.select(scope.data[3], 'single');
var selected = getSelected(scope.data);
expect(selected.length).toBe(1);
expect(selected[0]).toEqual(scope.data[3]);

ctrl.select(scope.data[3], 'single');
selected = getSelected(scope.data);

expect(selected.length).toBe(0);

ctrl.select(scope.data[3], 'single');
selected = getSelected(scope.data);

expect(selected.length).toBe(1);
expect(selected[0]).toEqual(scope.data[3]);
});

it('should select multiple row', function () {
ctrl.select(scope.data[3]);
ctrl.select(scope.data[4]);
var selected = scope.data.filter(function (val) {
return val.isSelected === true;
});
var selected = getSelected(scope.data);
expect(selected.length).toBe(2);
expect(selected).toEqual([scope.data[3], scope.data[4]]);
});

it('should unselect an item on mode single', function () {
ctrl.select(scope.data[3], 'single');
var selected = scope.data.filter(function (value) {
return value.isSelected === true;
});
var selected = getSelected(scope.data);
expect(selected.length).toBe(1);
expect(selected[0]).toEqual(scope.data[3]);

ctrl.select(scope.data[3], 'single');

selected = scope.data.filter(function (value) {
return value.isSelected === true;
});
selected = getSelected(scope.data);

expect(selected.length).toBe(0);
});

it('should unselect an item on mode multiple', function () {
ctrl.select(scope.data[3]);
ctrl.select(scope.data[4]);
var selected = scope.data.filter(function (val) {
return val.isSelected === true;
});
var selected = getSelected(scope.data);
expect(selected.length).toBe(2);
expect(selected).toEqual([scope.data[3], scope.data[4]]);

ctrl.select(scope.data[3]);
selected = scope.data.filter(function (val) {
return val.isSelected === true;
});
selected = getSelected(scope.data);
expect(selected.length).toBe(1);
expect(selected).toEqual([scope.data[4]]);
});
Expand Down

0 comments on commit 03185a6

Please sign in to comment.