---
title: "kendo ui grid fix header "
author: "lyh7609"
date: "2017-01-18T10:30:02Z"
tags: []
categories: ["Web前端"]
slug: "kendo-ui-grid-fix-header"
draft: false
lastmod: "2017-01-18T10:30:02Z"
id: 2353667
url: "https://lyh7609.iteye.com/blog/2353667"
---

refer to:http://dojo.telerik.com/ewis

 

 

&lt;!DOCTYPE html&gt;

&lt;html&gt;

&lt;head&gt;

    &lt;base href="http://demos.telerik.com/kendo-ui/grid/column-resizing"&gt;

    &lt;style&gt;html { font-size: 12px; font-family: Arial, Helvetica, sans-serif; }&lt;/style&gt;

    &lt;title&gt;&lt;/title&gt;

    &lt;link href="http://cdn.kendostatic.com/2014.2.903/styles/kendo.common.min.css" rel="stylesheet" /&gt;

    &lt;link href="http://cdn.kendostatic.com/2014.2.903/styles/kendo.default.min.css" rel="stylesheet" /&gt;

    &lt;script src="http://cdn.kendostatic.com/2014.2.903/js/jquery.min.js"&gt;&lt;/script&gt;

    &lt;script src="http://cdn.kendostatic.com/2014.2.903/js/kendo.web.min.js"&gt;&lt;/script&gt;

    &lt;style&gt;

     

        html,

        body

        {

            margin: 0;

            padding: 0;

        }

     

        #grid,

        #grid .k-grid-header

        {

            width: 100%;

            border-width: 0;

            padding: 0 !important;

        }

         

        #grid .k-grid-header

        {

            position: fixed;

            top: 0;

            z-index: 2;

        }

         

        #grid &gt; .k-grid-content

        {

            overflow: visible !important;

        }

      

      #grid &gt; .k-grid-pager

      {

          padding-left: 0;

      }

      

      #grid .k-pager-first

      {

      margin-left: .3em;

      }

    &lt;/style&gt;

&lt;/head&gt;

&lt;body&gt;

     

        &lt;div id="example"&gt;

            &lt;div id="grid"&gt;&lt;/div&gt;

 

            &lt;script&gt;

                $(document).ready(function() {

                    $("#grid").kendoGrid({

                        dataSource: {

                            type: "odata",

                          serverPaging: true,

                          serverSorting: true,

                            pageSize: 100,

                            transport: {

                                read: "http://demos.telerik.com/kendo-ui/service/Northwind.svc/Orders"

                            },

                            schema: {

                                model: {

                                    fields: {

                                        OrderID: { type: "number" },

                                        ShipCountry: { type: "string" },

                                        ShipCity: { type: "string" },

                                        ShipName: { type: "string" },

                                        OrderDate: { type: "date" },

                                        ShippedDate: { type: "date" }

                                    }

                                }

                            },

                            pageSize: 100

                        },

                        sortable: true,

                        resizable: true,

                        //pageable: true,

                      scrollable: {

                            virtual: true

                        },

                      columnResize: resizeHeaderPager,

                      dataBound: resizeHeaderPager,

                        columns: [

                            {

                                field: "OrderDate",

                                title: "Order Date",

                                width: 120,

                                format: "{0:MM/dd/yyyy}"

                            },

                            {

                                field: "ShipCountry",

                                title: "Ship Country"

                            },

                            {

                                field: "ShipCity",

                                title: "Ship City"

                            },

                            {

                                field: "ShipName",

                                title: "Ship Name"

                            },

                            {

                                field: "ShippedDate",

                                title: "Shipped Date",

                                format: "{0:MM/dd/yyyy}",

                                width: 200

                            },

                            {

                                field: "OrderID",

                                title: "ID",

                                width: 80

                            }

                        ]

                    });

                     

                    $(document.body).css("padding-top", $("#grid &gt; .k-grid-header").height());

                    var w = $("#grid .k-grid-header-wrap").width();

                  

                  $(window).scroll(function(){

                    $("#grid .k-grid-header").css("margin-left", - $(window).scrollLeft());

                  });

                });

              

              function resizeHeaderPager(e) {

                var newWidth = e.sender.table.width();

                  e.sender.wrapper.find(".k-grid-header,.k-grid-pager").each(function(){

                    $(this).width(newWidth);

                  });

              }

            &lt;/script&gt;

        &lt;/div&gt;

 

 

&lt;/body&gt;

&lt;/html&gt;