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

Excel export settings don't work after setOptions()

1 Answer 260 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Paweł
Top achievements
Rank 1
Paweł asked on 05 Mar 2015, 10:52 AM
Hi

I'm using 2014.3.1119.440 version of Kendo MVC UI.
I configured the grid to export all data and a custom filename, and a custom toolbar with export to excel button.

It works fine, when I don't restore grid options using setOptions function.
When I restore it, with my custom toolbar preserve, it looks, like the export options are ignored.
There are only records from current page exported and exported file has a standard name.

I found the settings are correct in the page source.

Do I need some extra steps to make the settings working after restoring grid options?

Here's my razor grid code:

 
@{ Html.Kendo().Grid<Vectra.CPN.CPNLibrary.ViewModel.TRANSFER_IN_V>()
        .Name("MainGrid")
        .ToolBar(toolBar => toolBar.Template(@<text>
                <input class="t-button" type="button" id="ShowCreateNewExtFormId" name="ShowCreateNewExtForm" value="@Resources.Create" onclick="location.assign('@Url.Content("~/" + ControllerName + "/New")')"/>
                <input class="t-button" type="button" id="ShowEditFormId" name="ShowEditForm" value="@Resources.Edit" />
                <input class="t-button" type="button" id="ShowDetailFormId" name="ShowDetailForm" value="@Resources.Details" />
                   
                <a class="k-button k-button-icontext k-grid-excel" href="#"><span class="k-icon k-i-excel"></span>Zapisz do Excela</a>
                   
                <a href="" class="k-button" id="save">Save View</a>
                <a href="" class="k-button" id="load">Restore View</a>
                </text>)
            )
        .Excel(ex => ex.AllPages(true).FileName("cpn_do_vectry_" + DateTime.Now.ToShortDateString() + ".xlsx"))
        .ColumnMenu(column => column.Columns(true))
        .Columns(columns => columns.LoadSettings((IEnumerable<GridColumnSettings>)ViewBag.GridColumns))
        .Reorderable(reorder => reorder.Columns(true))
        .Sortable(sortable => sortable.Enabled(true).SortMode(GridSortMode.SingleColumn).AllowUnsort(true))
        .Pageable(pageable => pageable.Enabled(true).Refresh(true).ButtonCount(5).PageSizes(true).PageSizes(new int[] { 5, 10, 20, 50 }))
        .Filterable(filtering => filtering.Enabled(true))
        .Selectable(s => s.Mode(Kendo.Mvc.UI.GridSelectionMode.Single).Type(GridSelectionType.Row))
        .Events(events => events.Change("onChange"))
        .DataSource(dataSource => dataSource
            .Ajax()
            .Read(read => read.Action("TransferIn_Read", "TransferIn"))
            .Model(model => model.Id(o => o.TRANSFER_ID))
            .ServerOperation(false)
            )
            .AutoBind(false)
        .Render()
        ;
}

and here's the part of my javascript for saving and restoring grid options:

var localStorageKey = "MainTransferInOptions";
var areOptionsLoaded = false;
 
function bindSaveRestoreCliks()
{
    var grid = $("#MainGrid").data("kendoGrid");
 
    $("#save").click(function (e) {
        e.preventDefault();
        localStorage[localStorageKey] = kendo.stringify(grid.getOptions());
    });
 
    $("#load").click(function (e) {
        e.preventDefault();
        loadGridOptions(e);
    });
}
 
function loadGridOptions(e)
{
    if (e == undefined || e.type == "click" || (!areOptionsLoaded && e.type == "read"))
    {
        var rawGrid = $("#MainGrid");
         
        var grid = $("#MainGrid").data("kendoGrid");
        var options = localStorage[localStorageKey];
        var toolbar = $("#MainGrid").find(".k-grid-toolbar").html();
 
        if (options) {
            grid.setOptions(JSON.parse(options));
        }
        else if (!areOptionsLoaded && e == undefined)
        {
            grid.dataSource.read();
        }
 
        var newtoolbar = $("#MainGrid").find(".k-grid-toolbar");
        $("#MainGrid").find(".k-grid-toolbar").html(toolbar);
        bindSaveRestoreCliks();
        areOptionsLoaded = true;
    }
 }


I restore the settings on page load:

$(window).ready(function (e) {
        loadGridOptions(undefined);
}
);

Thanks in advance.

Paweł

1 Answer, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 07 Mar 2015, 08:35 AM
Hello Pawel,

I posted a reply in the other thread that you opened on the same subject. For reference I will paste it here too:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

When the MVC wrappers are used and there is toolbar template (which is a server-side template and it might contain server logic) the setOptions will wipe the toolbar settings and we cannot re-execute the logic. What we provided as a work-around is shared here:


https://github.com/telerik/ui-for-aspnet-mvc-examples/tree/master/grid/grid-preserve-server-toolbar-template-after-set-options

Please give a try and let us know your findings.

Kind Regards,
Petur Subev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Paweł
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Share this question
or