Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change columns set of kendo grid dynamically

I am trying to change the columns collection of my Kendo grid in the below way.

var grid = $("#grid").data("kendoGrid");
$http.get('/api/GetGridColumns')
    .success(function (data) {
        grid.columns = data;                    
    })
    .error(function (data) {
        console.log(data);
    });

This is changing the column collection but not reflecting immediately in my grid. But when I try to perform some actions in the grid (like grouping), then my new column set is appearing.

Please let me know how can I achieve this.

Regards, Dilip Kumar

like image 439
user2503696 Avatar asked Jun 20 '13 04:06

user2503696


People also ask

How do I change Kendo grid column title dynamically?

Solution. The Kendo UI Grid with multi-header columns creates them with a data-title attribute equal to the current title of the column. To identify the column that needs to be updated we can use the tilde selector which does a contains search through the thead of the grid. Create a custom function updateColumnTitle .

How do I hide Kendo grid column dynamically?

You showing/hiding columns in KendoUI Grid you should use showColumn and hideColumn and use as argument a number (the index of the column that you want to show/hide) or a string (the name of the field associated in that column). Example: var grid = $("#grid").

How do I change the date format in kendo grid column?

{ field: "DOR", title: "Release Date", format: "{0:MM/dd/yyyy}", }, { field: "rating", title: "Rating" }, { command: ["edit", "destroy"], title: " ", width: "250px" }


1 Answers

You can do it by setting the KendoUI datasource, destroy the grid, and rebuild it

$("#load").click(function () {
        var grid = $("#grid").data("kendoGrid");

    var dataSource = grid.dataSource;

    $.ajax({
        url: "/Home/Load",
        success: function (state) {
            state = JSON.parse(state);

            var options = grid.options;

            options.columns = state.columns;

            options.dataSource.page = state.page;
            options.dataSource.pageSize = state.pageSize;
            options.dataSource.sort = state.sort;
            options.dataSource.filter = state.filter;
            options.dataSource.group = state.group;

            grid.destroy();

            $("#grid")
               .empty()
               .kendoGrid(options);
        }
    });
});

here you can just do this :

var options = grid.options;

options.columns = state.columns;

where you can retrieve the columns in a session or in a db

like image 186
Edu Cielo Avatar answered Sep 19 '22 11:09

Edu Cielo