react-bootstrap-table2
separate the filter core code base to react-bootstrap-table2-filter
, so there's a little bit different when you use column filter than react-bootstrap-table
. In the following, we are going to show you how to enable the column filter:
$ npm install react-bootstrap-table2-filter --save
You can get all types of filters via import and these filters are a factory function to create a individual filter instance. Currently, we support following filters:
Text Filterrequire('react-bootstrap-table2-filter/dist/react-bootstrap-table2-filter.min.css');
Â
import 'react-bootstrap-table2-filter/dist/react-bootstrap-table2-filter.min.css';
Following is a quick demo for enable the column filter on Product Price column!!
import filterFactory, { textFilter } from 'react-bootstrap-table2-filter';
Â
const columns = [
  ..., {
  dataField: 'price',
  text: 'Product Price',
  filter: textFilter()
}];
Â
<BootstrapTable keyField='id' data={ products } columns={ columns } filter={ filterFactory() } />
In addition, we preserve all of the filter features and functionality in legacy react-bootstrap-table
, but in different way to do it:
Select Filterimport filterFactory, { textFilter, Comparator } from 'react-bootstrap-table2-filter';
Â
const priceFilter = textFilter({
  placeholder: 'My Custom PlaceHolder', Â
  className: 'my-custom-text-filter',Â
  defaultValue: 'test',Â
  comparator: Comparator.EQ,Â
  caseSensitive: true,Â
  style: { ... },Â
  delay: 1000,Â
  id: 'id',Â
});
Â
A quick example:
import filterFactory, { selectFilter } from 'react-bootstrap-table2-filter';
Â
const selectOptions = {
  0: 'good',
  1: 'Bad',
  2: 'unknown'
};
Â
const columns = [
  ..., {
  dataField: 'quality',
  text: 'Product Quailty',
  formatter: cell => selectOptions[cell],
  filter: selectFilter({
    options: selectOptions
  })
}];
Â
<BootstrapTable keyField='id' data={ products } columns={ columns } filter={ filterFactory() } />
Following is an example for custom select filter:
import filterFactory, { selectFilter, Comparator } from 'react-bootstrap-table2-filter';
Â
const qualityFilter = selectFilter({
  options: selectOptions,
  placeholder: 'My Custom PlaceHolder', Â
  className: 'my-custom-text-filter',Â
  defaultValue: '2',Â
  comparator: Comparator.LIKE,Â
  style: { ... },Â
  withoutEmptyOption: true Â
  id: 'id',Â
});
Â
Array as optionsNote, the selectOptions can be an array or a function which return an array:
Function as optionsconst selectOptions = [
  { value: 0, label: 'good' },
  { value: 1, label: 'Bad' },
  { value: 2, label: 'unknown' }
];
const columns = [
  ..., {
  dataField: 'quality',
  text: 'Product Quailty',
  formatter: cell => selectOptions.find(opt => opt.value === cell).label,
  filter: selectFilter({
    options: selectOptions
  })
}];
const selectOptions = [
  { value: 0, label: 'good' },
  { value: 1, label: 'Bad' },
  { value: 2, label: 'unknown' }
];
const columns = [
  ..., {
  dataField: 'quality',
  text: 'Product Quailty',
  formatter: cell => selectOptions.find(opt => opt.value === cell).label,
  filter: selectFilter({
    options: () => selectOptions
  })
}];
The benifit is react-bootstrap-table2
will render the select options by the order of array.
A quick example:
import filterFactory, { multiSelectFilter } from 'react-bootstrap-table2-filter';
Â
const selectOptions = {
  0: 'good',
  1: 'Bad',
  2: 'unknown'
};
Â
const columns = [
  ..., {
  dataField: 'quality',
  text: 'Product Quailty',
  formatter: cell => selectOptions[cell],
  filter: multiSelectFilter({
    options: selectOptions
  })
}];
Â
<BootstrapTable keyField='id' data={ products } columns={ columns } filter={ filterFactory() } />
Following is an example for custom select filter:
Number Filterimport filterFactory, { multiSelectFilter, Comparator } from 'react-bootstrap-table2-filter';
Â
const qualityFilter = multiSelectFilter({
  options: selectOptions,
  placeholder: 'My Custom PlaceHolder', Â
  className: 'my-custom-text-filter',Â
  defaultValue: '2',Â
  comparator: Comparator.LIKE,Â
  style: { ... },Â
  withoutEmptyOption: true Â
  id: 'id',Â
});
Â
import filterFactory, { numberFilter } from 'react-bootstrap-table2-filter';
Â
const columns = [..., {
  dataField: 'price',
  text: 'Product Price',
  filter: numberFilter()
}];
Â
<BootstrapTable keyField='id' data={ products } columns={ columns } filter={ filterFactory() } />
Numner filter is same as other filter, you can custom the number filter via numberFilter
factory function:
Date Filterimport filterFactory, { selectFilter, Comparator, numberFilter } from 'react-bootstrap-table2-filter';
Â
const numberFilter = numberFilter({
  options: [2100, 2103, 2105], Â
  delay: 600, Â
  placeholder: 'custom placeholder', Â
  withoutEmptyComparatorOption: true, Â
  withoutEmptyNumberOption: true, Â
  comparators: [Comparator.EQ, Comparator.GT, Comparator.LT], Â
  style: { display: 'inline-grid' }, Â
  className: 'custom-numberfilter-class', Â
  comparatorStyle: { backgroundColor: 'antiquewhite' },Â
  comparatorClassName: 'custom-comparator-class', Â
  numberStyle: { backgroundColor: 'cadetblue', margin: '0px' }, Â
  numberClassName: 'custom-number-class', Â
  defaultValue: { number: 2103, comparator: Comparator.GT } Â
  id: 'id',Â
})
Â
import filterFactory, { dateFilter } from 'react-bootstrap-table2-filter';
Â
const columns = [..., {
  dataField: 'date',
  text: 'Product date',
  filter: dateFilter()
}];
Â
<BootstrapTable keyField='id' data={ products } columns={ columns } filter={ filterFactory() } />
Notes: date filter accept a Javascript Date object in your raw data and you have to use
column.formatter
to make it as your prefer string result
Date filter is same as other filter, you can custom the date filter via dateFilter
factory function:
Custom Filterimport filterFactory, { selectFilter, Comparator } from 'react-bootstrap-table2-filter';
Â
const dateFilter = dateFilter({
  delay: 600, Â
  placeholder: 'custom placeholder', Â
  withoutEmptyComparatorOption: true, Â
  comparators: [Comparator.EQ, Comparator.GT, Comparator.LT], Â
  style: { display: 'inline-grid' }, Â
  className: 'custom-dateFilter-class', Â
  comparatorStyle: { backgroundColor: 'antiquewhite' },Â
  comparatorClassName: 'custom-comparator-class', Â
  dateStyle: { backgroundColor: 'cadetblue', margin: '0px' }, Â
  dateClassName: 'custom-date-class', Â
  defaultValue: { date: new Date(2018, 0, 1), comparator: Comparator.GT } Â
  id: 'id',Â
})
Â
import filterFactory, { customFilter } from 'react-bootstrap-table2-filter';
Â
const columns = [..., {
  dataField: 'date',
  text: 'Product Name',
  filter: customFilter(),
  filterRenderer: (onFilter, column) => .....
}];
Â
<BootstrapTable keyField='id' data={ products } columns={ columns } filter={ filterFactory() } />
In custom filter case, you are suppose to finish following two steps:
customFilter
and pass to column.filter
column.filterRenderer
as a callback function and return your custom filter element.This function will pass two argument to you:
onFilter
: call it to trigger filter when you need.column
: Just the column object!In the end, please remember to return your custom filter element!
customFiltercustomFilter
function just same as textFilter
, selectFilter
etc, it is for customization reason. However, in the custom filter case, there's only one props is valid: type
import filterFactory, { FILTER_TYPES } from 'react-bootstrap-table2-filter';
Â
const customFilter = customFilter({
  type: FILTER_TYPES.NUMBER, Â
})
type
is a way to ask react-bootstrap-table
to filter you data as number, select, date or normal text.
Following properties is valid in FILTER_TYPES
:
Default filter is rendered inside the table column header, but you can choose to render them as a row by filterPosition
:
Render in the bottom of table body<BootstrapTable
  keyField='id'
  data={ products }
  columns={ columns }
  filter={ filterFactory() }
  filterPosition="top"
/>
Configuration<BootstrapTable
  keyField='id'
  data={ products }
  columns={ columns }
  filter={ filterFactory() }
  filterPosition="bottom"
/>
filterFactory
is a factory function for initializing some internal config. Below is available options for filterFactory
:
This hook function will be called with two arguments(new filter result and filter object) when filtering completed.
function afterFilter(newResult, newFilters) {
  console.log(newResult);
  console.log(newFilters);
}
Â
export default () => (
  <div>
    <BootstrapTable
      keyField="id"
      data={ products }
      columns={ columns }
      filter={ filterFactory({ afterFilter }) }
    />
  </div>
);
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4