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

Set datasource doesn't create aggregates if initial bind had no data

1 Answer 258 Views
Grid
This is a migrated thread and some comments may be shown as answers.
IT Dept
Top achievements
Rank 1
IT Dept asked on 15 Mar 2013, 09:42 AM
Hi,

I've got an issue when rebinding the datagrid and aggregates.

The following javascript creates the grid. Columns of the reports are unkown, the data I'm getting contains an array with the column list, and an array with the actual data.

I found this problem when executing a report that has left joins, so if there is no data, I still get rows, but no values to the columns I want to aggregate.

If the first time the report is generated has no data, the aggregates are not created the second time round, even if I do have data.

But if the first time the data loads I have data the aggregates are shown. Then if the second time I don't have data, I receive the following error "Uncaught TypeError: undefined has no properties" at "grid.setDataSource(newDs);"

var reports = {
    getGridColumns: function (columns) {
        var columnArray = [];
 
        $.each(columns, function (i, c) {
            var column = { field: c.Name, title: c.Title, width: 150 };
            if (c.Format) {
                column.format = '{0:' + c.Format + '}';
            }
            if (c.ShowTotals) {
                column.footerTemplate = "#=kendo.toString(sum,'" + c.Format + "')#";
            }
            columnArray.push(column);
        });
        return columnArray;
    },
    getGridAggregates: function (columns) {
        var columnArray = [];
 
        $.each(columns, function (i, c) {
 
            if (c.ShowTotals) {
                columnArray.push({ field: c.Name, aggregate: "sum" });
            }
 
        });
        if (columnArray.length == 0) { columnArray = undefined; }
        return columnArray;
    },
    bindKendoGrid: function () {
        $.ajax({
            type: 'POST',
            url: '/Reports/GetReportData',
            data: reports.getReportParams(),
            success: function (data) {
                if (data && data.Data && data.Data.length > 0) {
                    if (!$('#report-grid').data('kendoGrid')) {
                        $('#report-grid').kendoGrid({
                            dataSource: {
                                data: eval(data.Data),
                                pageSize: 15,
                                aggregate: reports.getGridAggregates(data.Columns)
                            },
                            columns: reports.getGridColumns(data.Columns),
                            scrollable: true,
                            sortable: true,
                            filterable: true,
                            pageable: {
                                input: true,
                                numeric: false
                            }
                        });
                    }
                    else {
                        var grid = $('#report-grid').data('kendoGrid');
 
                        var newDs = new kendo.data.DataSource({
                            data: eval(data.Data),
                            pageSize: 15,
                            aggregate: reports.getGridAggregates(data.Columns)
                        });
                        grid.setDataSource(newDs);
                    }
                    $('#report-grid').css('display', 'block');
 
                    $('#report-container #report-export').show();
                }
                else {
                    $('#no-data').css('display', 'block');
                }
                $('#report-container').css({ position: '', width: '', height: '' });
                kendo.ui.progress($("#report-container"), false)
                $('#btnReportRefresh').removeAttr('disabled').removeClass('k-state-disabled');
            },
            error: function (data) {
                kendo.ui.progress($("#report-container"), false)
                $('#btnReportRefresh').removeAttr('disabled').removeClass('k-state-disabled');
                $('#no-data').css('display', 'block');
                kendo.ui.progress($("#commissions-dashboard"), false);
            }
        });
    }
}

1 Answer, 1 is accepted

Sort by
0
Alexander Valchev
Telerik team
answered on 19 Mar 2013, 01:30 PM
Hello Massimiliano,

I am afraid that the provided information is not sufficient enough in order to determine what is going wrong.
I tried to reproduce the behaviour in a sample page, but to no avail. Could you please check my test page and let me know what I am missing?

Kind regards,
Alexander Valchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
IT Dept
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
Share this question
or