Hide Column Based On Remote Data

2 posts, 0 answers
  1. Graham
    Graham avatar
    5 posts
    Member since:
    Jul 2010

    Posted 16 Oct 2015 Link to this post

    I have a kendo grid that gets data remotely (because I use the export to excel functionality). The column headers are bound to my model which is passed as empty before the data source request gets the populated one.

        @model ReportingPortalWeb.Models.HireReportViewModel

                      .Columns(columns =>
                          columns.Bound(p => p.SohQty).Title("Quantity");
                          columns.Bound(p => p.StockName);
                          columns.Bound(p => p.SiteAddress3);
                          columns.Bound(p => p.PostCode);
                      .ToolBar(tools => tools.Excel())
                      .Excel(excel => excel
                          .FileName("Kendo UI Grid Export.xlsx")
                          .ProxyURL(Url.Action("CustomReport_Data", "Reports"))
                      .DataSource(dataSource => dataSource
                                            .Read(read => read.Action("CustomReport_Data", "Reports").Data("GetData")))
                      .HtmlAttributes(new {style = "height:550px;"})

    Before having to use the excel export I passed the data when the gird was created and I could hide a column easily by doing something like 

         columns.Bound(p => p.PrCode).Visible(Model.StockReport.Count(t => t.PrCode.Any()) > 0);


    now that I am remotely fetching the data instead, is there a data on load event that I can use to hide the columns in my report that don't have any data in them?



  2. Radoslav
    Radoslav avatar
    1566 posts

    Posted 20 Oct 2015 Link to this post

    Hello Graham,

    To achieve the desired functionality you can handle the client side dataBound event and check the column data, if the column’s data is empty call grid’s hideColumn method which will hide the column. For example:
    dataBound: function(e) {
        var grid = $("#grid").data("kendoGrid");
        var currentView = grid.dataSource.view();
        var shouldBeHidden = true;
        for(var i= 0, i< currentView.lenght; i++)
              if(currentView[i].FieldName != '')
                     shouldBeHidden = false;

    I hope this helps.

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top