This is a migrated thread and some comments may be shown as answers.

Grouping stops working if you recreate the grid

3 Answers 240 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Iron
Iron
Andrew asked on 13 Feb 2012, 06:04 AM
I have code which creates a grid.

When the user pressed the "Refresh" button, my code then destroys this grid and recreates it again.

For some reason, when you recreate the grid, the grouping doesn't work. You drag a column into the grouping banner, but then it just doesn't stick there.

I have created a simple working example here: http://jsfiddle.net/xmpzk/1/ 

Press the button "Create", and you see the grid appear. You can happily drag column headers into the grouping banner.

Then press the button "Refresh". This time, you will find that you cannot drag columns.

Just in case jsFiddle is down:

The HTML:
<button type="button" id="refreshButton">Create Grid</button>  

<div id="demoGrid">
    
</div>


The Javascript:

function createGrid({

    var dataSource {
        data[
            {
            FirstName"Harold",
            LastName"Kumar",
            City"London",
            Title"Mr",
            Age63}
              ]
    };
    $("#demoGrid").empty().kendoGrid({
        height250,
        scrollabletrue,
        sortabletrue,
        filterabletrue,
        pageabletrue,
        groupabletrue,
        dataSourcedataSource,
        columns[
            {
            field"FirstName",
            title"First Name"},
        {
            field"LastName",
            title"Last Name"},
        {
            field"City"},
        {
            field"Title"},
        {
            field"Age"}
        ]
    });
    $("#refreshButton").text("Refresh Grid");
};

// createGrid();

$("#refreshButton").click(function({
    createGrid();
});​ 










3 Answers, 1 is accepted

Sort by
0
Cuinn
Top achievements
Rank 1
answered on 13 Feb 2012, 07:03 AM
Hi Andrew,

Try this.  http://jsfiddle.net/CuinnWylie/xmpzk/2/ 

You're emptying the div, but not clearing the data stored on the div itself.  This way you wrap the div and re-create the grid div each time.  This clears everything attached to it.  Not the most elegant possibly, but it'll work every time.

Also, if you have filterable on then you'll leak resources as the filter elements are re-added each time as well.  Probably some way to remove those properly, can't look into it for you right now though.

Regards,
Cuinn. 
0
Cuinn
Top achievements
Rank 1
answered on 14 Feb 2012, 03:44 AM
Andrew,

Once again I don't know if this is the best way to do it, but this will clear the DOM.  As from http://jsfiddle.net/CuinnWylie/xmpzk/7/ 
kg = $('#demoGrid').data('kendoGrid');
if (kg !== undefined) {
    $('#demoGrid').data('kendoGrid').thead.find('.k-filterable').each(function(i, HeadEl) {
        var HEForm = $(HeadEl).data('kendoFilterMenu').form;
        HEForm.find('select').each(function(i, el) {
            var SelectList = $(el).data('kendoDropDownList');
            $(SelectList.list[0]).remove();
        })
        $(HEForm).remove();
    });
}

Let me know how you go.

Regards,
Cuinn.
0
Andrew
Top achievements
Rank 1
Iron
Iron
answered on 14 Feb 2012, 07:28 AM

I found the best thing to do was change my approach.

I didn't need to recreate the grid, just update the data. This is a better approach, because the grid then retains the groupings and sorting it had before the update.

For example:

var kendoGrid = $("#kendoGridPlayersInError").data("kendoGrid");
var kendoGridDataSource = kendoGrid.dataSource;
kendoGridDataSource.data(newValues);


 

 

 

 

 

 

 

 

 


Tags
Grid
Asked by
Andrew
Top achievements
Rank 1
Iron
Iron
Answers by
Cuinn
Top achievements
Rank 1
Andrew
Top achievements
Rank 1
Iron
Iron
Share this question
or