带有隐藏列和聚合的Kendo UI网格

时间:2015-12-09 19:07:55

标签: jquery kendo-ui telerik kendo-grid telerik-grid

如果我创建一个隐藏列,在这种情况下是BirthDateMonth并从数据集创建它,如果我还在另一个字段上添加一个组聚合,那么它将破坏JS错误" 总和未定义"

var grid = $("#grid").kendoGrid({
    dataSource: {
        data: createRandomData(10),
        schema: {
            model: {
                fields: {
                    FirstName: { type: "string" },
                    LastName: { type: "string" },
                    City: { type: "string" },
                    Title: { type: "string" },
                    BirthDate: { type: "date" },
                    //BirthDateMonth: { type: "date" },
                    Age: { type: "number" }
                },
            },
            parse: function (data) {
                $.each(data, function (idx, item) {
                    if (item.BirthDate)
                    {
                        item.BirthDateMonth = new Date(item.BirthDate.getTime());
                        item.BirthDateMonth.setDate(1);  
                    }                        
                });
                return data;
            }                
        },
        pageSize: 10,
        aggregate: [        
                {field: "Age", aggregate: "sum"}
        ]
    },
    height: 500,
    scrollable: true,
    groupable: true,
    columns: [
        {
            field: "FirstName",
            title: "First Name"
        },
        {
            field: "LastName",
            title: "Last Name"
        },
        {
            field: "City",
        },
        {
            field: "Title"
        },
        {
            field: "BirthDate",
            title: "Birth Date",
            template: '#= kendo.toString(BirthDate,"MM/dd/yyyy") #'
        },
        {
            field: "BirthDateMonth",
            title: "Birth Month",
            template: '#= kendo.toString(BirthDateMonth,"MM/yyyy") #',
            hidden: true
        },
        {
            field: "Age",
            aggregates: ["sum"],
            footerTemplate: "Sum: #=sum#",
            groupFooterTemplate: "Sum: #=sum#"               

        }
    ]
}).data("kendoGrid");

grid.dataSource.group([{field: "BirthDateMonth"}]);

JSFiddle,任何想法都表示赞赏。我尝试将隐藏列字段添加到架构中,但没有效果。

2 个答案:

答案 0 :(得分:1)

来自Jayesh的解决方案是正确的,谢谢。

你认为这里有什么值得报道的,或者这是预期的行为?

我发现的另一点是,如果我添加:

groupHeaderTemplate: "Birth Month: #= value # (Count: #= count#)" 

到专栏:

{
  field: "BirthDateMonth",
  title: "Birth Month",
  template: '#= kendo.toString(BirthDateMonth,"MM/yyyy") #',
  hidden: true
},

要获取组计数,那么组函数需要在同一个字段中包含计数聚合:

grid.dataSource.group({
            field: "BirthDateMonth",
            aggregates: [
                { field: "Age", aggregate: "sum" },
                { field: "BirthDateMonth", aggregate: "count" }
            ]
        })

答案 1 :(得分:0)

请尝试使用以下代码段。

<!DOCTYPE html>
<html>
<head>
    <title>Jayesh Goyani</title>
    <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2015.2.902/styles/kendo.common-bootstrap.min.css" />
    <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2015.2.902/styles/kendo.bootstrap.min.css" />
    <script src="https://kendo.cdn.telerik.com/2015.2.902/js/jquery.min.js"></script>
    <script src="https://kendo.cdn.telerik.com/2015.2.902/js/kendo.all.min.js"></script>
</head>
<body>
    <div id="grid"></div>
    <script>
        var firstNames = ["Nancy", "Andrew", "Janet", "Margaret", "Steven", "Michael", "Robert", "Laura", "Anne", "Nige"],
    lastNames = ["Davolio", "Fuller", "Leverling", "Peacock", "Buchanan", "Suyama", "King", "Callahan", "Dodsworth", "White"],
    cities = ["Seattle", "Tacoma", "Kirkland", "Redmond", "London", "Philadelphia", "New York", "Seattle", "London", "Boston"],
    titles = ["Accountant", "Vice President, Sales", "Sales Representative", "Technical Support", "Sales Manager", "Web Designer",
    "Software Developer", "Inside Sales Coordinator", "Chief Techical Officer", "Chief Execute Officer"],
    birthDates = [new Date("1948/12/08"), new Date("1952/02/19"), new Date("1963/08/30"), new Date("1937/09/19"), new Date("1955/03/04"), new Date("1963/07/02"), new Date("1960/05/29"), new Date("1958/01/09"), new Date("1966/01/27"), new Date("1966/03/27")];

        function createRandomData(count) {
            var data = [],
                now = new Date();
            for (var i = 0; i < count; i++) {
                var firstName = firstNames[Math.floor(Math.random() * firstNames.length)],
                    lastName = lastNames[Math.floor(Math.random() * lastNames.length)],
                    city = cities[Math.floor(Math.random() * cities.length)],
                    title = titles[Math.floor(Math.random() * titles.length)],
                    birthDate = birthDates[Math.floor(Math.random() * birthDates.length)],
                    age = now.getFullYear() - birthDate.getFullYear();

                data.push({
                    Id: i + 1,
                    FirstName: firstName,
                    LastName: lastName,
                    City: city,
                    Title: title,
                    BirthDate: birthDate,
                    Age: age
                });
            }
            return data;
        }

        $(document).ready(function () {
            var grid = $("#grid").kendoGrid({
                dataSource: {
                    data: createRandomData(10),
                    schema: {
                        model: {
                            fields: {
                                FirstName: { type: "string" },
                                LastName: { type: "string" },
                                City: { type: "string" },
                                Title: { type: "string" },
                                BirthDate: { type: "date" },
                                //BirthDateMonth: { type: "date" },
                                Age: { type: "number" }
                            },
                        },
                        parse: function (data) {
                            $.each(data, function (idx, item) {
                                if (item.BirthDate) {
                                    item.BirthDateMonth = new Date(item.BirthDate.getTime());
                                    item.BirthDateMonth.setDate(1);
                                }
                            });
                            return data;
                        }
                    },
                    pageSize: 10,
                    aggregate: [
                            { field: "Age", aggregate: "sum" },
                            { field: "BirthDateMonth", aggregate: "sum" }
                    ]
                },
                height: 500,
                scrollable: true,
                groupable: true,
                columns: [
                    {
                        field: "FirstName",
                        title: "First Name"
                    },
                    {
                        field: "LastName",
                        title: "Last Name"
                    },
                    {
                        field: "City",
                    },
                    {
                        field: "Title"
                    },
                    {
                        field: "BirthDate",
                        title: "Birth Date",
                        template: '#= kendo.toString(BirthDate,"MM/dd/yyyy") #'
                    },
                    {
                        field: "BirthDateMonth",
                        title: "Birth Month",
                        template: '#= kendo.toString(BirthDateMonth,"MM/yyyy") #',
                        hidden: true
                    },
                    {
                        field: "Age",
                        aggregates: ["sum"],
                        footerTemplate: "Sum: #=sum#",
                        groupFooterTemplate: "Sum: #=sum#"

                    }
                ]
            }).data("kendoGrid");
            grid.dataSource.group({
                field: "BirthDateMonth",
                aggregates: [
                    { field: "Age", aggregate: "sum" }]
            })
        });
    </script>
</body>
</html>

如果有任何疑虑,请告诉我。