Skip to content

Commit

Permalink
Fix: -init columnDefs weren't being applied if using with _all ta…
Browse files Browse the repository at this point in the history
…rget and an HTML table with no columns predefined (e.g. defining the columns in the column options).

DD-2914
https://datatables.net/forums/discussion/78209
  • Loading branch information
AllanJard committed Feb 19, 2024
1 parent ee57d73 commit 076c4f6
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
10 changes: 8 additions & 2 deletions js/core/core.columns.js
Expand Up @@ -441,12 +441,18 @@ function _fnApplyColumnDefs( oSettings, aoColDefs, aoCols, headerLayout, fn )
else if ( typeof target === 'string' )
{
for ( k=0, kLen=columns.length ; k<kLen ; k++ ) {
if (target.indexOf(':name') !== -1) {
if (target === '_all') {
// Apply to all columns
fn( k, def );
}
else if (target.indexOf(':name') !== -1) {
// Column selector
if (columns[k].sName === target.replace(':name', '')) {
fn( k, def );
}
}
else {
// Cell selector
headerLayout.forEach(function (row) {
var cell = $(row[k].cell);

Expand All @@ -457,7 +463,7 @@ function _fnApplyColumnDefs( oSettings, aoColDefs, aoCols, headerLayout, fn )
target = '.' + target;
}

if (target === '_all' || cell.is( target )) {
if (cell.is( target )) {
fn( k, def );
}
});
Expand Down
45 changes: 45 additions & 0 deletions test/options/columns/columnDefs.js
Expand Up @@ -248,4 +248,49 @@ describe('columnDefs option', function() {
expect(data[0]).toBe('Accountant');
});
});

describe('Empty table', function() {
var counter = 0;

dt.html('empty_no_header');

it('Init the DataTable', function() {
$('#example').DataTable({
columns: [
{
title: 'Test'
}
],
columnDefs: [
{
targets: '_all',
createdCell: function (td, cellData) {
counter++;
$(td).addClass('test-' + cellData);
},
className: 'test-all'
}
],
data: [ [0], [1], [2] ]
});

expect($('tbody tr').length).toBe(3);
});

it('createdCell ran for each cell', function() {
expect(counter).toBe(3);
});

it('And applyed its operation to the cell', function() {
expect($('tbody td').eq(0).hasClass('test-0')).toBe(true);
expect($('tbody td').eq(1).hasClass('test-1')).toBe(true);
expect($('tbody td').eq(2).hasClass('test-2')).toBe(true);
});

it('Applyed static class', function() {
expect($('tbody td').eq(0).hasClass('test-all')).toBe(true);
expect($('tbody td').eq(1).hasClass('test-all')).toBe(true);
expect($('tbody td').eq(2).hasClass('test-all')).toBe(true);
});
});
});
33 changes: 33 additions & 0 deletions test/options/columns/columns.createdCell.js
Expand Up @@ -262,4 +262,37 @@ describe('column.createdCell option', function() {
});
});
});

describe('Empty table', function() {
var counter = 0;

dt.html('empty_no_header');

it('Init the DataTable', function() {
$('#example').DataTable({
columns: [
{
title: 'Test',
createdCell: function (td, cellData) {
counter++;
$(td).addClass('test-' + cellData);
}
}
],
data: [ [0], [1], [2] ]
});

expect($('tbody tr').length).toBe(3);
});

it('Ran for each cell', function() {
expect(counter).toBe(3);
});

it('Applyed to the cell', function() {
expect($('tbody td').eq(0).hasClass('test-0')).toBe(true);
expect($('tbody td').eq(1).hasClass('test-1')).toBe(true);
expect($('tbody td').eq(2).hasClass('test-2')).toBe(true);
});
});
});

0 comments on commit 076c4f6

Please sign in to comment.