Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion js/dataTables.select.js
Original file line number Diff line number Diff line change
Expand Up @@ -1900,6 +1900,7 @@ DataTable.render.select = function (valueProp, nameProp) {
var selected = dtRow._select_selected;
var ariaLabel = meta.settings.oLanguage.select.aria.rowCheckbox;
var selectable = meta.settings._select.selectable;
var closestElement = meta.settings._select.items === 'cell' ? 'td' : 'tr';

if (type === 'display') {
// Check if the row is selectable before showing the checkbox
Expand Down Expand Up @@ -1927,7 +1928,7 @@ DataTable.render.select = function (valueProp, nameProp) {
// And make sure this checkbox matches it's row as it is possible
// to check out of sync if this was clicked on to deselect a range
// but remains selected itself
this.checked = $(this).closest('tr').hasClass('selected');
this.checked = $(this).closest(closestElement).hasClass('selected');
})[0];
}
else if (type === 'type') {
Expand Down
117 changes: 117 additions & 0 deletions test/options/select-checkbox-items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
describe('Select - type - select-checkbox items is row', function() {
let table;

dt.libs({
js: ['jquery', 'datatables', 'select'],
css: ['datatables', 'select']
});

describe('Functional tests', function() {
dt.html('basic');

it('No info', function() {
var cols = dt.getTestColumns();
cols[0].data = null;
cols[0].defaultContent = '';
cols[0].render = DataTable.render.select();

table = $('#example').DataTable({
select: {
items: 'row',
style: 'multi',
selector: 'td:first-child',
},
columns: cols,
order: [1, 'desc'],
columnDefs: [
{
orderable: true,
className: 'select-checkbox',
targets: -1,
orderDataType: 'select-checkbox'
}
]
});
});

it('Checkbox is unchecked before selection', function() {
expect($('tbody tr:eq(7) td:eq(0) input').prop('checked')).toBe(false);
});

it('Checkbox is checked when row is selected', function() {
$('tbody tr:eq(7) td:eq(0)').click();
expect($('tbody tr:eq(7)').hasClass('selected')).toBe(true);
expect($('tbody tr:eq(7) td:eq(0) input').prop('checked')).toBe(true);
});

it('Other rows remain unchecked', function() {
expect($('tbody tr:eq(0) td:eq(0) input').prop('checked')).toBe(false);
expect($('tbody tr:eq(1) td:eq(0) input').prop('checked')).toBe(false);
});

it('Checkbox is unchecked when row is deselected', function() {
$('tbody tr:eq(7) td:eq(0)').click();
expect($('tbody tr:eq(7)').hasClass('selected')).toBe(false);
expect($('tbody tr:eq(7) td:eq(0) input').prop('checked')).toBe(false);
});
});
});

describe('Select - type - select-checkbox items is cell ', function() {
let table;

dt.libs({
js: ['jquery', 'datatables', 'select'],
css: ['datatables', 'select']
});

describe('Functional tests', function() {
dt.html('basic');

it('No info', function() {
var cols = dt.getTestColumns();
cols[0].data = null;
cols[0].defaultContent = '';
cols[0].render = DataTable.render.select();

table = $('#example').DataTable({
select: {
items: 'cell',
style: 'multi',
selector: 'td:first-child',
},
columns: cols,
order: [1, 'desc'],
columnDefs: [
{
orderable: true,
className: 'select-checkbox',
targets: -1,
orderDataType: 'select-checkbox'
}
]
});
});

it('Checkbox is unchecked before selection', function() {
expect($('tbody tr:eq(7) td:eq(0) input').prop('checked')).toBe(false);
});

it('Checkbox is checked when cell is selected', function() {
$('tbody tr:eq(1) td:eq(0)').click();
expect($('tbody tr:eq(1) td:eq(0)').hasClass('selected')).toBe(true);
expect($('tbody tr:eq(1) td:eq(0) input').prop('checked')).toBe(true);
});

it('Other rows remain unchecked', function() {
expect($('tbody tr:eq(0) td:eq(0) input').prop('checked')).toBe(false);
expect($('tbody tr:eq(1) td:eq(0) input').prop('checked')).toBe(false);
});

it('Checkbox is unchecked when cell is deselected', function() {
$('tbody tr:eq(7) td:eq(0)').click();
expect($('tbody tr:eq(7) td:eq(0)').hasClass('selected')).toBe(false);
expect($('tbody tr:eq(7) td:eq(0) input').prop('checked')).toBe(false);
});
});
});