【问题标题】:add new row to empty kendo grid将新行添加到空的剑道网格
【发布时间】:2013-06-24 14:11:54
【问题描述】:

我有这个网格

$("#email-grid").kendoGrid({
    dataSource: {
        transport: {
            read: {
                url: "operations/get_emails_sales_reps.php?salesRepsId=" + salesRepsId,
                type: "GET"
            },
            update: {
                url: "operations/edit_email.php?salesRepsId=" + salesRepsId,
                type: "POST",
                complete: function (e) {
                    $("#email-grid").data("kendoGrid").dataSource.read();
                }
            },
            destroy: {
                url: "operations/delete_email.php",
                type: "POST",
                complete: function (e) {
                    $("#email-grid").data("kendoGrid").dataSource.read();
                }
            },
            create: {
                url: "operations/add_email.php?salesRepsId=" + salesRepsId,
                type: "POST",
                complete: function (e) {
                    $("#email-grid").data("kendoGrid").dataSource.read();
                }
            },
        },


        schema: {
            data: "data",
            total: "data.length", //total amount of records
            model: {
                id: "SalesRepId",
                fields: {
                    EmailType: {
                        defaultValue: {
                            EmailTypeId: 2,
                            EmailTypeName: "Home"
                        }
                    },
                    EmailText: {
                        type: "string"
                    },
                    IsMainEmail: {
                        type: "boolean"
                    }
                }
            }

        },
        pageSize: 5,
    },
    height: 250,
    filterable: true,
    sortable: true,
    pageable: true,
    reorderable: false,
    groupable: false,
    batch: true,
    navigatable: true,
    toolbar: ["create", "save", "cancel"],
    editable: true,
    columns: [{
        field: "EmailType",
        title: "Type",
        editor: EmailTypeDropDownEditor,
        template: "#=EmailType.EmailTypeName#"
    }, {
        field: "EmailText",
        title: "Email",

    }, {
        field: "IsMainEmail",
        title: "Main?",
        width: 65,
        template: function (e) {
            if (e.IsMainEmail == true) {
                return '<img align="center" src ="images/check-icon.png" />';
            } else {
                return '';
            }
        }
        // hidden: true

    }, {
        command: "destroy",
        title: "&nbsp;",
        width: 90
    },

    ]
});

服务器端的代码(get_emails_sales_reps.php)

<?php
require_once ("../lib/salesrep.php");
require_once ("../lib/helper.php");

// add the header line to specify that the content type is JSON
header("Content-type: application/json");

$options = array();

$result = SalesRep::getRepEmails($_GET["salesRepsId"]);
if (isset($result) && $result != null) {
    $result = _object_to_array($result);
    if (isset($result[0]) && is_array($result)) {
        for ($i = 0; $i < count($result); $i++) {
            $result[$i]["EmailType"] = array("EmailTypeName" => $result[$i]["EmailType"], "EmailTypeId" => $result[$i]["EmailTypeId"]);
        }
    } else {
        $result["EmailType"] = array("EmailTypeName" => $result["EmailType"], "EmailTypeId" => $result["EmailTypeId"]);
    }


    if (isset($result) || $result != null) {
        echo "{\"data\":" . json_encode($result) . "}";
    } else {
        echo "{\"data\": {} }";
    }
}
?>

当网格有一个或多个记录时,我可以添加新记录而不会出现任何错误,但是当网格没有记录时,我会尝试添加新记录。我收到此错误

未捕获的类型错误:无法读取未定义的属性“长度”

请问,我该如何解决这个问题??

【问题讨论】:

  • 我已经解决了这个问题,谢谢
  • 请回答您的问题,以便其他面临同样困难的人可以得到您的帮助.. :)

标签: javascript grid kendo-ui kendo-grid


【解决方案1】:

我已经通过编辑 php 文件解决了这个问题。当结果为空(空)时,我必须像这样返回一个空的 json 数组

else {
      // the result is null 

    echo "{\"data\": [] }";
}

【讨论】:

  • 这不是发布和回答问题的好方法,仅限您自己。stackoverflow.com/questions/how-to-ask
  • @dotNETkid 如果你回答你的问题绝对没有问题,即使你的帖子没有得到答案也是首选。
猜你喜欢
  • 2023-03-10
  • 1970-01-01
  • 2013-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多