Kendo Grid:调整(缩小)任何列的大小时最后留空白

时间:2019-07-11 05:39:48

标签: kendo-ui kendo-grid

我有一个网格,当我调整(缩小)任何列的大小时,网格末尾会出现空白。我已经与Kendo官方样本进行了核对,似乎在某些样本中显示的行为出现在他们的样本中。

我尝试设置标题,单元格内容等的宽度。但是它仍然显示一些UI问题,并且我有多个网格,需要通用修复程序。

如果这不是问题和行为,那么请有人看看并解释如何解决。

我添加了普通的屏幕截图并调整了屏幕截图的大小。

普通

enter image description here

调整大小后

enter image description here

为了对其进行测试,我添加了一个jsfiddle。

http://jsfiddle.net/49bhz2sk/

html

<div class="panel panel-body">
    <div id="fleetInfoGridDisplayDummy" class="" data-bind="autoHeightContainer:true"></div>
</div>

脚本

 $("#fleetInfoGridDisplayDummy").kendoGrid({
                dataSource: {
                    type: "odata",
                    transport: {
                        read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Customers"
                    },
                    pageSize: 20
                },
                navigatable: true,
                selectable: true,
                sortable: true,
                reorderable: true,
                resizable: true,
                scrollable: { virtual: true },
                columnMenu: true, // Needed to hide and show columns.
                pageable: {
                    refresh: true,
                    pageSizes: true,
                    buttonCount: 5
                },
                columns: [{
                    template: "<div class='customer-photo'" +
                    "style='background-image: url(../content/web/Customers/#:data.CustomerID#.jpg);'></div>" +
                    "<div class='customer-name'>#: ContactName #</div>",
                    field: "ContactName",
                    title: "Contact Name",
                    width: 240
                }, {
                    field: "ContactTitle",
                    title: "Contact Title"
                }, {
                    field: "CompanyName",
                    title: "Company Name"
                }, {
                    field: "Country",
                    width: 150
                }]
            });

2 个答案:

答案 0 :(得分:0)

根据各种剑道来源,这是observed normal behavior (2013)unexpected behavior (2017)的混合。 Kendo确实为此问题提供了a workaround,因为我怀疑它不一定与kendo有关,而是更多的HTML / Table功能。

<style>
    .k-grid {
        width: 700px;
    }
</style>

<div id="grid1"></div>

<script>
    function getMasterColumnsWidth(tbl) {
        var result = 0;
        tbl.children("colgroup").find("col").not(":last").each(function (idx, element) {
            result += parseInt($(element).outerWidth() || 0, 10);
        });

        return result;
    }

    function adjustLastColumn() {
        var grid = $("#grid1").data("kendoGrid");
        var contentDiv = grid.wrapper.children(".k-grid-content");
        var masterHeaderTable = grid.thead.parent();
        var masterBodyTable = contentDiv.children("table");
        var gridDivWidth = contentDiv.width() - kendo.support.scrollbar();

        masterHeaderTable.width("");
        masterBodyTable.width("");

        var headerWidth = getMasterColumnsWidth(masterHeaderTable),
            lastHeaderColElement = grid.thead.parent().find("col").last(),
            lastDataColElement = grid.tbody.parent().children("colgroup").find("col").last(),
            delta = parseInt(gridDivWidth, 10) - parseInt(headerWidth, 10);

        if (delta > 0) {
            delta = Math.abs(delta);
            lastHeaderColElement.width(delta);
            lastDataColElement.width(delta);
        } else {
            lastHeaderColElement.width(0);
            lastDataColElement.width(0);
        }

        contentDiv.scrollLeft(contentDiv.scrollLeft() - 1);
        contentDiv.scrollLeft(contentDiv.scrollLeft() + 1);
    }


    $("#grid1").kendoGrid({
        dataSource: {
            type: "odata",
            transport: {
                read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Employees"
            },
            pageSize: 6,
            serverPaging: true,
            serverSorting: true
        },
        height: 430,
        pageable: true,
        resizable: true,
        columnResize: adjustLastColumn,
        dataBound: adjustLastColumn,
        columns: [{
            field: "FirstName",
            title: "First Name",
            width: "100px"
        }, {
            field: "LastName",
            title: "Last Name",
            width: "150px"
        }, {
            field: "Country",
            width: "100px"
        }, {
            field: "City",
            width: "100px"
        }, {
            field: "Title",
            width: "200px"
        }, {
            template: "&nbsp;"
        }]
    });
</script>

答案 1 :(得分:0)

我已将其发布在telerik论坛中,并得到了管理员的答复,这是他们建议解决问题的方法。在这里发布,以便其他人可以从中受益。

“ Drew B.”提出的答案也有效,我在另一篇文章中也看到了。我发布的代码使用最少的代码就不那么麻烦了。

columnResize: function (e) {

                    // what is thead and tbody: https://docs.telerik.com/kendo-ui/api/javascript/ui/grid#fields
                    var grid = e.sender,
                        gridHeaderTable = grid.thead.parent(),
                        gridBodyTable = grid.tbody.parent();

                    // what is wrapper: https://docs.telerik.com/kendo-ui/api/javascript/ui/widget/fields/wrapper
                    // what is scrollbar(): https://docs.telerik.com/kendo-ui/api/javascript/kendo/fields/support
                    if (gridBodyTable.width() < grid.wrapper.width() - kendo.support.scrollbar()) {

                      // remove the width style from the last VISIBLE column's col element
                      gridHeaderTable.find("> colgroup > col").last().width("");
                      gridBodyTable.find("> colgroup > col").last().width("");

                      // remove the width property from the last VISIBLE column's object
                      // https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/fields/columns
                      grid.columns[grid.columns.length - 1].width = "";

                      // remove the Grid tables' pixel width
                      gridHeaderTable.width("");
                      gridBodyTable.width("");
                    }

                  },