Skip to content

Commit

Permalink
adjust header&body column width only on scrolbar exist, and verfify i…
Browse files Browse the repository at this point in the history
…t by body height instead of body width
  • Loading branch information
AllenFang committed Jan 22, 2017
1 parent 0390b8e commit 5cdfefe
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
53 changes: 32 additions & 21 deletions src/BootstrapTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -964,32 +964,43 @@ class BootstrapTable extends Component {

_adjustHeaderWidth() {
const header = this.refs.header.getHeaderColGrouop();
const headerContainer = this.refs.header.refs.container;
const tbody = this.refs.body.refs.tbody;
const bodyHeader = this.refs.body.getHeaderColGrouop();
const firstRow = tbody.childNodes[0];
const isScroll = headerContainer.offsetWidth !== tbody.parentNode.offsetWidth;
const isScroll = tbody.parentNode.getBoundingClientRect().height >
tbody.parentNode.parentNode.getBoundingClientRect().height;

const scrollBarWidth = isScroll ? Util.getScrollBarWidth() : 0;
if (firstRow && this.store.getDataNum()) {
const cells = firstRow.childNodes;
for (let i = 0; i < cells.length; i++) {
const cell = cells[i];
const computedStyle = window.getComputedStyle(cell);
let width = parseFloat(computedStyle.width.replace('px', ''));
if (this.isIE) {
const paddingLeftWidth = parseFloat(computedStyle.paddingLeft.replace('px', ''));
const paddingRightWidth = parseFloat(computedStyle.paddingRight.replace('px', ''));
const borderRightWidth = parseFloat(computedStyle.borderRightWidth.replace('px', ''));
const borderLeftWidth = parseFloat(computedStyle.borderLeftWidth.replace('px', ''));
width = width + paddingLeftWidth + paddingRightWidth + borderRightWidth + borderLeftWidth;
}
const lastPadding = (cells.length - 1 === i ? scrollBarWidth : 0);
if (width <= 0) {
width = 120;
cell.width = width + lastPadding + 'px';
if (isScroll) {
const cells = firstRow.childNodes;
for (let i = 0; i < cells.length; i++) {
const cell = cells[i];
const computedStyle = window.getComputedStyle(cell);
let width = parseFloat(computedStyle.width.replace('px', ''));
if (this.isIE) {
const paddingLeftWidth = parseFloat(computedStyle.paddingLeft.replace('px', ''));
const paddingRightWidth = parseFloat(computedStyle.paddingRight.replace('px', ''));
const borderRightWidth = parseFloat(computedStyle.borderRightWidth.replace('px', ''));
const borderLeftWidth = parseFloat(computedStyle.borderLeftWidth.replace('px', ''));
width = width + paddingLeftWidth + paddingRightWidth + borderRightWidth + borderLeftWidth;
}
const lastPadding = (cells.length - 1 === i ? scrollBarWidth : 0);
if (width <= 0) {
width = 120;
cell.width = width + lastPadding + 'px';
}
const result = width + lastPadding + 'px';
header[i].style.width = result;
header[i].style.minWidth = result;
if (cells.length - 1 === i) {
bodyHeader[i].style.width = width + 'px';
bodyHeader[i].style.minWidth = width + 'px';
} else {
bodyHeader[i].style.width = result;
bodyHeader[i].style.minWidth = result;
}
}
const result = width + lastPadding + 'px';
header[i].style.width = result;
header[i].style.minWidth = result;
}
} else {
React.Children.forEach(this.props.children, (child, i) => {
Expand Down
4 changes: 4 additions & 0 deletions src/TableBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ class TableBody extends Component {
return this.props.selectRow.mode === Const.ROW_SELECT_SINGLE ||
this.props.selectRow.mode === Const.ROW_SELECT_MULTI;
}

getHeaderColGrouop = () => {
return this.refs.header.childNodes;
}
}
TableBody.propTypes = {
data: PropTypes.array,
Expand Down

0 comments on commit 5cdfefe

Please sign in to comment.