Skip to content
11 changes: 9 additions & 2 deletions playwright/tests/cell.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,16 @@ test.describe('Cell mouse selection', () => {
await expect(highlighted).toHaveCount(4 + 2);

const table = await page.locator('.datatable').first().boundingBox();
const [, y] = await center(3, 2);
await page.mouse.move(table.x + table.width + 40, y, { steps: 5 });
const viewport = page.viewportSize();
const outside = [
[table.x + table.width + 20, table.y + 10],
[table.x + 10, table.y + table.height + 20],
[table.x - 20, table.y + 10],
[table.x + 10, table.y - 20]
].find(([x, y]) => x > 0 && y > 0 && x < viewport.width && y < viewport.height);
await page.mouse.move(...outside, { steps: 5 });
await page.mouse.up();
await page.waitForTimeout(200);
const selected = await highlighted.count();

await page.mouse.move(...await center(5, 4), { steps: 5 });
Expand Down
32 changes: 32 additions & 0 deletions playwright/tests/inline_filters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,35 @@ test('a filter with no match stays empty when a later column is filtered', async
await page.evaluate(() => window.filterTable.columnmanager.applyFilter({ 1: 'zzz', 2: 'Paris' }));
await expect(rows).toHaveCount(0);
});

test('= matches the whole text of a cell', async ({ page }) => {
await page.evaluate(() => {
window.accountTable = new window.DataTable('#datatable2', {
columns: ['Account'],
data: [['Sales'], ['VAT on Sales'], ['Purchase']],
inlineFilters: true
});
});
const rows = page.locator('#datatable2 .dt-scrollable .dt-row');
await expect(rows).toHaveCount(3);
await page.evaluate(() => window.accountTable.columnmanager.applyFilter({ 1: 'sales' }));
await expect(rows).toHaveCount(2);
await page.evaluate(() => window.accountTable.columnmanager.applyFilter({ 1: '=sales' }));
await expect(rows).toHaveCount(1);
await expect(rows.first()).toContainText('Sales');
await expect(rows.first()).not.toContainText('VAT');
});

test('= also matches the raw value of a formatted cell', async ({ page }) => {
await page.evaluate(() => {
window.itemTable = new window.DataTable('#datatable2', {
columns: [{ name: 'Item', format: value => `${value}: Smartphone` }],
data: [['SKU004'], ['SKU0045']],
inlineFilters: true
});
});
const rows = page.locator('#datatable2 .dt-scrollable .dt-row');
await page.evaluate(() => window.itemTable.columnmanager.applyFilter({ 1: '=sku004' }));
await expect(rows).toHaveCount(1);
await expect(rows.first()).toContainText('SKU004: Smartphone');
});
19 changes: 19 additions & 0 deletions playwright/tests/new_instance.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,23 @@ test.describe('DataTable init', () => {
await expect(page.locator('#datatable2 .datatable')).toContainText('Faris');
expect(errors).toEqual([]);
});

test('a new instance in the same wrapper does not break window resize', async ({ page }) => {
const errors = [];
page.on('pageerror', error => errors.push(error.message));

await page.evaluate(() => {
const options = { columns: ['Name'], data: [['Faris']] };
// eslint-disable-next-line no-new
new window.DataTable('#datatable2', options);
// eslint-disable-next-line no-new
new window.DataTable('#datatable2', options);
});
await page.setViewportSize({ width: 800, height: 600 });
await page.waitForTimeout(400);
await page.setViewportSize({ width: 1000, height: 660 });
await page.waitForTimeout(400);

expect(errors).toEqual([]);
});
});
3 changes: 2 additions & 1 deletion src/cellmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,8 @@ export default class CellManager {
isBodyCell && (row && row.meta.isTreeNodeClose) ? 'dt-cell--tree-close' : '',
isSticky ? 'dt-cell--sticky' : '',
isSticky && !isBodyCell ? 'dt-cell--sticky-top' : '',
isLastStickyColumn ? 'dt-cell--sticky-last' : ''
isLastStickyColumn ? 'dt-cell--sticky-last' : '',
column.id === '_checkbox' ? 'dt-cell--checkbox' : ''
].join(' ');

