Nisrak
Nisrak

Reputation: 335

jqGrid horizontal scrollbar at top by header

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!

Upvotes: 3

Views: 4096

Answers (1)

Oleg
Oleg

Reputation: 221997

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.

Upvotes: 6

Related Questions