Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Kendo UI grid column widths programmatically

I would like to set Kendo UI grid column widths programmatically. I am using the following code:

function setColumnWidths(grid, options) {
    for (var i = 0; i < options.columns.length; i++) {
        grid.columns[i].width = options.columns[i].width;
    }
}

When debugging in chrome after the statements executed, grid.columns[i].width seems to be appropriately set to the new value, however nothing changes in the GUI, the column widths remain the same. What am I missing?

like image 642
g.pickardou Avatar asked May 10 '15 10:05

g.pickardou


People also ask

How do I set the width on a kendo grid?

To control the width of the Grid, set the CSS width properties to the Grid itself or to some of its ancestors. If you use hierarchy and unless the detail template is scrollable, the detail template has to be narrower than the sum of the widths of all master columns.

How do I make my Kendo grid responsive?

The responsive features of the Kendo UI Grid for Angular are: Responsive columns—Based on the viewport width, the visibility of the Grid columns toggles. Responsive height—Based on the height setting (for example, "100%" ), the Grid adjusts its size depending on the height of its container.

What is pageSize in kendo grid?

pageSize Number The number of data items per page. The property has no default value.


2 Answers

I've ended with this. Dion's solution gave me starting idea about using colgroups, however that solution is limited to not having locked columns, what are in different colgroups.

Also note: I do not want to use grid.setOptions, because its limitations, ruining attached events and header (in case of using ASP MVC helper to render the grid)

function setColumnWidths(grid, options) {
    var lockedCount = 0;
    for (var i = 0; i < options.columns.length; i++) {
        if (options.columns[i].hasOwnProperty('locked')) {
            if (options.columns[i].locked) {
                lockedCount++;
            }
        }
    }

    for (var i = 0; i < options.columns.length; i++) {
        var width = options.columns[i].width;
        grid.columns[i].width = width;
        if (options.columns[i].hasOwnProperty('locked') && options.columns[i].locked) {
            $("#grid .k-grid-header-locked").find("colgroup col").eq(i).width(width);
            $("#grid .k-grid-content-locked").find("colgroup col").eq(i).width(width);

        } else {
            $("#grid .k-grid-header-wrap").find("colgroup col").eq(i-lockedCount).width(width);
            $("#grid .k-grid-content").find("colgroup col").eq(i - lockedCount).width(width);
        }
    }
    // Hack to refresh grid visual state
    grid.reorderColumn(1, grid.columns[0]);
    grid.reorderColumn(1, grid.columns[0]);
}
like image 54
g.pickardou Avatar answered Sep 28 '22 04:09

g.pickardou


You need to change grid's width through its element instead of its definition. Kendo grid contains header and content, so you need to change two elements.

Use this code instead

$("#grid-id .k-grid-header-wrap").find("colgroup col").eq(xx).width(yy);
$("#grid-id .k-grid-content").find("colgroup col").eq(xx).width(yy);

Sample

like image 22
Dion Dirza Avatar answered Sep 28 '22 02:09

Dion Dirza