return `
Expand Down
13 changes: 13 additions & 0 deletions src/filterRows.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ function getFilterMethod(rows, allData, filter) {
.map(cell => cell.rowIndex);
},

equalsText(keyword, cells) {
return cells
.filter(cell => {
const content = cell.content == null ? '' : String(cell.content);
return content.trim().toLowerCase() === keyword || stringCompareValue(cell).trim() === keyword;
})
.map(cell => cell.rowIndex);
},

equals(keyword, cells) {
return cells
.filter(cell => {
Expand Down Expand Up @@ -226,6 +235,10 @@ function guessFilter(keyword = '') {
text: Number(keyword.slice(1).trim())
};
}
return {
type: 'equalsText',
text: compareString.trim().toLowerCase()
};
}

if (keyword.startsWith('!=')) {
Expand Down
11 changes: 6 additions & 5 deletions src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
position: relative;
z-index: 2;

.dt-row:last-child .dt-cell {
.dt-row:last-child {
box-shadow: 0 1px 0 var(--dt-border-color);
}
}
Expand Down Expand Up @@ -117,14 +117,15 @@
overflow: hidden;
}

&__content:has(> input[type="checkbox"]) {
&--checkbox &__content {
display: flex;
align-items: center;
justify-content: center;
}

&__content > input[type="checkbox"] {
margin: 0;
&--checkbox &__content > input[type="checkbox"] {
flex-shrink: 0;
margin: 0 !important;
}

&__edit {
Expand Down Expand Up @@ -180,7 +181,7 @@
font-weight: 500;
}

&--header &__content:has(> input[type="checkbox"]) {
&--header&--checkbox &__content {
padding-right: var(--dt-spacer-2);
}

Expand Down
46 changes: 40 additions & 6 deletions src/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export default class Style {
}

onWindowResize() {
if (!this.styleEl.isConnected) {
$.off(window, 'resize', this.onWindowResize);
return;
}
this.distributeRemainingWidth();
this.refreshColumnWidth();
this.setBodyStyle();
Expand Down Expand Up @@ -265,21 +269,51 @@ export default class Style {
}

// The unique values of a column, as the HTML and the text that the cells show
getColumnValues(colIndex) {
getColumnValues(colIndex, limit = 100) {
const rows = this.bodyRenderer.visibleRows || [];
const values = new Map();
const cellsByContent = new Map();

rows.forEach(row => {
const cell = row[colIndex];
if (!cell) return;

const html = this.getCellContentHTML(cell);
if (html && !values.has(html)) {
values.set(html, { html, text: this.getTextFromHTML(html) });
const content = cell.content == null ? '' : String(cell.content);
if (!cellsByContent.has(content)) {
cellsByContent.set(content, cell);
}
});

return Array.from(values.values());
const htmls = new Set();
cellsByContent.forEach(cell => {
const html = this.getCellContentHTML(cell);
if (html) htmls.add(html);
});

const shownLength = html => (/[<&]/.test(html) ? html.replace(/<[^>]*>/g, '') : html).length;
let candidates = Array.from(htmls);
if (candidates.length > limit) {
const countByLength = [];
candidates.forEach(html => {
const length = shownLength(html);
countByLength[length] = (countByLength[length] || 0) + 1;
});

let minLength = 0;
let count = 0;
for (let length = countByLength.length - 1; length >= 0; length--) {
count += countByLength[length] || 0;
if (count >= limit) {
minLength = length;
break;
}
}

const longer = candidates.filter(html => shownLength(html) > minLength);
const atMinLength = candidates.filter(html => shownLength(html) === minLength);
candidates = longer.concat(atMinLength).slice(0, limit);
}

return candidates.map(html => ({ html, text: this.getTextFromHTML(html) }));
}

// The cell content with the column formatter applied
Expand Down
Loading