Skip to content

Commit

Permalink
Fix: Additional columns specified in the init options weren't being d…
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanJard committed Feb 27, 2024
1 parent ff9834d commit bdcbfb3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
31 changes: 20 additions & 11 deletions js/core/core.draw.js
Expand Up @@ -62,7 +62,7 @@ function _fnCreateTr ( oSettings, iRow, nTrIn, anTds )
for ( i=0, iLen=oSettings.aoColumns.length ; i<iLen ; i++ )
{
oCol = oSettings.aoColumns[i];
create = nTrIn ? false : true;
create = nTrIn && anTds[i] ? false : true;

nTd = create ? document.createElement( oCol.sCellType ) : anTds[i];

Expand Down Expand Up @@ -91,11 +91,11 @@ function _fnCreateTr ( oSettings, iRow, nTrIn, anTds )
}

// Visibility - add or remove as required
if ( oCol.bVisible && ! nTrIn )
if ( oCol.bVisible && create )
{
nTr.appendChild( nTd );
}
else if ( ! oCol.bVisible && nTrIn )
else if ( ! oCol.bVisible && ! create )
{
nTd.parentNode.removeChild( nTd );
}
Expand Down Expand Up @@ -179,14 +179,23 @@ function _fnBuildHead( settings, side )
}

// If no cells yet and we have content for them, then create
if ( $('th, td', target).length === 0 && (side === 'header' || _pluck(settings.aoColumns, titleProp).join('')) ) {
row = $('<tr/>')
.appendTo( target );

for ( i=0, ien=columns.length ; i<ien ; i++ ) {
$('<th/>')
.html( columns[i][titleProp] || '' )
.appendTo( row );
if (side === 'header' || _pluck(settings.aoColumns, titleProp).join('')) {
row = $('tr', target);

// Add a row if needed
if (! row.length) {
row = $('<tr/>').appendTo(target)
}

// Add the number of cells needed to make up to the number of columns
if (row.length === 1) {
var cells = $('td, th', row);

for ( i=cells.length, ien=columns.length ; i<ien ; i++ ) {
$('<th/>')
.html( columns[i][titleProp] || '' )
.appendTo( row );
}
}
}

Expand Down
38 changes: 38 additions & 0 deletions test/functional/init/additionalColumns.js
@@ -0,0 +1,38 @@
describe('Empty DataTable', function() {
dt.libs({
js: ['jquery', 'datatables'],
css: ['datatables']
});

dt.html('basic');

it('DataTables can be initialised with an extra column', function() {
new DataTable('#example', {
columns: [
null,
null,
null,
null,
null,
null,
{ title: 'Title', defaultContent: 'Content' }
]
});

// No JS errors occurred
expect(true).toEqual(true);
});

it('Last title contains set title', function() {
expect($('thead tr:first-child th:last-child').text()).toBe('Title');
});

it('Last column contains default content', function() {
expect($('tbody tr:first-child td:last-child').text()).toBe('Content');
});

it('And first column is as expected', function() {
expect($('thead tr:first-child th:first-child').text()).toBe('Name');
expect($('tbody tr:first-child td:first-child').text()).toBe('Airi Satou');
});
});

0 comments on commit bdcbfb3

Please sign in to comment.