Skip to content

Commit afdb4dc

Browse files
authored
window.matchMedia check
1 parent 59b90e6 commit afdb4dc

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

django/django-admin-horizontal-scroll.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,26 @@ Here's the recipe I came up with for doing that for tables in the Django admin:
1515
function resizeTable() {
1616
/* So Windows mouse users can see the horizontal scrollbar
1717
https://github.com/CAVaccineInventory/vial/issues/363 */
18-
let container = document.querySelector("#changelist-form .results");
19-
let paginator = document.querySelector("p.paginator");
20-
if (!container || !paginator) {
21-
return;
18+
if (window.matchMedia('screen and (min-width: 800px)').matches) {
19+
let container = document.querySelector("#changelist-form .results");
20+
let paginator = document.querySelector("p.paginator");
21+
if (!container || !paginator) {
22+
return;
23+
}
24+
let height =
25+
window.innerHeight -
26+
container.getBoundingClientRect().top -
27+
paginator.getBoundingClientRect().height -
28+
10;
29+
container.style.overflowY = "auto";
30+
container.style.height = height + "px";
2231
}
23-
let height =
24-
window.innerHeight -
25-
container.getBoundingClientRect().top -
26-
paginator.getBoundingClientRect().height -
27-
10;
28-
container.style.overflowY = "auto";
29-
container.style.height = height + "px";
3032
}
3133
window.addEventListener("load", resizeTable);
3234
</script>
3335
```
36+
I added the `window.matchMedia()` check when I realized that this approach wasn't useful at mobile screen sizes.
37+
3438
Here `#changelist-form .results` is a `<div>` that wraps the main table on the page, and `p.paginator` is the pagination links shown directly below the table. I decided to set the vertically scrollable height to `window height - top-of-table - paginator height - 10px`.
3539

3640
I added this code to my project's custom `admin/base_site.html` template, which now looks something like this:

0 commit comments

Comments
 (0)