mirror of
https://github.com/gosticks/react-bootstrap-table2.git
synced 2025-10-16 11:55:39 +00:00
refine remote examples
This commit is contained in:
parent
5cbeae704b
commit
e14c596b8c
202
packages/react-bootstrap-table2-example/examples/remote/remote-all.js
vendored
Normal file
202
packages/react-bootstrap-table2-example/examples/remote/remote-all.js
vendored
Normal file
@ -0,0 +1,202 @@
|
||||
/* eslint guard-for-in: 0 */
|
||||
/* eslint no-restricted-syntax: 0 */
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import BootstrapTable from 'react-bootstrap-table2';
|
||||
import paginator from 'react-bootstrap-table2-paginator';
|
||||
import filterFactory, { textFilter, Comparator } from 'react-bootstrap-table2-filter';
|
||||
import Code from 'components/common/code-block';
|
||||
import { productsGenerator } from 'utils/common';
|
||||
|
||||
const products = productsGenerator(87);
|
||||
|
||||
const columns = [{
|
||||
dataField: 'id',
|
||||
text: 'Product ID'
|
||||
}, {
|
||||
dataField: 'name',
|
||||
text: 'Product Name',
|
||||
filter: textFilter()
|
||||
}, {
|
||||
dataField: 'price',
|
||||
text: 'Product Price',
|
||||
filter: textFilter()
|
||||
}];
|
||||
|
||||
const sourceCode = `\
|
||||
import BootstrapTable from 'react-bootstrap-table2';
|
||||
import paginator from 'react-bootstrap-table2-paginator';
|
||||
import filterFactory, { textFilter, Comparator } from 'react-bootstrap-table2-filter';
|
||||
// ...
|
||||
|
||||
const columns = [{
|
||||
dataField: 'id',
|
||||
text: 'Product ID'
|
||||
}, {
|
||||
dataField: 'name',
|
||||
text: 'Product Name',
|
||||
filter: textFilter()
|
||||
}, {
|
||||
dataField: 'price',
|
||||
text: 'Product Price',
|
||||
filter: textFilter()
|
||||
}];
|
||||
|
||||
const RemoteAll = ({ data, page, sizePerPage, onTableChange, totalSize }) => (
|
||||
<div>
|
||||
<BootstrapTable
|
||||
remote={ { pagination: true } }
|
||||
keyField="id"
|
||||
data={ data }
|
||||
columns={ columns }
|
||||
filter={ filterFactory() }
|
||||
pagination={ paginator({ page, sizePerPage, totalSize }) }
|
||||
onTableChange={ onTableChange }
|
||||
/>
|
||||
<Code>{ sourceCode }</Code>
|
||||
</div>
|
||||
);
|
||||
|
||||
RemoteAll.propTypes = {
|
||||
data: PropTypes.array.isRequired,
|
||||
page: PropTypes.number.isRequired,
|
||||
totalSize: PropTypes.number.isRequired,
|
||||
sizePerPage: PropTypes.number.isRequired,
|
||||
onTableChange: PropTypes.func.isRequired
|
||||
};
|
||||
|
||||
class Container extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
page: 1,
|
||||
data: products.slice(0, 10),
|
||||
totalSize: products.length,
|
||||
sizePerPage: 10
|
||||
};
|
||||
this.handleTableChange = this.handleTableChange.bind(this);
|
||||
}
|
||||
|
||||
handleTableChange = (type, { page, sizePerPage, filters }) => {
|
||||
const currentIndex = (page - 1) * sizePerPage;
|
||||
setTimeout(() => {
|
||||
const result = products.filter((row) => {
|
||||
let valid = true;
|
||||
for (const dataField in filters) {
|
||||
const { filterVal, filterType, comparator } = filters[dataField];
|
||||
|
||||
if (filterType === 'TEXT') {
|
||||
if (comparator === Comparator.LIKE) {
|
||||
valid = row[dataField].toString().indexOf(filterVal) > -1;
|
||||
} else {
|
||||
valid = row[dataField] === filterVal;
|
||||
}
|
||||
}
|
||||
if (!valid) break;
|
||||
}
|
||||
return valid;
|
||||
});
|
||||
this.setState(() => ({
|
||||
page,
|
||||
data: result.slice(currentIndex, currentIndex + sizePerPage),
|
||||
totalSize: result.length,
|
||||
sizePerPage
|
||||
}));
|
||||
}, 2000);
|
||||
}
|
||||
|
||||
render() {
|
||||
const { data, sizePerPage, page } = this.state;
|
||||
return (
|
||||
<RemoteAll
|
||||
data={ data }
|
||||
page={ page }
|
||||
sizePerPage={ sizePerPage }
|
||||
totalSize={ this.state.totalSize }
|
||||
onTableChange={ this.handleTableChange }
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
const RemoteAll = ({ data, page, sizePerPage, onTableChange, totalSize }) => (
|
||||
<div>
|
||||
<h3>When <code>remote.pagination</code> is enabled, the filtering,
|
||||
sorting and searching will also change to remote mode automatically</h3>
|
||||
<BootstrapTable
|
||||
remote={ { pagination: true } }
|
||||
keyField="id"
|
||||
data={ data }
|
||||
columns={ columns }
|
||||
filter={ filterFactory() }
|
||||
pagination={ paginator({ page, sizePerPage, totalSize }) }
|
||||
onTableChange={ onTableChange }
|
||||
/>
|
||||
<Code>{ sourceCode }</Code>
|
||||
</div>
|
||||
);
|
||||
|
||||
RemoteAll.propTypes = {
|
||||
data: PropTypes.array.isRequired,
|
||||
page: PropTypes.number.isRequired,
|
||||
totalSize: PropTypes.number.isRequired,
|
||||
sizePerPage: PropTypes.number.isRequired,
|
||||
onTableChange: PropTypes.func.isRequired
|
||||
};
|
||||
|
||||
class Container extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
page: 1,
|
||||
data: products.slice(0, 10),
|
||||
totalSize: products.length,
|
||||
sizePerPage: 10
|
||||
};
|
||||
this.handleTableChange = this.handleTableChange.bind(this);
|
||||
}
|
||||
|
||||
handleTableChange = (type, { page, sizePerPage, filters }) => {
|
||||
const currentIndex = (page - 1) * sizePerPage;
|
||||
setTimeout(() => {
|
||||
const result = products.filter((row) => {
|
||||
let valid = true;
|
||||
for (const dataField in filters) {
|
||||
const { filterVal, filterType, comparator } = filters[dataField];
|
||||
|
||||
if (filterType === 'TEXT') {
|
||||
if (comparator === Comparator.LIKE) {
|
||||
valid = row[dataField].toString().indexOf(filterVal) > -1;
|
||||
} else {
|
||||
valid = row[dataField] === filterVal;
|
||||
}
|
||||
}
|
||||
if (!valid) break;
|
||||
}
|
||||
return valid;
|
||||
});
|
||||
this.setState(() => ({
|
||||
page,
|
||||
data: result.slice(currentIndex, currentIndex + sizePerPage),
|
||||
totalSize: result.length,
|
||||
sizePerPage
|
||||
}));
|
||||
}, 2000);
|
||||
}
|
||||
|
||||
render() {
|
||||
const { data, sizePerPage, page } = this.state;
|
||||
return (
|
||||
<RemoteAll
|
||||
data={ data }
|
||||
page={ page }
|
||||
sizePerPage={ sizePerPage }
|
||||
totalSize={ this.state.totalSize }
|
||||
onTableChange={ this.handleTableChange }
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default Container;
|
||||
105
packages/react-bootstrap-table2-example/examples/remote/remote-filter.js
vendored
Normal file
105
packages/react-bootstrap-table2-example/examples/remote/remote-filter.js
vendored
Normal file
@ -0,0 +1,105 @@
|
||||
/* eslint guard-for-in: 0 */
|
||||
/* eslint no-restricted-syntax: 0 */
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import BootstrapTable from 'react-bootstrap-table2';
|
||||
import filterFactory, { textFilter, Comparator } from 'react-bootstrap-table2-filter';
|
||||
import Code from 'components/common/code-block';
|
||||
import { productsGenerator } from 'utils/common';
|
||||
|
||||
const products = productsGenerator(17);
|
||||
|
||||
const columns = [{
|
||||
dataField: 'id',
|
||||
text: 'Product ID'
|
||||
}, {
|
||||
dataField: 'name',
|
||||
text: 'Product Name',
|
||||
filter: textFilter()
|
||||
}, {
|
||||
dataField: 'price',
|
||||
text: 'Product Price',
|
||||
filter: textFilter()
|
||||
}];
|
||||
|
||||
const sourceCode = `\
|
||||
import filterFactory, { textFilter } from 'react-bootstrap-table2-filter';
|
||||
|
||||
const columns = [{
|
||||
dataField: 'id',
|
||||
text: 'Product ID',
|
||||
}, {
|
||||
dataField: 'name',
|
||||
text: 'Product Name',
|
||||
filter: textFilter()
|
||||
}, {
|
||||
dataField: 'price',
|
||||
text: 'Product Price',
|
||||
filter: textFilter()
|
||||
}];
|
||||
|
||||
<BootstrapTable keyField='id' data={ products } columns={ columns } filter={ filterFactory() } />
|
||||
`;
|
||||
|
||||
const RemoteFilter = props => (
|
||||
<div>
|
||||
<BootstrapTable
|
||||
remote={ { filter: true } }
|
||||
keyField="id"
|
||||
data={ props.data }
|
||||
columns={ columns }
|
||||
filter={ filterFactory() }
|
||||
onTableChange={ props.onTableChange }
|
||||
/>
|
||||
<Code>{ sourceCode }</Code>
|
||||
</div>
|
||||
);
|
||||
|
||||
RemoteFilter.propTypes = {
|
||||
data: PropTypes.array.isRequired,
|
||||
onTableChange: PropTypes.func.isRequired
|
||||
};
|
||||
|
||||
class Container extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
data: products
|
||||
};
|
||||
}
|
||||
|
||||
handleTableChange = (type, { filters }) => {
|
||||
setTimeout(() => {
|
||||
const result = products.filter((row) => {
|
||||
let valid = true;
|
||||
for (const dataField in filters) {
|
||||
const { filterVal, filterType, comparator } = filters[dataField];
|
||||
|
||||
if (filterType === 'TEXT') {
|
||||
if (comparator === Comparator.LIKE) {
|
||||
valid = row[dataField].toString().indexOf(filterVal) > -1;
|
||||
} else {
|
||||
valid = row[dataField] === filterVal;
|
||||
}
|
||||
}
|
||||
if (!valid) break;
|
||||
}
|
||||
return valid;
|
||||
});
|
||||
this.setState(() => ({
|
||||
data: result
|
||||
}));
|
||||
}, 2000);
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<RemoteFilter
|
||||
data={ this.state.data }
|
||||
onTableChange={ this.handleTableChange }
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default Container;
|
||||
@ -105,7 +105,7 @@ class Container extends React.Component {
|
||||
};
|
||||
}
|
||||
|
||||
handleTableChange = ({ page, sizePerPage }) => {
|
||||
handleTableChange = (type, { page, sizePerPage }) => {
|
||||
const currentIndex = (page - 1) * sizePerPage;
|
||||
setTimeout(() => {
|
||||
this.setState(() => ({
|
||||
@ -82,12 +82,16 @@ import HideSelectionColumnTable from 'examples/row-selection/hide-selection-colu
|
||||
import PaginationTable from 'examples/pagination';
|
||||
import PaginationHooksTable from 'examples/pagination/pagination-hooks';
|
||||
import CustomPaginationTable from 'examples/pagination/custom-pagination';
|
||||
import RemotePaginationTable from 'examples/pagination/remote-pagination';
|
||||
|
||||
// loading overlay
|
||||
import EmptyTableOverlay from 'examples/loading-overlay/empty-table-overlay';
|
||||
import TableOverlay from 'examples/loading-overlay/table-overlay';
|
||||
|
||||
// remote
|
||||
import RemoteFilter from 'examples/remote/remote-filter';
|
||||
import RemotePaginationTable from 'examples/remote/remote-pagination';
|
||||
import RemoteAll from 'examples/remote/remote-all';
|
||||
|
||||
// css style
|
||||
import 'bootstrap/dist/css/bootstrap.min.css';
|
||||
import 'stories/stylesheet/tomorrow.min.css';
|
||||
@ -179,9 +183,13 @@ storiesOf('Row Selection', module)
|
||||
storiesOf('Pagination', module)
|
||||
.add('Basic Pagination Table', () => <PaginationTable />)
|
||||
.add('Pagination Hooks', () => <PaginationHooksTable />)
|
||||
.add('Custom Pagination', () => <CustomPaginationTable />)
|
||||
.add('Remote Pagination', () => <RemotePaginationTable />);
|
||||
.add('Custom Pagination', () => <CustomPaginationTable />);
|
||||
|
||||
storiesOf('EmptyTableOverlay', module)
|
||||
.add('Empty Table Overlay', () => <EmptyTableOverlay />)
|
||||
.add('Table Overlay', () => <TableOverlay />);
|
||||
|
||||
storiesOf('Remote', module)
|
||||
.add('Remote Filter', () => <RemoteFilter />)
|
||||
.add('Remote Pagination', () => <RemotePaginationTable />)
|
||||
.add('Remote All', () => <RemoteAll />);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user