This commit is contained in:
AllenFang 2019-09-07 14:44:18 +08:00
parent 4dc5e6099f
commit 00b1558df0
4 changed files with 162 additions and 5 deletions

View File

@ -0,0 +1,116 @@
/* eslint react/prop-types: 0 */
/* eslint no-unused-vars: 0 */
import React from 'react';
import BootstrapTable from 'react-bootstrap-table-next';
import ToolkitProvider, { Search } from 'react-bootstrap-table2-toolkit';
import Code from 'components/common/code-block';
import { productsGenerator } from 'utils/common';
const { SearchBar } = Search;
const products = productsGenerator();
const columns = [{
dataField: 'id',
text: 'Product ID'
}, {
dataField: 'name',
text: 'Product Name'
}, {
dataField: 'price',
text: 'Product Price'
}];
const sourceCode = `\
import BootstrapTable from 'react-bootstrap-table-next';
import ToolkitProvider, { Search } from 'react-bootstrap-table2-toolkit';
const { SearchBar } = Search;
const columns = [{
dataField: 'id',
text: 'Product ID'
}, {
dataField: 'name',
text: 'Product Name'
}, {
dataField: 'price',
text: 'Product Price'
}];
// Implement startWith instead of contain
function customMatchFunc({
searchText,
value,
column,
row
}) {
if (typeof value !== 'undefined') {
return value.startsWith(searchText);
}
return false;
}
export default () => (
<div>
<ToolkitProvider
keyField="id"
data={ products }
columns={ columns }
search={ { customMatchFunc } }
>
{
props => (
<div>
<h3>Input something at below input field:</h3>
<SearchBar { ...props.searchProps } />
<hr />
<BootstrapTable
{ ...props.baseProps }
/>
</div>
)
}
</ToolkitProvider>
<Code>{ sourceCode }</Code>
</div>
);
`;
// Implement startWith instead of contain
function customMatchFunc({
searchText,
value,
column,
row
}) {
if (typeof value !== 'undefined') {
return `${value}`.toLowerCase().startsWith(searchText.toLowerCase());
}
return false;
}
export default () => (
<div>
<h1>Custom a search match function by startWith instead of contain</h1>
<ToolkitProvider
keyField="id"
data={ products }
columns={ columns }
search={ { onColumnMatch: customMatchFunc } }
>
{
props => (
<div>
<h3>Input something at below input field:</h3>
<SearchBar { ...props.searchProps } />
<hr />
<BootstrapTable
{ ...props.baseProps }
/>
</div>
)
}
</ToolkitProvider>
<Code>{ sourceCode }</Code>
</div>
);

View File

@ -195,6 +195,7 @@ import FullyCustomSearch from 'examples/search/fully-custom-search';
import SearchFormattedData from 'examples/search/search-formatted';
import CustomSearchValue from 'examples/search/custom-search-value';
import SearchableColumn from 'examples/search/searchable-column';
import CustomMatchFunction from 'examples/search/custom-match-function';
// CSV
import ExportCSV from 'examples/csv';
@ -451,7 +452,8 @@ storiesOf('Table Search', module)
.add('Searchable Column', () => <SearchableColumn />)
.add('Fully Custom Search', () => <FullyCustomSearch />)
.add('Search Formatted Value', () => <SearchFormattedData />)
.add('Custom Search Value', () => <CustomSearchValue />);
.add('Custom Search Value', () => <CustomSearchValue />)
.add('Custom match function', () => <CustomMatchFunction />);
storiesOf('Column Toggle', module)
.addDecorator(bootstrapStyle())

View File

@ -98,6 +98,33 @@ Accept a string that will be used for default searching when first time table re
</ToolkitProvider>
```
#### onColumnMatch - [function]
Acccpt a function which will be called when table try to match every cells when search happening. This function accept an object like below example:
```js
function onColumnMatch({
searchText,
value,
column,
row
}) {
// implement your custom match logic on every cell value
}
<ToolkitProvider
keyField="id"
data={ products }
columns={ columns }
search={ {
onColumnMatch
} }
>
// ...
</ToolkitProvider>
```
> Notes: You have to return `true` when your match logic is positive and vice versa.
#### searchFormatted - [bool]
If you want to search on the formatted data, you are supposed to enable this props. `react-bootstrap-table2` will check if you define the `column.formatter` when doing search.

View File

@ -8,7 +8,8 @@ import React from 'react';
import PropTypes from 'prop-types';
export default (options = {
searchFormatted: false
searchFormatted: false,
onColumnMatch: null
}) => (
_,
isRemoteSearch,
@ -83,11 +84,22 @@ export default (options = {
} else if (column.filterValue) {
targetValue = column.filterValue(targetValue, row);
}
if (targetValue !== null && typeof targetValue !== 'undefined') {
targetValue = targetValue.toString().toLowerCase();
if (targetValue.indexOf(searchText) > -1) {
if (options.onColumnMatch) {
if (options.onColumnMatch({
searchText,
value: targetValue,
column,
row
})) {
return true;
}
} else {
if (targetValue !== null && typeof targetValue !== 'undefined') {
targetValue = targetValue.toString().toLowerCase();
if (targetValue.indexOf(searchText) > -1) {
return true;
}
}
}
}
return false;