Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / Javascript
Tip/Trick

Kendo Grid: Column Freezing on Horizontal Scrolling

Rate me:
Please Sign up or sign in to vote.
4.71/5 (5 votes)
11 Sep 2014CPOL 44.4K   5   2
This tip is used to create a freezing Kendo Grid.

Introduction

Sometimes, we have lots of columns in a kendo grid with a Horizontal Scrollbar. In that case, sometimes we need some column to be fixed (not affected by the horizontal scrolling) for easier viewing of the data.

The column freezing functionality is included in the latest version of kendoUI. If we are using the older version of Kendo UI, then there is no built in feature for freezing columns on horizontal scrolling.

Here is a way in which we can achieve the same using CSS and Jquery:

  1. First, we have to divide the grid structure into 2 grids, one for the frozen columns and other for the scrollable columns.
  2. Add some CSS to the Freezable columns grid so that they can't be scrolled.
  3. We need to have some JS logic so that on vertically scrolling of non frozen column, automatically the frozen column will also be scrolled.

Here is the code for the same.

HTML

HTML
<div id="divFreezableEmployeeDetails"></div>
<div id="divScollableEmployeeDetails"></div>

Jquery

JavaScript
var freezedGridId = $("#divFreezableEmployeeDetails");

var ScollableGridId = $("#divScollableEmployeeDetails");


//Initialize and populate the grids

GetDatasource( function (dataSource) {

            if (dataSource != null) {
                InitializeFreezableEmployeeDetails(freezedGridId, dataSource );

                InitializeScollableEmployeeDetails(ScollableGridId , dataSource );            }
        });


//initialize the FreezedEmployeeDetails

function InitializeFreezableEmployeeDetails(gridId, source) {


        gridId.kendoGrid({
            dataSource: {
                data: source,
                schema: {
                    model: {
                        fields:
                        {
                            EmployeeId: { type: "int" },
                            Name: { type: "string" }
                        }
                    }
                }
            },


             height: 700,
            sortable: true,
            resizable: true,
            scrollable: true,
            width: 1700,
            columns: [
                {
                    title:"EmployeeId"
                    field: "EmployeeId"
                    width: 90
                },

                {
                    title:"Name"
                    field: "Name"
                    width: 90
                },

  }
            ]
        });



//initialize the ScollableEmployeeDetails
function InitializeScollableEmployeeDetails(gridId, source) {


        gridId.kendoGrid({
            dataSource: {
                data: source,
                schema: {
                    model: {
                        fields:
                        {
                            Age: { type: "int" },
                            Address: { type: "string" },

                            Exp:{type:"string"},

                            Dob:{type:"string"},

                            Skill:{type:"string"}
                        }
                    }
                }
            },


             height: 700,
            sortable: true,
            resizable: true,
            scrollable: true,
            width: 1700,
            columns: [
                {
                    title:"Age"
                    field: "Age"
                    width: 90
                },

                {
                    title:"Address"
                    field: "Address"
                    width: 90
                },

               {
                    title:"Exp"
                    field: "Exp"
                    width: 90
                },

                {
                    title:"Dob"
                    field: "Dob"
                    width: 90
                },

                {
                    title:"Skill"
                    field: "Skill"
                    width: 90
                },

  }
            ]
        });


//Make the Freezable grid vertically scrollable on vertical scrolling of Scrollable grid

$("#divScollableEmployeeDetails .k-grid-content").on("scroll", function(e) {
  $("#divFreezableEmployeeDetails .k-grid-content").scrollTop($(e.currentTarget).scrollTop());
});

CSS

CSS
#divFreezableEmployeeDetails{
  width: 100px;
  border-right: none;
  float:left
}
#divScollableEmployeeDetails {
  width: 200px;
  float:left
}
#divFreezableEmployeeDetails .k-grid-header {
  padding-right: 0 !important;
}
#divFreezableEmployeeDetails .k-grid-content{
  overflow-x: scroll;
  overflow-y: hidden;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow to Frozen columns kendo gride? Pin
asemonize202020-Dec-16 20:23
asemonize202020-Dec-16 20:23 
QuestionMake sure your solution actually runs Pin
DaveCline12-Sep-14 6:26
DaveCline12-Sep-14 6:26 
You may want to clean up your script, incorrectly formatted.
Also, provide a complete script if you can.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.