Skip to content

Improvement table body row render calculator #566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions examples/js/selection/big-row-select-table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint max-len: 0 */
import React from 'react';
import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table';


const products = [];

function addProducts(quantity) {
const startId = products.length;
for (let i = 0; i < quantity; i++) {
const id = startId + i;
products.push({
id: id,
name: 'Item name ' + id,
price: 2100 + i
});
}
}

addProducts(1000);

const selectRowProp = {
mode: 'checkbox'
};

export default class BigRowSelectTable extends React.Component {
render() {
return (
<BootstrapTable data={ products } selectRow={ selectRowProp } height='400'>
<TableHeaderColumn dataField='id' isKey={ true }>Product ID</TableHeaderColumn>
<TableHeaderColumn dataField='name'>Product Name</TableHeaderColumn>
<TableHeaderColumn dataField='price'>Product Price</TableHeaderColumn>
</BootstrapTable>
);
}
}
10 changes: 10 additions & 0 deletions examples/js/selection/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import SelectValidationTable from './select-validation-table';
import RowClickTable from './row-click-table';
import OnlySelectedTable from './only-show-selected-table';
import ExternallyManagedSelection from './externally-managed-selection';
import BigRowSelectTable from './big-row-select-table';

class Demo extends React.Component {
render() {
Expand Down Expand Up @@ -126,6 +127,15 @@ class Demo extends React.Component {
</div>
</div>
</div>
<div className='col-md-offset-1 col-md-8'>
<div className='panel panel-default'>
<div className='panel-heading'>Handle big data row select (Maybe > 1000)</div>
<div className='panel-body'>
<h5>Source in big-row-select-table.js</h5>
<BigRowSelectTable />
</div>
</div>
</div>
</div>
);
}
Expand Down
34 changes: 33 additions & 1 deletion src/TableBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,38 @@ const isFun = function(obj) {
return obj && (typeof obj === 'function');
};

const emptyTableRowHeight = {
height: 37,
visibility: 'hidden'
};

class TableBody extends Component {

constructor(props) {
super(props);
this.state = {
currEditCell: null
currEditCell: null,
scrollTop: 0,
availableHeight: 0
};
this.editing = false;
}

_handleTableScroll = (e) => {
this.setState({
scrollTop: e.target.scrollTop,
availableHeight: e.target.clientHeight
});
}

componentDidMount() {
this.refs.container.addEventListener('scroll', this._handleTableScroll);
}

componentWillUnmount() {
this.refs.container.removeEventListener('scroll', this._handleTableScroll);
}

render() {
const tableClasses = classSet('table', {
'table-striped': this.props.striped,
Expand All @@ -32,7 +54,17 @@ class TableBody extends Component {
const tableHeader = this.renderTableHeader(isSelectRowDefined);
const inputType = this.props.selectRow.mode === Const.ROW_SELECT_SINGLE ? 'radio' : 'checkbox';

const rowHeight = 37;
const numRows = this.props.data.length;
const scrollBottom = this.state.scrollTop + this.state.availableHeight;
const startIndex = Math.max(0, Math.floor(this.state.scrollTop / rowHeight));
const RowEndIndex = Math.min(numRows, Math.ceil(scrollBottom / rowHeight) + 10);
const RowIndex = startIndex;

const tableRows = this.props.data.map(function(data, r) {
if (r < RowIndex || r > RowEndIndex) {
return <tr style={ emptyTableRowHeight } key={ r }><td></td></tr>;
}
const tableColumns = this.props.columns.map(function(column, i) {
const fieldValue = data[column.name];
if (this.editing &&
Expand Down