Add exact match filtering to console table (#7448)

* Add exact filtering

* Bug fix

* Extract filter as a function

* change code position
This commit is contained in:
Qi Shu 2019-04-17 23:05:44 -07:00 committed by Clint Wylie
parent 9732e04c60
commit c8bffd9351
3 changed files with 32 additions and 6 deletions

View File

@ -20,7 +20,7 @@ import * as React from 'react';
import { Filter, ReactTableDefaults } from 'react-table'; import { Filter, ReactTableDefaults } from 'react-table';
import { Loader } from '../components/loader'; import { Loader } from '../components/loader';
import { countBy, makeTextFilter } from '../utils'; import { countBy, customTableFilter, makeTextFilter } from '../utils';
import { ReactTableCustomPagination } from './react-table-custom-pagination'; import { ReactTableCustomPagination } from './react-table-custom-pagination';
@ -38,8 +38,7 @@ class NoData extends React.Component {
Object.assign(ReactTableDefaults, { Object.assign(ReactTableDefaults, {
defaultFilterMethod: (filter: Filter, row: any, column: any) => { defaultFilterMethod: (filter: Filter, row: any, column: any) => {
const id = filter.pivotId || filter.id; return customTableFilter(filter, row);
return row[id] !== undefined ? String(row[id]).includes(filter.value) : true;
}, },
LoadingComponent: Loader, LoadingComponent: Loader,
loadingText: '', loadingText: '',

View File

@ -23,6 +23,7 @@ import * as React from 'react';
import { Filter, FilterRender } from 'react-table'; import { Filter, FilterRender } from 'react-table';
export function addFilter(filters: Filter[], id: string, value: string): Filter[] { export function addFilter(filters: Filter[], id: string, value: string): Filter[] {
value = `"${value}"`;
const currentFilter = filters.find(f => f.id === id); const currentFilter = filters.find(f => f.id === id);
if (currentFilter) { if (currentFilter) {
filters = filters.filter(f => f.id !== id); filters = filters.filter(f => f.id !== id);
@ -65,6 +66,32 @@ export function makeBooleanFilter(): FilterRender {
}; };
} }
export function customTableFilter(filter: Filter, row?: any): boolean | string {
const id = filter.pivotId || filter.id;
const clientSideFilter: boolean = row !== undefined;
if (clientSideFilter && row[id] === undefined) {
return true;
}
const targetValue = (clientSideFilter && String(row[id].toLowerCase())) || JSON.stringify(filter.id).toLowerCase();
let filterValue = filter.value.toLowerCase();
if (filterValue.startsWith(`"`) && filterValue.endsWith(`"`)) {
const exactString = filterValue.slice(1, -1);
if (clientSideFilter) {
return String(targetValue) === exactString;
} else {
return `${targetValue} = '${exactString}'`;
}
}
if (filterValue.startsWith(`"`)) {
filterValue = filterValue.substring(1);
}
if (clientSideFilter) {
return targetValue.includes(filterValue);
} else {
return `${targetValue} LIKE '%${filterValue}%'`;
}
}
// ---------------------------- // ----------------------------
export function countBy<T>(array: T[], fn: (x: T, index: number) => string = String): Record<string, number> { export function countBy<T>(array: T[], fn: (x: T, index: number) => string = String): Record<string, number> {

View File

@ -29,7 +29,7 @@ import { TableColumnSelection } from '../components/table-column-selection';
import { ViewControlBar } from '../components/view-control-bar'; import { ViewControlBar } from '../components/view-control-bar';
import { AppToaster } from '../singletons/toaster'; import { AppToaster } from '../singletons/toaster';
import { import {
addFilter, addFilter, customTableFilter,
formatBytes, formatBytes,
formatNumber, LocalStorageKeys, formatNumber, LocalStorageKeys,
makeBooleanFilter, makeBooleanFilter,
@ -123,7 +123,7 @@ export class SegmentsView extends React.Component<SegmentsViewProps, SegmentsVie
if (f.value === 'all') return null; if (f.value === 'all') return null;
return `${JSON.stringify(f.id)} = ${f.value === 'true' ? 1 : 0}`; return `${JSON.stringify(f.id)} = ${f.value === 'true' ? 1 : 0}`;
} else { } else {
return `${JSON.stringify(f.id)} LIKE '%${f.value}%'`; return customTableFilter(f);
} }
}).filter(Boolean); }).filter(Boolean);
@ -176,7 +176,7 @@ export class SegmentsView extends React.Component<SegmentsViewProps, SegmentsVie
accessor: 'datasource', accessor: 'datasource',
Cell: row => { Cell: row => {
const value = row.value; const value = row.value;
return <a onClick={() => { this.setState({ segmentFilter: addFilter(segmentFilter, 'datasource', value) }); }}>{value}</a>; return <a onClick={() => { this.setState({ segmentFilter: addFilter(segmentFilter, 'datasource', value)}); }}>{value}</a>;
}, },
show: tableColumnSelectionHandler.showColumn('Datasource') show: tableColumnSelectionHandler.showColumn('Datasource')
}, },