【问题标题】:Kendo grid - adding column from nested dictionary剑道网格 - 从嵌套字典中添加列
【发布时间】:2015-06-05 00:40:44
【问题描述】:
    [Serializable]
    public class ContactDto
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string CompanyName { get; set; }
        public Dictionary<string, string> CustomFields { get; set; }
    }

我有上面的contactdto 类和自定义字段字典。这些可以是任何“母亲姓名”“生日”等。

如何在剑道网格中将自定义字段显示为列?所有contactDTO 都将具有相同的字典键。我正在使用 ASP.net MVC 助手。

我尝试过类似的方法 - 不起作用 -

@(Html.Kendo().Grid<ContactDto>(Model)
.Name("ReportViewer-Data")
.Columns(columns =>
{
    columns.Bound("FirstName");
    columns.Bound("LastName");
    columns.Bound("CustomFields.Industry");
})
.Sortable()
.Pageable(pageable => pageable.PageSizes(true).ButtonCount(5))
.Groupable()
.Selectable(selectable => selectable.Mode(GridSelectionMode.Multiple))
.Resizable(resize => resize.Columns(true))
.Reorderable(reorder => reorder.Columns(true))
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.Read(read => read.Action("DataView_Test", "Reports"))
)
)

【问题讨论】:

  • 是否要将字典显示为与其他属性相同的网格中的列?还是在第一个网格内的嵌套网格中?你能不能给一些虚拟的json作为例子
  • 我想要像 FirstName LastName BirthDate MothersName 这样的列 - 最后两列来自字典

标签: jquery asp.net asp.net-mvc kendo-ui kendo-grid


【解决方案1】:

假设来自后端的 json 数据与此类似

data: [{
    FirstName: "John",
    LastName: "Doe",
    CustomField: {
        BirthDate: "9 June 1999",
        MotherName: "Valen"
    }
}, {
    FirstName: "Ray",
    LastName: "Doe",
    CustomField: {
        BirthDate: "9 June 1998",
        MotherName: "Valen"
    }
}, {
    FirstName: "Grey",
    LastName: "Doe",
    CustomField: {
        BirthDate: "9 June 1997",
        MotherName: "Valen"
    }
}]

由于 kendo 数据源目前不支持复杂数据类型 比如数组和对象。

解决方法是像这样将 schema 添加到您的数据源中

schema: {
    parse: function (response) {
        var items = [];
        for (var i = 0; i < response.length; i++) {
          console.log(response);
            var item = {
                FirstName: response[i].FirstName,
                LastName: response[i].LastName,
                BirthDate: response[i].CustomField.BirthDate,
                Mother: response[i].CustomField.MotherName
            };

            items.push(item);
        }
        return items;
    },
    pageSize: 20
},

获取数据后,dataSource 将解析嵌套到普通字段的 customField。请参阅此工作example

【讨论】:

  • 感谢您的详细回答——这真的很有帮助——你知道这将如何转化为 HTMLHelper MVC 包装器吗?如果我找不到使用 MVC UI 包装器做同样事情的正确方法,我将使用这个纯 JS 解决方案
  • 让我挖掘一下,因为我还没用过。如果我设法找到一个,我会更新这个
  • 谢谢 - 将此标记为答案,因为这非常接近。
  • @MoXplod 我读到了这个article,上面写着“通过 MVC 包装器不支持显式设置模式”,但它也说“数据源模式将从指定的模型类自动生成”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多