使用 Javascript/KendoUI 自动完成渲染数据时出错 - Object #<Object>没有方法“切片" - 如何解决? [英] Error rendering data with Javascript / KendoUI autocomplete - Object #<Object> has no method 'slice' - how to resolve?

查看:12
本文介绍了使用 Javascript/KendoUI 自动完成渲染数据时出错 - Object #<Object>没有方法“切片" - 如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 使用 Kendo UI 和 MVC4 WebAPIOData 和 EF 文章.安装 KendoUI 并确保设置了所有引用后,我输入了三个字符,并收到以下错误:

<块引用>

Uncaught TypeError: Object # has no method 'slice'

问题的根源

为了节省阅读更新:通过调试,我发现问题是 JS 期望解析一个数组,而它在数据中不可用 - 在根.在数据层次结构中,它是一级.

原始问题

我清理了 kendo.web.min.js 并且错误发生在第 3498 行附近:

成功:函数(n){var i = 这个,r = i.options;返回 i.trigger(wt, {回复: n,类型:阅读"}), n = i.reader.parse(n), i._handleCustomErrors(n) ?(i._dequeueRequest(), t) : (i._pristine = et(n) ? e.extend(!0, {}, n) : n.slice ? n.slice(0) : n, i._total =i.reader.total(n), i._aggregate && r.serverAggregates && (i._aggregateResult = i.reader.aggregates(n)), n = i._readData(n), i._pristineData= n.slice(0), i._data = i._observe(n), i._addRange(i._data), i._process(i._data), i._dequeueRequest(), t)

Kendo UI 小部件和 css 一样加载得很好:

<link href="~/Content/kendo/kendo.default.min.css" rel="stylesheet"/><script src="~/Scripts/jquery-1.9.1.min.js"></script><script src="~/Scripts/kendo/kendo.web.min.js"></script><script src="~/Scripts/kendo/kendo.aspnetmvc.min.js"></script><script src="~/Scripts/appScripts.js"></script>

而且我在使用 Razor MVC 帮助程序/扩展时看到了同样的错误:

@(Html.Kendo().AutoComplete().Name("userAutoComplete")//指定小部件的id"属性.DataTextField("用户名").DataSource(源=>{source.Read(读=>{read.Url("/api/user");}).ServerFiltering(true);//如果为 true,DataSource 将不会过滤客户端上的数据}))

直接通过JS:

///<参考路径="kendo/kendo.aspnetmvc.min.js"/>///<reference path="kendo/kendo.core.min.js"/>///<reference path="kendo/kendo.autocomplete.min.js"/>///<reference path="kendo/kendo.web.min.js"/>$(document).ready(function () {//加载 KendoUI//从/api/user 获取数据var dataSource = new kendo.data.DataSource({运输: {读: {网址:/api/用户"}}});$("#userSearch").kendoAutoComplete({数据源:数据源,数据文本字段:用户名",最小长度:3});$("#userSearch").on('input', function () {console.log($("#userSearch").val());});});//$(document).ready()

我确定这是我可能遗漏的一些简单的东西.我已经尝试过 web 和所有 js 文件.

如有任何帮助,我们将不胜感激.

-- 更新 --

该内容中唯一缺少的真正 html 是 <input id="userAutoComplete"/>

基于从 http 获取 JSON 数据的 Kendo UI 示例之一,我创建了一个全新的解决方案和一个非常简单的视图://api.geonames.org,并得到同样的错误.

我认为使用最新的 JS 库(//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js 可能会导致问题,所以我尝试了 1.7 lib.同样的问题:

@using Kendo.Mvc.UI@{布局 = 空;}<!DOCTYPE html><头><meta name="viewport" content="width=device-width"/><title>索引</title><link rel="stylesheet" href="@Url.Content("~/Content/kendo.common.min.css")"><link rel="stylesheet" href="@Url.Content("~/Content/kendo.default.min.css")"><link rel="stylesheet" href="@Url.Content("~/Content/kendo.dataviz.min.css")"><script src="//ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script><script src="@Url.Content("~/Scripts/kendo.web.min.js")"></script><script src="@Url.Content("~/Scripts/kendo.aspnetmvc.min.js")"></script><script src="@Url.Content("~/Scripts/kendo.dataviz.min.js")"></script><script type="text/javascript">$(document).ready(function () {$("#autoComplete").kendoAutoComplete({最小长度:6,数据文本字段:标题",过滤器:包含",数据源:新 kendo.data.DataSource({运输: {读: {url: "http://api.geonames.org/wikipediaSearchJSON",数据: {q:函数(){返回 $("#autoComplete").data("kendoAutoComplete").value();},最大行数:10,用户名:演示"}}},架构:{数据:地名"}}),改变:函数(){this.dataSource.read();}})});<身体><div><input id="autoComplete"/>

-- 更新 --

使用上面的代码,我返回并再次尝试 - 效果很好.多次尝试后,我遇到了同样的问题.这是由于有效的 JSON 数据更改为以下内容:

{"status":{"message":"已超过每日30000个演示点数限制.请使用应用程序专用帐户.请勿使用演示帐户进行应用程序.","value":18}}

... 这让我看到了来自我的 API 的数据格式(在 Fiddler 中查看:

代替:

JSON---{...数据...

这是

JSON---$id=1---$值------{}---------$id=2---------创建时间...---------EMAIL=email@email.com---------团体------------$id=...------------$id=...---------USERNAME=某个用户名------{}---------$id=4...

因此,错误是由于无法在预期位置访问数组而导致的 - 而不是根,而是一层深.

如何将数据绑定到一层深度而不是 JSON 对象的根?

谢谢.

解决方案

对此的解决方案是通过描述结果格式来遍历数据层次结构.

由于数组包含在 $values 中,我使用了以下数据源/模式定义:

//从/api/user 获取数据var dataSource = new kendo.data.DataSource({运输: {读: {网址:/api/用户"}},schema: {//描述结果格式data: function(data) {//数据源将绑定到的数据在值字段中控制台日志(数据.$值);返回数据.$values;}}});

一件好事是能够在 Razor 助手中添加数据模式类型 - 似乎没有暂时支持.

因此,以下仍然不起作用:

@(Html.Kendo().AutoComplete().Name("userAutoComplete")//指定小部件的id"属性.Filter("开头").Placeholder("输入用户名...").DataTextField("用户名").DataSource(源=>{来源:source.Read(读=>{read.Url("/api/user");}).ServerFiltering(true);//如果为 true,DataSource 将不会过滤客户端上的数据}))

I am following the Using Kendo UI with MVC4 WebAPI OData and EF article. After installing KendoUI and making sure all references are set, I type in three characters, and get the following error:

Uncaught TypeError: Object # has no method 'slice'

Root of the Problem

To save reading through the updates: Through debugging I found that the issue is that JS is expecting to parse an array, where it isn't available in the data - at the root. In the data hierarchy, it's one level in.

Original Problem

I cleaned up kendo.web.min.js and the error is occuring around line 3498:

success: function (n) {
     var i = this,
         r = i.options;
     return i.trigger(wt, {
         response: n,
         type: "read"
     }), n = i.reader.parse(n), i._handleCustomErrors(n) ? (i._dequeueRequest(), t) : (i._pristine = et(n) ? e.extend(!0, {}, n) : n.slice ? n.slice(0) : n, i._total = i.reader.total(n), i._aggregate && r.serverAggregates && (i._aggregateResult = i.reader.aggregates(n)), n = i._readData(n), i._pristineData = n.slice(0), i._data = i._observe(n), i._addRange(i._data), i._process(i._data), i._dequeueRequest(), t)

The Kendo UI widgets are loading just fine as well as the css:

<link href="~/Content/kendo/kendo.common.min.css" rel="stylesheet" />
<link href="~/Content/kendo/kendo.default.min.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.9.1.min.js"></script>
<script src="~/Scripts/kendo/kendo.web.min.js"></script>
<script src="~/Scripts/kendo/kendo.aspnetmvc.min.js"></script>
<script src="~/Scripts/appScripts.js"></script>

And I am seeing the same error both with using the Razor MVC helper/extension:

@(Html.Kendo().AutoComplete()
    .Name("userAutoComplete")                   // specifies the "id" attribute of the widget
    .DataTextField("USERNAME")
    .DataSource(source =>
        {
            source.Read(read =>
                {
                    read.Url("/api/user");
                })
                  .ServerFiltering(true);       // if true, the DataSource will not filter the data on the client
        }
    )
)

and through directly through JS:

/// <reference path="kendo/kendo.aspnetmvc.min.js" />
/// <reference path="kendo/kendo.core.min.js" />
/// <reference path="kendo/kendo.autocomplete.min.js" />
/// <reference path="kendo/kendo.web.min.js" />

$(document).ready(function () {
    // load up KendoUI

    // gets data from /api/user
    var dataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: "/api/user"
            }
        }
    });

    $("#userSearch").kendoAutoComplete({
        dataSource: dataSource,
        dataTextField: "USERNAME",
        minLength: 3
    });

    $("#userSearch").on('input', function () {
        console.log($("#userSearch").val());
    });

}); // $(document).ready()

I'm sure this is something simple that I may be missing. I have tried both with the web and all js files.

Any assistance would be appreciated.

-- UPDATE --

The only real html missing from that content is the <input id="userAutoComplete" />

I created a brand new solution and a very simple view, based on one of the Kendo UI examples that gets JSON data from http://api.geonames.org, and getting the same error.

I thought that using the newest JS library (//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js may have been causing a problem so I tried the 1.7 lib. Same issue:

@using Kendo.Mvc.UI

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>

    <link rel="stylesheet" href="@Url.Content("~/Content/kendo.common.min.css")">
    <link rel="stylesheet" href="@Url.Content("~/Content/kendo.default.min.css")">
    <link rel="stylesheet" href="@Url.Content("~/Content/kendo.dataviz.min.css")">

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script> 

    <script src="@Url.Content("~/Scripts/kendo.web.min.js")"></script>
    <script src="@Url.Content("~/Scripts/kendo.aspnetmvc.min.js")"></script>
    <script src="@Url.Content("~/Scripts/kendo.dataviz.min.js")"></script>

    <script type="text/javascript">

        $(document).ready(function () {
            $("#autoComplete").kendoAutoComplete({
                minLength: 6,
                dataTextField: "title",
                filter: "contains",
                dataSource: new kendo.data.DataSource({
                    transport: {
                        read: {
                            url: "http://api.geonames.org/wikipediaSearchJSON",
                            data: {
                                q: function () {
                                    return $("#autoComplete").data("kendoAutoComplete").value();
                                },
                                maxRows: 10,
                                username: "demo"
                            }
                        }
                    },
                    schema: {
                        data: "geonames"
                    }
                }),
                change: function () {
                    this.dataSource.read();
                }
            })
        });

    </script>    


</head>
<body>
    <div>

        <input id="autoComplete"/>

    </div>
</body>
</html>

-- UPDATE --

Using the code above, I went back and tried it again - it worked fine. After trying several more times, I experienced the same issue. This was due to the valid JSON data changing to the following:

{"status":{"message":"the daily limit of 30000 credits for demo has been exceeded. Please use an application specific account. Do not use the demo account for your application.","value":18}}

... which lead me to look at the formatting of the data coming from my API (looking at it in Fiddler:

Instead of:

JSON ---{... data...

it's

JSON
---$id=1
---$values
------{}
---------$id=2
---------CREATEDATETIME...
---------EMAIL=email@email.com
---------GROUPS
------------$id=...
------------$id=...
---------USERNAME=someusername
------{}
---------$id=4
.
.
.

So the error is caused by the array not being accessible where the it's expected - instead of the root, it's one level deep.

How do I get data binding to the one-level-deep rather than the root of the JSON object?

Thanks.

解决方案

The solution for this was to traverse the data hierarchy by describing the result format.

Since the array is contained in $values, I used the following data source/schema definition:

    // gets data from /api/user
    var dataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: "/api/user"
            }
        },
        schema: {                               // describe the result format
            data: function(data) {              // the data which the data source will be bound to is in the values field
                console.log(data.$values);
                return data.$values;
            }
        }
    });

One thing that would be nice is to be able to add a data schema type in the Razor helper - which doesn't seem to be supported at this time.

Thus, the following still would not work:

@(Html.Kendo().AutoComplete()
        .Name("userAutoComplete")                   // specifies the "id" attribute of the widget
        .Filter("startswith")
        .Placeholder("Type user name...")
        .DataTextField("USERNAME")
        .DataSource(source =>
            {
                source:
                    source.Read(read =>
                        {
                            read.Url("/api/user");
                        })
                        .ServerFiltering(true);       // if true, the DataSource will not filter the data on the client

            }
        )
)

这篇关于使用 Javascript/KendoUI 自动完成渲染数据时出错 - Object #&lt;Object&gt;没有方法“切片" - 如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