diff --git a/playwright/tests/cell.spec.js b/playwright/tests/cell.spec.js index 3dd357a..bdeb994 100644 --- a/playwright/tests/cell.spec.js +++ b/playwright/tests/cell.spec.js @@ -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 }); diff --git a/playwright/tests/inline_filters.spec.js b/playwright/tests/inline_filters.spec.js index e5ef338..58d8d23 100644 --- a/playwright/tests/inline_filters.spec.js +++ b/playwright/tests/inline_filters.spec.js @@ -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'); +}); diff --git a/playwright/tests/new_instance.spec.js b/playwright/tests/new_instance.spec.js index d43045e..4a60980 100644 --- a/playwright/tests/new_instance.spec.js +++ b/playwright/tests/new_instance.spec.js @@ -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([]); + }); }); diff --git a/src/cellmanager.js b/src/cellmanager.js index 9d4f613..58fbfbc 100644 --- a/src/cellmanager.js +++ b/src/cellmanager.js @@ -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 ` diff --git a/src/filterRows.js b/src/filterRows.js index 078a893..1fb581e 100644 --- a/src/filterRows.js +++ b/src/filterRows.js @@ -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 => { @@ -226,6 +235,10 @@ function guessFilter(keyword = '') { text: Number(keyword.slice(1).trim()) }; } + return { + type: 'equalsText', + text: compareString.trim().toLowerCase() + }; } if (keyword.startsWith('!=')) { diff --git a/src/style.css b/src/style.css index 5106298..431eda5 100644 --- a/src/style.css +++ b/src/style.css @@ -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); } } @@ -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 { @@ -180,7 +181,7 @@ font-weight: 500; } - &--header &__content:has(> input[type="checkbox"]) { + &--header&--checkbox &__content { padding-right: var(--dt-spacer-2); } diff --git a/src/style.js b/src/style.js index 95a307e..820e2ec 100644 --- a/src/style.js +++ b/src/style.js @@ -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(); @@ -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