jqGrid horizontal scrollbar at top by header
Asked Answered
P

1

3

Just curious if anyone knows of a way to add a horizontal scrollbar to the top of jqGrid (like just under or above the headers)? I'm using frozen columns and height:auto but when there are lots of rows, the user has to scroll down to scroll the grid horizontally and can't see the headers... I did some searching and it looks like scrollbars are hard to mess with in most cases, but I figured I would just put it out there.

Thanks!

Pneumatograph answered 25/9, 2013 at 0:38 Comment(1)
Oleg, I just noticed one more snag related to the top scrollbar. I have a grid with some columns hidden by default. If I make them visible with the column chooser, the top scrollbar still only scrolls as far as the original final column, although the bottom scrollbar scrolls all the way to the end of the grid. Am I missing something to fix this? (Again sorry, don't have enough rep to make a comment.)Camara
T
6

I find your question interesting. I invested some time and created the following demo which demonstrates how your requirements could be implemented. It displays

enter image description here

where one can use both horizontal scrollbars on the top or on the bottom of the grid. I used the answer as the basis of creating the top scroll bar. Additionally I included the part of code which fixed the size and position of all jqGrid dives in case if the user uses zoom in the web browser.

The most important part of the code of my answer I included below:

var $grid = $("#list");
// create the grid with some frozen columns
$grid.jqGrid({
    ....
});

var $gview = $grid.closest(".ui-jqgrid-view"),
    $topToolbar = $gview.find(">.ui-userdata"),
    $bdiv = $grid.closest(".ui-jqgrid-bdiv"),
    resetTopToolbarHeight = function () {
        var scrollbarHeight = 18; // some test value
        $topToolbar.find(">div").height(scrollbarHeight);
        $topToolbar.css("border-top", "0").css("height", "auto");
        scrollbarHeight = $topToolbar.height() - scrollbarHeight;
        $topToolbar.find(">div").height(scrollbarHeight);
        $topToolbar.height(scrollbarHeight);
        fixPositionsOfFrozenDivs.call($grid[0]);
    };
// insert empty div in the top toolbar and make its width
// the same as the width of the grid
$topToolbar.css({ overflowX: "scroll", overflowY: "hidden"})
    .append($("<div>").width($grid.width()));
// set the height of the div and the height of toolbar
// based on the height of the horizontal scrollbar
resetTopToolbarHeight();
// detect scrolling of topbar
$topToolbar.scroll(function () {
    // synchronize the srollbar of the grid
    $bdiv.scrollLeft($(this).scrollLeft());
});
// detect scrolling of the grid
$bdiv.scroll(function () {
    // synchronize the srollbar of the toppbar
    $topToolbar.scrollLeft($(this).scrollLeft());
});
// detect zoop of the page and adjust the
$(window).on("resize", function () {
    resetTopToolbarHeight();
    fixPositionsOfFrozenDivs.call($grid[0]);
});

Other parts of the code I get from my old answers about the usage of frozen columns.

Trierarch answered 25/9, 2013 at 10:33 Comment(3)
Great solution I didn't think it would be solved so quickly and well... This is going to make my application that much slicker!Pneumatograph
Oleg's answer is great, I will just add that in order to get it working I needed to make sure the toolbar: [true, "top"] option was used, as explained in Oleg's answer here: stackoverflow.com/a/21252652, and also add in the fixPositionsOfFrozenDivs function, which I found from the source of Oleg's demo. (Sorry I couldn't add this as a comment on Oleg's answer, as I'm a new user and don't have the rep to do so.)Camara
@SLee: I'm glad that the old answer can help you. You are right - the toolbar: [true, "top"] is required of cause. It will be used in the demo, but not explicitly mentioned in the text of the answer. The fixPositionsOfFrozenDivs is required only if you use old version of jqGrid (in version <=4.7). The implementation of frozen columns in free jqGrid fork, which I started to develop later, should not require this (I hope).Trierarch

© 2022 - 2024 — McMap. All rights reserved.