mirror of
https://github.com/gosticks/react-bootstrap-table2.git
synced 2025-10-16 11:55:39 +00:00
fix #1078
This commit is contained in:
parent
4dc5e6099f
commit
00b1558df0
116
packages/react-bootstrap-table2-example/examples/search/custom-match-function.js
vendored
Normal file
116
packages/react-bootstrap-table2-example/examples/search/custom-match-function.js
vendored
Normal 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>
|
||||
);
|
||||
@ -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())
|
||||
|
||||
@ -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.
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user