mirror of
https://github.com/gosticks/react-bootstrap-table2.git
synced 2026-05-29 14:34:26 +00:00
add stories for custom filter
This commit is contained in:
184
packages/react-bootstrap-table2-example/examples/column-filter/advance-custom-filter.js
vendored
Normal file
184
packages/react-bootstrap-table2-example/examples/column-filter/advance-custom-filter.js
vendored
Normal file
@@ -0,0 +1,184 @@
|
||||
/* eslint no-return-assign: 0 */
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import BootstrapTable from 'react-bootstrap-table-next';
|
||||
import filterFactory, { textFilter, customFilter, Comparator, FILTER_TYPES } from 'react-bootstrap-table2-filter';
|
||||
import Code from 'components/common/code-block';
|
||||
import { productsGenerator } from 'utils/common';
|
||||
|
||||
const products = productsGenerator(8);
|
||||
|
||||
class PriceFilter extends React.Component {
|
||||
static propTypes = {
|
||||
column: PropTypes.object.isRequired,
|
||||
onFilter: PropTypes.func.isRequired
|
||||
}
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.filter = this.filter.bind(this);
|
||||
this.getValue = this.getValue.bind(this);
|
||||
this.onChange = this.onChange.bind(this);
|
||||
this.state = { value: 2100 };
|
||||
}
|
||||
onChange(e) {
|
||||
this.setState({ value: e.target.value });
|
||||
}
|
||||
getValue() {
|
||||
return parseInt(this.range.value, 10);
|
||||
}
|
||||
filter() {
|
||||
this.props.onFilter({
|
||||
number: this.getValue(),
|
||||
comparator: this.select.value
|
||||
});
|
||||
}
|
||||
render() {
|
||||
return [
|
||||
<input
|
||||
key="range"
|
||||
ref={ node => this.range = node }
|
||||
type="range"
|
||||
min="2100"
|
||||
max="2110"
|
||||
onChange={ this.onChange }
|
||||
/>,
|
||||
<p
|
||||
key="show"
|
||||
ref={ node => this.showValue = node }
|
||||
style={ { textAlign: 'center' } }
|
||||
>
|
||||
{ this.state.value }
|
||||
</p>,
|
||||
<select
|
||||
key="select"
|
||||
ref={ node => this.select = node }
|
||||
className="form-control"
|
||||
>
|
||||
<option value={ Comparator.GT }>></option>
|
||||
<option value={ Comparator.EQ }>=</option>
|
||||
<option value={ Comparator.LT }><</option>
|
||||
</select>,
|
||||
<button
|
||||
key="submit"
|
||||
className="btn btn-warning"
|
||||
onClick={ this.filter }
|
||||
>
|
||||
{ `Filter ${this.props.column.text}` }
|
||||
</button>
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
const columns = [{
|
||||
dataField: 'id',
|
||||
text: 'Product ID'
|
||||
}, {
|
||||
dataField: 'name',
|
||||
text: 'Product Name',
|
||||
filter: textFilter()
|
||||
}, {
|
||||
dataField: 'price',
|
||||
text: 'Product Price',
|
||||
filter: customFilter({
|
||||
type: FILTER_TYPES.NUMBER // ask react-bootstrap-table to filter data as number
|
||||
}),
|
||||
filterRenderer: (onFilter, column) =>
|
||||
<PriceFilter onFilter={ onFilter } column={ column } />
|
||||
}];
|
||||
|
||||
const sourceCode = `\
|
||||
import BootstrapTable from 'react-bootstrap-table-next';
|
||||
import filterFactory, { textFilter, customFilter, Comparator, FILTER_TYPES } from 'react-bootstrap-table2-filter';
|
||||
|
||||
class PriceFilter extends React.Component {
|
||||
static propTypes = {
|
||||
column: PropTypes.object.isRequired,
|
||||
onFilter: PropTypes.func.isRequired
|
||||
}
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.filter = this.filter.bind(this);
|
||||
this.getValue = this.getValue.bind(this);
|
||||
this.onChange = this.onChange.bind(this);
|
||||
this.state = { value: 2100 };
|
||||
}
|
||||
onChange(e) {
|
||||
this.setState({ value: e.target.value });
|
||||
}
|
||||
getValue() {
|
||||
return parseInt(this.range.value, 10);
|
||||
}
|
||||
filter() {
|
||||
this.props.onFilter({
|
||||
number: this.getValue(),
|
||||
comparator: this.select.value
|
||||
});
|
||||
}
|
||||
render() {
|
||||
return [
|
||||
<input
|
||||
key="range"
|
||||
ref={ node => this.range = node }
|
||||
type="range"
|
||||
min="2100"
|
||||
max="2110"
|
||||
onChange={ this.onChange }
|
||||
/>,
|
||||
<p
|
||||
key="show"
|
||||
ref={ node => this.showValue = node }
|
||||
style={ { textAlign: 'center' } }
|
||||
>
|
||||
{ this.state.value }
|
||||
</p>,
|
||||
<select
|
||||
key="select"
|
||||
ref={ node => this.select = node }
|
||||
className="form-control"
|
||||
>
|
||||
<option value={ Comparator.GT }>></option>
|
||||
<option value={ Comparator.EQ }>=</option>
|
||||
<option value={ Comparator.LT }><</option>
|
||||
</select>,
|
||||
<button
|
||||
key="submit"
|
||||
className="btn btn-warning"
|
||||
onClick={ this.filter }
|
||||
>
|
||||
{ \`Filter $\{this.props.column.text}\` }
|
||||
</button>
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
const columns = [{
|
||||
dataField: 'id',
|
||||
text: 'Product ID'
|
||||
}, {
|
||||
dataField: 'name',
|
||||
text: 'Product Name',
|
||||
filter: textFilter()
|
||||
}, {
|
||||
dataField: 'price',
|
||||
text: 'Product Price',
|
||||
filter: customFilter({
|
||||
type: FILTER_TYPES.NUMBER // ask react-bootstrap-table to filter data as number
|
||||
}),
|
||||
filterRenderer: (onFilter, column) =>
|
||||
<PriceFilter onFilter={ onFilter } column={ column } />
|
||||
}];
|
||||
|
||||
<BootstrapTable keyField='id' data={ products } columns={ columns } filter={ filterFactory() } />
|
||||
`;
|
||||
|
||||
export default () => (
|
||||
<div>
|
||||
<BootstrapTable
|
||||
keyField="id"
|
||||
data={ products }
|
||||
columns={ columns }
|
||||
filter={ filterFactory() }
|
||||
/>
|
||||
<Code>{ sourceCode }</Code>
|
||||
</div>
|
||||
);
|
||||
128
packages/react-bootstrap-table2-example/examples/column-filter/custom-filter.js
vendored
Normal file
128
packages/react-bootstrap-table2-example/examples/column-filter/custom-filter.js
vendored
Normal file
@@ -0,0 +1,128 @@
|
||||
/* eslint no-return-assign: 0 */
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import BootstrapTable from 'react-bootstrap-table-next';
|
||||
import filterFactory, { textFilter, customFilter } from 'react-bootstrap-table2-filter';
|
||||
import Code from 'components/common/code-block';
|
||||
import { productsGenerator } from 'utils/common';
|
||||
|
||||
const products = productsGenerator(8);
|
||||
|
||||
class PriceFilter extends React.Component {
|
||||
static propTypes = {
|
||||
column: PropTypes.object.isRequired,
|
||||
onFilter: PropTypes.func.isRequired
|
||||
}
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.filter = this.filter.bind(this);
|
||||
this.getValue = this.getValue.bind(this);
|
||||
}
|
||||
getValue() {
|
||||
return this.input.value;
|
||||
}
|
||||
filter() {
|
||||
this.props.onFilter(this.getValue());
|
||||
}
|
||||
render() {
|
||||
return [
|
||||
<input
|
||||
key="input"
|
||||
ref={ node => this.input = node }
|
||||
type="text"
|
||||
placeholder="Input price"
|
||||
/>,
|
||||
<button
|
||||
key="submit"
|
||||
className="btn btn-warning"
|
||||
onClick={ this.filter }
|
||||
>
|
||||
{ `Find ${this.props.column.text}` }
|
||||
</button>
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
const columns = [{
|
||||
dataField: 'id',
|
||||
text: 'Product ID'
|
||||
}, {
|
||||
dataField: 'name',
|
||||
text: 'Product Name',
|
||||
filter: textFilter()
|
||||
}, {
|
||||
dataField: 'price',
|
||||
text: 'Product Price',
|
||||
filter: customFilter(),
|
||||
filterRenderer: (onFilter, column) =>
|
||||
<PriceFilter onFilter={ onFilter } column={ column } />
|
||||
}];
|
||||
|
||||
const sourceCode = `\
|
||||
import BootstrapTable from 'react-bootstrap-table-next';
|
||||
import filterFactory, { textFilter, customFilter } from 'react-bootstrap-table2-filter';
|
||||
|
||||
class PriceFilter extends React.Component {
|
||||
static propTypes = {
|
||||
column: PropTypes.object.isRequired,
|
||||
onFilter: PropTypes.func.isRequired
|
||||
}
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.filter = this.filter.bind(this);
|
||||
this.getValue = this.getValue.bind(this);
|
||||
}
|
||||
getValue() {
|
||||
return this.input.value;
|
||||
}
|
||||
filter() {
|
||||
this.props.onFilter(this.getValue());
|
||||
}
|
||||
render() {
|
||||
return [
|
||||
<input
|
||||
key="input"
|
||||
ref={ node => this.input = node }
|
||||
type="text"
|
||||
placeholder="Input price"
|
||||
/>,
|
||||
<button
|
||||
key="submit"
|
||||
className="btn btn-warning"
|
||||
onClick={ this.filter }
|
||||
>
|
||||
{ \`Filter $\{this.props.column.text}\` }
|
||||
</button>
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
const columns = [{
|
||||
dataField: 'id',
|
||||
text: 'Product ID'
|
||||
}, {
|
||||
dataField: 'name',
|
||||
text: 'Product Name',
|
||||
filter: textFilter()
|
||||
}, {
|
||||
dataField: 'price',
|
||||
text: 'Product Price',
|
||||
filter: customFilter(),
|
||||
filterRenderer: (onFilter, column) =>
|
||||
<PriceFilter onFilter={ onFilter } column={ column } />
|
||||
}];
|
||||
|
||||
<BootstrapTable keyField='id' data={ products } columns={ columns } filter={ filterFactory() } />
|
||||
`;
|
||||
|
||||
export default () => (
|
||||
<div>
|
||||
<BootstrapTable
|
||||
keyField="id"
|
||||
data={ products }
|
||||
columns={ columns }
|
||||
filter={ filterFactory() }
|
||||
/>
|
||||
<Code>{ sourceCode }</Code>
|
||||
</div>
|
||||
);
|
||||
@@ -55,6 +55,8 @@ import ProgrammaticallyTextFilter from 'examples/column-filter/programmatically-
|
||||
import ProgrammaticallySelectFilter from 'examples/column-filter/programmatically-select-filter';
|
||||
import ProgrammaticallyNumberFilter from 'examples/column-filter/programmatically-number-filter';
|
||||
import ProgrammaticallyDateFilter from 'examples/column-filter/programmatically-date-filter';
|
||||
import CustomFilter from 'examples/column-filter/custom-filter';
|
||||
import AdvanceCustomFilter from 'examples/column-filter/advance-custom-filter';
|
||||
|
||||
// work on rows
|
||||
import RowStyleTable from 'examples/rows/row-style';
|
||||
@@ -186,7 +188,9 @@ storiesOf('Column Filter', module)
|
||||
.add('Programmatically Text Filter', () => <ProgrammaticallyTextFilter />)
|
||||
.add('Programmatically Select Filter', () => <ProgrammaticallySelectFilter />)
|
||||
.add('Programmatically Number Filter', () => <ProgrammaticallyNumberFilter />)
|
||||
.add('Programmatically Date Filter', () => <ProgrammaticallyDateFilter />);
|
||||
.add('Programmatically Date Filter', () => <ProgrammaticallyDateFilter />)
|
||||
.add('Custom Filter', () => <CustomFilter />)
|
||||
.add('Advance Custom Filter', () => <AdvanceCustomFilter />);
|
||||
|
||||
storiesOf('Work on Rows', module)
|
||||
.add('Customize Row Style', () => <RowStyleTable />)
|
||||
|
||||
Reference in New Issue
Block a user