Setting Kendo UI grid height 100% of wrapper

后端 未结 4 1115
无人共我
无人共我 2020-12-13 21:45

I know there is an easy way to set fixed height of a Kendo UI grid through their API but for our specific needs, I need to make the grid expand in full height of its wrapper

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-13 22:08

    I created another solution that works when your html is different in the sense that its not just the grid alone but other content above it. The JSFiddle is located here.

    HTML

    hey there

    CSS

    html,
    body {
      margin: 0;
      padding: 0;
      height: 100%;
    }
    .container{
      height:100%;
    }
    .test{
      height:100px;
    }
    html {
      overflow: hidden;
    }
    

    JS

    function resizeGrid() {
      var gridElement = $("#grid");
      var dataArea = gridElement.find(".k-grid-content");
      var newHeight = gridElement.parent().innerHeight() - 2 -calcHeightsOfParentChildren(gridElement);
      var diff = gridElement.innerHeight() - dataArea.innerHeight();
      if((newHeight-diff)>0){
        gridElement.height(newHeight);
        dataArea.height(newHeight - diff);
      }
    }
    
    function calcHeightsOfParentChildren(element){
        var children = $(element).parent().children();
      var height = 0;
      $.each(children, function( index, value ) {
        if($(value).attr("id") != $(element).attr("id")){
            height = height + $(value).height();
        }
        });
        return height;
    }
    
    $(window).resize(function() {
      resizeGrid();
    });
    
    $("#grid").kendoGrid({
      dataSource: {
        type: "odata",
        transport: {
          read: "http://demos.kendoui.com/service/Northwind.svc/Orders"
        },
        schema: {
          model: {
            fields: {
              OrderID: {
                type: "number"
              },
              ShipName: {
                type: "string"
              },
              ShipCity: {
                type: "string"
              }
            }
          }
        },
        pageSize: 10,
        serverPaging: true,
        serverFiltering: true,
        serverSorting: true
      },
      height: 250,
      filterable: true,
      sortable: true,
      pageable: true,
      dataBound: resizeGrid,
      columns: [{
          field: "OrderID",
          filterable: false
        },
        "ShipName",
        "ShipCity"
      ]
    });
    

    The key in my solution is the modified resizeGrid function. What happens without this modification is if the user scrolls far enough upwards the bottom pager gets lost! By checking to make sure the newHeight-diff is greater than 0 it prevents this error.

    Second calcHeightsOfParentChildren when passed the element of the grid in question will calculate all the other heights except its own to help calculate the correct difference to place the pager control and grid so that it truly takes up 100% and retains its 100% ratio.

提交回复
热议问题