ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript-新添加的数据未正确保存在kendo网格中

2019-10-31 12:36:36  阅读:185  来源: 互联网

标签:kendo-ui kendo-dataviz javascript jquery


完成以下操作时,网格行为不符合预期.

>添加新记录
>然后按网格中的更新按钮(新添加的行)
>然后在保存之前按取消.

完成上述操作后,新添加的行将消失.
link to js fiddle

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Demo Grid App</title>
    <!-- CDN-based stylesheet reference for Kendo UI DataViz -->
    <link rel="stylesheet" href="http://cdn.kendostatic.com/2012.2.710/styles/kendo.default.min.css">
     <link rel="stylesheet" href="http://cdn.kendostatic.com/2012.2.710/styles/kendo.common.min.css">
            <!-- CDN-based script reference for jQuery; utilizing a local reference if offline -->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
       <!-- CDN-based script reference for Kendo UI DataViz; utilizing a local reference if offline -->
    <script src="http://cdn.kendostatic.com/2012.2.710/js/kendo.all.min.js"></script>

</head>
<body>
    <header>
        <h1>Demo Grid App</h1>
    </header>

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

function generatePeople(itemCount, callback) {
    var data = [],
        delay = 25,
        interval = 500,
        starttime;

    var now = new Date();
    setTimeout(function() {
        starttime = +new Date();
        do {
            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: data.length + 1,
                FirstName: firstName,
                LastName: lastName,
                City: city,
                Title: title,
                BirthDate: birthDate,
                Age: age
            });
        } while(data.length < itemCount && +new Date() - starttime < interval);

        if (data.length < itemCount) {
            setTimeout(arguments.callee, delay);
        } else {
            callback(data);
        }
    }, delay);
}
                $(document).ready(function() {
                    $("#grid").kendoGrid({

                        dataSource: {
                            data: createRandomData(10),


                            schema: {
                                model: {
                                    id:"FirstName",
                                    fields: {
                                        LastName: { type: "string" },
                                        City: { type: "string" },
                                        Title: { type: "string" },
                                        BirthDate: { type: "date" },
                                        Age: { type: "number" }
                                    }
                                }
                            },
                            pageSize: 10
                        },
                        height: 500,
                        width:300,
                         toolbar: ["create"],
                        scrollable: true,
                        sortable: true,
                        filterable: true,
                        pageable: {
                            input: true,
                            numeric: false
                        },
                        columns: [

                            {
                                field: "LastName",
                                title: "Last Name",

                            },
                            {
                                field: "City",

                            },
                            {
                                field: "Title"
                            },
                            {
                                field: "BirthDate",
                                title: "Birth Date",
                                template: '#= kendo.toString(BirthDate,"MM/dd/yyyy") #'
                            },
                            {
                                field: "Age",
                                width: 50
                            },
                              { command: ["edit", "destroy"], title: "&nbsp;", width: "210px" },
                        ],
                         editable: "inline"
                    });
                });
            </script>
    <div role="main">
    </div>
    <footer>
    </footer>
</body>
</html>

解决方法:

该行为是预期的.问题来自这样一个事实,即新创建的记录没有ID,这就是为什么将它们视为新记录的原因.您可以使用isNew()方法进行检查.

当您取消对新记录(尚未同步)的编辑时,它将被删除.该操作与以下操作相同:按“添加新记录”,然后按“取消”.

这是an example,它演示了如何使用本地数据实现CRUD操作.

        transport: {
            read: function(o) {
                //pass the date
                o.success(data);
            },
            create: function(o) {
                var item = o.data;
                //assign a unique ID and return the record
                counter++;
                item.Id = counter;
                o.success(item);
            },
            update: function(o) {
                //edit the local array and mark the operation as successful
                o.success();
            },
            destroy: function(o) {
                //edit the local array and mark the operation as successful
                o.success();   
            }
        }

标签:kendo-ui,kendo-dataviz,javascript,jquery
来源: https://codeday.me/bug/20191031/1975620.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有