I have a function that uses window height to set a bootstrapTable’s height. In addition, I’m using a resize event listener to change the height of the table if the window is resized.
This is inside an electron project on a windows machine.
This is the first part of the code which is triggered when a button is clicked:
var $table = $("#results-table");
var $availableHeight =
$(window).height() -
$("#results-header").outerHeight(true) -
$("#search-section").outerHeight(true) -
50;
$table
.bootstrapTable("destroy")
.bootstrapTable({ data: data, height: $availableHeight });
If I log the window height to console I get a window height of 835px which is correct and the table is sized as I expect.
This is the same code within a window resize event listener:
window.addEventListener("resize", function (event) {
var $table = $("#results-table");
var $tableData = $table.bootstrapTable("getData");
var $availableHeight =
$(window).height() -
$("#results-header").outerHeight(true) -
$("#search-section").outerHeight(true) -
50;
$table
.bootstrapTable("destroy")
.bootstrapTable({ data: $tableData, height: $availableHeight });
});
If I maximize the window the table resizes correctly. Unfortunately when I restore the window back to its original size (by clicking the min/max button again) and log window height to console it now shows as 818px even though the window size has not actually changed (it is still has a height of 835px). This causes my table to be slightly shorter than it was originally.
I haven’t been able to figure out why this is happening.
2
Answers
Using
setTimeout
to get window height after rendering completed,$(window).height()
, anddocument.documentElement.clientHeight
all return the same inconsistent results (835px after the window is initiated and 818px after it's been maximized/restored to the original size).window.innerHeight
consistently returned 835px so that was my solution.Try these approaches:
Debounce Resize:
Use
document.documentElement.clientHeight
instead of$(window).height()
for stable height measurements.Electron Resize Workaround: Use Electron’s
BrowserWindow
API to get accurate dimensions on resize.