通过可枚举列表生成动态列-Kendo Grid

时间:2019-04-03 17:43:10

标签: c# kendo-ui kendo-grid kendo-asp.net-mvc

我有两节课:

  1. DealCashFlowBC
  2. CusipCashFlowBC

一笔交易可以包含多个客户的信息,这就是为什么我们在DealCashFlowBC中拥有类型CusipCashflowBC的原因。

现在的问题是:

  1. 我需要在剑道网格中动态绑定这些列
  2. 就我而言,我进行了数据绑定,但没有列标题。

代码如下:

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; }
}

网格代码:

.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;

}

问题:无法获取动态列的标题

我想要这些动态列的标题,从 客户端模板迭代。

1 个答案:

答案 0 :(得分:0)

为了获取动态列,我建议您使用替代方法而不是客户端模板,因为您将需要更改标题行HTML,这可能会导致排序和过滤错误。

<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>

替代方案可以是:-