0
votes

I have two classes:

  1. DealCashFlowBC
  2. CusipCashFlowBC

A deal can have information of multiple cusips that's why we have a list of type CusipCashflowBC in DealCashFlowBC.

Now the problem is:

  1. I need to bind these columns dynamically in kendo grid
  2. In my case, I did data binding but column headers are not there.

Here is the code:

public class DealCashFlowBC
{
    public string DealName { get; set; }
    public int Period { get; set; }
    public DateTime Date { get; set; }
    public int NetInterest { get; set; }
    public int PeriodicASER { get; set; }
    public int AdjustedNetInterest { get; set; }
    public int TotalPrincipal { get; set; }
    public int Balance { get; set; }
    public int PrincipalLoss { get; set; }
    public List<CusipCashFlowBC> CCFBC { get; set; }
}

public class CusipCashFlowBC
{
    public string Cusip { get; set; }
    public int Period { get; set; }
    public string ClassName { get; set; }
    public double? Interest { get; set; }
    public double Principal { get; set; }
    public double Loss { get; set; }
    public double EndBal { get; set; }
    public double Penalty { get; set; }
    public double AccumulatedShortfall { get; set; }
}

Grid Code:

.Columns(columns =>
{
    columns.Bound(e => e.Period).Title("Period").Width(100);
    columns.Bound(e => e.Date).Title("Settlement Date").Width(100);
    columns.Bound(e => e.NetInterest).Title("Net Interest @ original terms").Width(80);
    columns.Bound(e => e.PeriodicASER).Title("Periodic ASER").Width(100);
    columns.Bound(e => e.AdjustedNetInterest).Title("Adjusted Net Interest").Width(100);
    columns.Bound(e => e.TotalPrincipal).Title("Total Principal").Width(100);
    columns.Bound(e => e.Balance).Title("Balance").Width(100);
    columns.Bound(e => e.PrincipalLoss).Title("Principal Loss").Width(100);
    columns.Template(e => { }).ClientTemplate("#=iterate(CCFBC)#").Title("CCFBC");

Javascript:

function iterate(ReportList) {
    var template = "";
    for (var i = 0; i < ReportList.length; i++) {
        template = template + "<td role='gridcell'>" + ReportList[i].Cusip + "</td><td role='gridcell'>" + ReportList[i].Period + "</td><td role='gridcell'>" + ReportList[i].ClassName + "</td><td role='gridcell'>" + ReportList[i].Principal + "</td>";
    }
    return template;

}

Problem: Not able to get headers for dynamic columns

I want header for those dynamic column, generating from client Template iteration.

1

1 Answers

0
votes

In order to get dynamic columns I suggest you use an alternate approach and not client templates, because you will need to change the header row HTML which may lead to errors in sorting and filtering.

<div id="grid"></div>

            <script>

             var products = [{
                                ProductID : 1,
                                ProductName : "Chai",
                                SupplierID : 1,
                                CategoryID : 1,
                                QuantityPerUnit : "10 boxes x 20 bags",
                                UnitPrice : 18.0000,
                                UnitsInStock : 39,
                                UnitsOnOrder : 0,
                                ReorderLevel : 10,
                                Discontinued : false,
                                Category : [{
                                    CategoryID : 1,
                                    CategoryName : "Beverages",
                                    Description : "Soft drinks, coffees, teas, beers, and ales"
                                },{
                                    CategoryID : 2,
                                    CategoryName : "Condiments",
                                    Description : "Sweet and savory sauces, relishes, spreads, and seasonings"
                                }]
                            }, 
                            {
                                ProductID : 2,
                                ProductName : "Chang",
                                SupplierID : 1,
                                CategoryID : 1,
                                QuantityPerUnit : "24 - 12 oz bottles",
                                UnitPrice : 19.0000,
                                UnitsInStock : 17,
                                UnitsOnOrder : 40,
                                ReorderLevel : 25,
                                Discontinued : false,
                                Category : [{
                                    CategoryID : 1,
                                    CategoryName : "Beverages",
                                    Description : "Soft drinks, coffees, teas, beers, and ales"
                                },
                                {
                                    CategoryID : 3,
                                    CategoryName : "Confections",
                                    Description : "Desserts, candies, and sweet breads"
                                }]
                            }]

                $(document).ready(function() {
                    $("#grid").kendoGrid({
                        dataSource: {
                            data: products,
                            schema: {
                                model: {
                                    fields: {
                                        ProductName: { type: "string" },
                                        UnitPrice: { type: "number" },
                                        UnitsInStock: { type: "number" },
                                        Discontinued: { type: "boolean" }
                                    }
                                }
                            },
                            pageSize: 20
                        },
                        height: 550,                        
                        pageable: {
                            input: true,
                            numeric: false
                        },
                        columns: [
                            "ProductName",
                            { field: "UnitPrice", title: "Unit Price", format: "{0:c}", width: "130px", sortable: true, filterable: true },
                            { field: "UnitsInStock", title: "Units In Stock", width: "130px" , sortable: true, filterable: true},
                            { field: "Discontinued", width: "130px" , sortable: true, filterable: true},
                            { field: "Category", sortable: false, filterable: false,
                            template: function iterate(dataItem) 
                                     {
                                            var template = "";
                                            for (var i = 0; i < dataItem.Category.length; i++) {
                                                template = template + "<td role='gridcell'>" + dataItem.Category[i].CategoryName 
                                                  + "</td><td role='gridcell'>" 
                                                  + dataItem.Category[i].Description + "</td>";
                                            }
                                            return template;

                                        }
                            }],
                    dataBound:function(e)
                      {
                        $("#grid thead tr").append('<th scope="col" role="columnheader" data-field="Category" aria-haspopup="true" class="k-header">Category Desc</th><th scope="col" role="columnheader" data-field="Category" aria-haspopup="true" class="k-header">Category</th><th scope="col" role="columnheader" data-field="Category Desc" aria-haspopup="true" class="k-header">Category Desc</th>');
                      }
                    });
                });              
            </script>

Alternatives could be:-