Skip to content
Merged
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
23 changes: 23 additions & 0 deletions cypress/integration/column.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,29 @@ describe('Column', function () {
cy.clickDropdownItem(2, 'Reset sorting');
});

it('sorts on the value returned by a column sortValue hook', function () {
// Sort the Name column by surname instead of the cell content.
cy.window().then(win => {
win.datatable.getColumn(2).sortValue = cell => String(cell.content).split(' ').pop();
});

cy.clickDropdown(2);
cy.clickDropdownItem(2, 'Sort Ascending');

cy.window().then(win => {
const datamanager = win.datatable.datamanager;
const surnames = datamanager.rowViewOrder
.map(rowIndex => String(datamanager.getCell(2, rowIndex).content).split(' ').pop());

expect(surnames).to.deep.equal([...surnames].sort());
});

cy.clickDropdownItem(2, 'Reset sorting');
cy.window().then(win => {
delete win.datatable.getColumn(2).sortValue;
});
});

it('removes column using dropdown action', function () {
cy.get('.dt-cell--header').should('have.length', 12);

Expand Down
12 changes: 8 additions & 4 deletions src/datamanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,18 @@ export default class DataManager {
}
}

const sortValue = cell => {
const hook = cell.column && cell.column.sortValue;
const value = hook ? hook(cell) : cell.content;
return value == null ? '' : value;
};

this.rowViewOrder.sort((a, b) => {
const aIndex = a;
const bIndex = b;

let aContent = this.getCell(colIndex, a).content;
let bContent = this.getCell(colIndex, b).content;
aContent = aContent == null ? '' : aContent;
bContent = bContent == null ? '' : bContent;
const aContent = sortValue(this.getCell(colIndex, a));
const bContent = sortValue(this.getCell(colIndex, b));

if (sortOrder === 'none') {
return aIndex - bIndex;
Expand Down
Loading