Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding / Showing Kendo grid scrollbar

I want to show my kendo grid's scrollbar only if it's necessary. Here is my grid initialization:

@(Html.Kendo().Grid<UT.Repo.Core.up_HedgedCustomerLatestTradeListGet_Result>()
    .Name("lastPositionsGrid")
    .Columns(columns =>
    {
        columns.Bound(c => c.ACCOUNT).Title("Hesap").Width(70);
        columns.Bound(c => c.TICKET).Title("Emir");
        columns.Bound(c => c.SIDE).Title("Yön").Width(50);
        columns.Bound(c => c.STATE).Title("Durum").Width(65);
        columns.Bound(c => c.SYMBOL).Title("Sembol");
        columns.Bound(c => c.VOLUME).Title("Hacim").Width(65);
        columns.Bound(c => c.OPENPX).Title("Açılış");
        columns.Bound(c => c.CLOSEPX).Title("Kapanış");
        columns.Bound(c => c.P_L).Title("Kar Zarar").Width(75);
        columns.Bound(c => c.SL).Title("Zararı Durdur");
        columns.Bound(c => c.TP).Title("Karı Al");
        columns.Bound(c => c.TIME).Title("Zaman").ClientTemplate("#= kendo.toString(TIME, \"dd/MM/yyyy HH:mm:ss\") #").Width(160);
    })
    .Scrollable()
    .Sortable()
    .Resizable(resize => resize.Columns(true))
    .Events(events => events.DataBound("onLastPositionsGridDataBound"))
    .DataSource(dataSource => dataSource
        .Ajax()
        .Sort(sort => sort.Add("TIME").Descending())
        .Read(read => read.Action("HedgedCustomerLatestTradeListGet", "Home"))
    )
)

On data bound I'm trying to set visibility of scrollbar:

function onLastPositionsGridDataBound(e) {
    var gridHeight = $("#lastPositionsGrid").outerHeight();
    var gridHeaderHeight = $("#lastPositionsGrid table:eq(0)").outerHeight();
    var gridBodyHeight = $("#lastPositionsGrid table:eq(1)").outerHeight();
    if (gridHeight < gridHeaderHeight + gridBodyHeight) { // show the scrollbar
        $("#lastPositionsGrid .k-grid-header").css('padding', '');
        $("#lastPositionsGrid .k-grid-header").css('padding-right', '17px');
        $("#lastPositionsGrid .k-grid-content").css('overflow-y','scroll');
    }
    else { // hide the scrollbar
        $("#lastPositionsGrid .k-grid-header").css('padding','0 !important');
        $("#lastPositionsGrid .k-grid-content").css('overflow-y', 'visible');
    }
}

The part that hides the scrollbar does its work nicely but the part that shows the scrollbar does not. Here is a screenshot after showing the scrollbar:

enter image description here

As you see the lines those separate the cells in header and the rows does not fit. How can I fix it?

like image 287
anilca Avatar asked Feb 29 '16 08:02

anilca


People also ask

How do I hide the scrollbar in kendo grid?

To remove vertical scrollbar, you have to change the CSS on the Kendo UI Grid. Replace #GridId with your grid id.

How do I hide Kendo grid?

Kendo grid doesn't have HIDE/SHOW property. You need to do this in JQuery. At run time, kendo grid will convert it into DIV tag. you need to hide/show DIV tag in jquery.

How do I add a vertical scrollbar to Kendo grid?

You can use something like this to show the vertical scroll bar: $("#grid . k-grid-content"). css({ "overflow-y": "scroll" });

How do I add a horizontal scroll bar in kendo grid?

Apply Scrollable() and give Width for each columns and then if the total width exceeds the width of the grid the horizontal scrollbar will appear.


2 Answers

this will work, try overflow auto always.

function onLastPositionsGridDataBound(e) {
    var gridHeight = $("#lastPositionsGrid").outerHeight();
    var gridHeaderHeight = $("#lastPositionsGrid table:eq(0)").outerHeight();
    var gridBodyHeight = $("#lastPositionsGrid table:eq(1)").outerHeight();
    if (gridHeight < gridHeaderHeight + gridBodyHeight) { // show the scrollbar
        $("#lastPositionsGrid .k-grid-header").css('padding', '');
        $("#lastPositionsGrid .k-grid-header").css('padding-right', '17px');
        $("#lastPositionsGrid .k-grid-content").css('overflow-y','auto');
    }
    else { // hide the scrollbar
        $("#lastPositionsGrid .k-grid-header").css('padding','0 !important');
        $("#lastPositionsGrid .k-grid-content").css('overflow-y', 'auto');
    }
}
like image 125
Ajai Krishnan R Avatar answered Oct 20 '22 01:10

Ajai Krishnan R


I just changed two things in your javascript and it worked like charm. Thanks

  1. Changing the value from 17px to 21px. So basically you have to find the right value of padding-right.

  2. Changing the value from '0 !important' to '0px'. To fix the header to not show an extra column at the end.

No Scrollbar

With Scrollbar

like image 21
Akshay G Avatar answered Oct 20 '22 00:10

Akshay G