Skip to content

Commit

Permalink
Fix: If a column data type is null when it is sorted, it should be re…
Browse files Browse the repository at this point in the history
…solved. This could happen after data has been invalidated (e.g. after column reordering).

DataTables/ColReorder#90
  • Loading branch information
AllanJard committed Jan 16, 2025
1 parent 11e6790 commit 7bceadf
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 24 deletions.
4 changes: 4 additions & 0 deletions js/core/core.sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,14 @@ function _fnSort ( oSettings, col, dir )
displayMaster = oSettings.aiDisplayMaster,
aSort;

// Make sure the columns all have types defined
_fnColumnTypes(oSettings);

// Allow a specific column to be sorted, which will _not_ alter the display
// master
if (col !== undefined) {
var srcCol = oSettings.aoColumns[col];

aSort = [{
src: col,
col: col,
Expand Down
76 changes: 52 additions & 24 deletions test/functional/features/data-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ describe('Data type detection', function () {
var table;
var data = [];

for ( var i=0 ; i<100 ; i++) {
data.push([
'a', // plain string
i, // number
'<div>' + i + '</div>', // HTML number
'2020-01-01', // Date
'c' + i, // String
'<div>d' + i + '</div>'] // HTML
for (var i = 0; i < 100; i++) {
data.push(
[
'a', // plain string
i, // number
'<div>' + i + '</div>', // HTML number
'2020-01-01', // Date
'c' + i, // String
'<div>d' + i + '</div>'
] // HTML
);
}

Expand All @@ -23,14 +25,14 @@ describe('Data type detection', function () {
it('Types detected as expected', function () {
table = new DataTable('#example', {
data: data
});

expect(table.column(0).type()).toBe('string');
expect(table.column(1).type()).toBe('num');
expect(table.column(2).type()).toBe('html-num');
expect(table.column(3).type()).toBe('date');
expect(table.column(4).type()).toBe('string');
expect(table.column(5).type()).toBe('html');
});

expect(table.column(0).type()).toBe('string');
expect(table.column(1).type()).toBe('num');
expect(table.column(2).type()).toBe('html-num');
expect(table.column(3).type()).toBe('date');
expect(table.column(4).type()).toBe('string');
expect(table.column(5).type()).toBe('html');
});

dt.html('empty');
Expand All @@ -40,13 +42,39 @@ describe('Data type detection', function () {
data: data,
searching: false,
ordering: false
});

expect(table.column(0).type()).toBe('string');
expect(table.column(1).type()).toBe('num');
expect(table.column(2).type()).toBe('html-num');
expect(table.column(3).type()).toBe('date');
expect(table.column(4).type()).toBe('string');
expect(table.column(5).type()).toBe('html');
});

expect(table.column(0).type()).toBe('string');
expect(table.column(1).type()).toBe('num');
expect(table.column(2).type()).toBe('html-num');
expect(table.column(3).type()).toBe('date');
expect(table.column(4).type()).toBe('string');
expect(table.column(5).type()).toBe('html');
});

// Data types after an invalidation
// https://github.com/DataTables/ColReorder/issues/90
dt.html('empty');

it('Type detected as expected on start up', function () {
table = new DataTable('#example', {
data: data
});

expect(table.column(3).type()).toBe('date');
});

it('Invalidate data causes type to be null', function () {
table.cell(0, 3).invalidate('data');

// Note need, to check the internal value, as `column().type()` will resolve the
// type
expect(table.settings()[0].aoColumns[3].sType).toBe(null);
});

it('Ordering by the column resolves type', async function () {
await dt.clickHeader(3);

expect(table.settings()[0].aoColumns[3].sType).toBe('date');
});
});

0 comments on commit 7bceadf

Please sign in to comment.