Skip to content

Commit 5cdfefe

Browse files
committed
adjust header&body column width only on scrolbar exist, and verfify it by body height instead of body width
1 parent 0390b8e commit 5cdfefe

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

src/BootstrapTable.js

+32-21
Original file line numberDiff line numberDiff line change
@@ -964,32 +964,43 @@ class BootstrapTable extends Component {
964964

965965
_adjustHeaderWidth() {
966966
const header = this.refs.header.getHeaderColGrouop();
967-
const headerContainer = this.refs.header.refs.container;
968967
const tbody = this.refs.body.refs.tbody;
968+
const bodyHeader = this.refs.body.getHeaderColGrouop();
969969
const firstRow = tbody.childNodes[0];
970-
const isScroll = headerContainer.offsetWidth !== tbody.parentNode.offsetWidth;
970+
const isScroll = tbody.parentNode.getBoundingClientRect().height >
971+
tbody.parentNode.parentNode.getBoundingClientRect().height;
972+
971973
const scrollBarWidth = isScroll ? Util.getScrollBarWidth() : 0;
972974
if (firstRow && this.store.getDataNum()) {
973-
const cells = firstRow.childNodes;
974-
for (let i = 0; i < cells.length; i++) {
975-
const cell = cells[i];
976-
const computedStyle = window.getComputedStyle(cell);
977-
let width = parseFloat(computedStyle.width.replace('px', ''));
978-
if (this.isIE) {
979-
const paddingLeftWidth = parseFloat(computedStyle.paddingLeft.replace('px', ''));
980-
const paddingRightWidth = parseFloat(computedStyle.paddingRight.replace('px', ''));
981-
const borderRightWidth = parseFloat(computedStyle.borderRightWidth.replace('px', ''));
982-
const borderLeftWidth = parseFloat(computedStyle.borderLeftWidth.replace('px', ''));
983-
width = width + paddingLeftWidth + paddingRightWidth + borderRightWidth + borderLeftWidth;
984-
}
985-
const lastPadding = (cells.length - 1 === i ? scrollBarWidth : 0);
986-
if (width <= 0) {
987-
width = 120;
988-
cell.width = width + lastPadding + 'px';
975+
if (isScroll) {
976+
const cells = firstRow.childNodes;
977+
for (let i = 0; i < cells.length; i++) {
978+
const cell = cells[i];
979+
const computedStyle = window.getComputedStyle(cell);
980+
let width = parseFloat(computedStyle.width.replace('px', ''));
981+
if (this.isIE) {
982+
const paddingLeftWidth = parseFloat(computedStyle.paddingLeft.replace('px', ''));
983+
const paddingRightWidth = parseFloat(computedStyle.paddingRight.replace('px', ''));
984+
const borderRightWidth = parseFloat(computedStyle.borderRightWidth.replace('px', ''));
985+
const borderLeftWidth = parseFloat(computedStyle.borderLeftWidth.replace('px', ''));
986+
width = width + paddingLeftWidth + paddingRightWidth + borderRightWidth + borderLeftWidth;
987+
}
988+
const lastPadding = (cells.length - 1 === i ? scrollBarWidth : 0);
989+
if (width <= 0) {
990+
width = 120;
991+
cell.width = width + lastPadding + 'px';
992+
}
993+
const result = width + lastPadding + 'px';
994+
header[i].style.width = result;
995+
header[i].style.minWidth = result;
996+
if (cells.length - 1 === i) {
997+
bodyHeader[i].style.width = width + 'px';
998+
bodyHeader[i].style.minWidth = width + 'px';
999+
} else {
1000+
bodyHeader[i].style.width = result;
1001+
bodyHeader[i].style.minWidth = result;
1002+
}
9891003
}
990-
const result = width + lastPadding + 'px';
991-
header[i].style.width = result;
992-
header[i].style.minWidth = result;
9931004
}
9941005
} else {
9951006
React.Children.forEach(this.props.children, (child, i) => {

src/TableBody.js

+4
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,10 @@ class TableBody extends Component {
294294
return this.props.selectRow.mode === Const.ROW_SELECT_SINGLE ||
295295
this.props.selectRow.mode === Const.ROW_SELECT_MULTI;
296296
}
297+
298+
getHeaderColGrouop = () => {
299+
return this.refs.header.childNodes;
300+
}
297301
}
298302
TableBody.propTypes = {
299303
data: PropTypes.array,

0 commit comments

Comments
 (0)