简体   繁体   English

带有Json的jQuery UI自动完成功能未显示建议

[英]jQuery UI Autocomplete With Json Not Showing Suggestions

I'm experiencing some troubles with an autocomplete component from jQueryUi. 我在使用jQueryUi的自动完成组件时遇到了一些麻烦。 The list with autocomplete suggestions doesn't appear. 带有自动完成建议的列表不会出现。

I've tested the following code (from jQuery UI ) and, despite servlet is sending a JSON object and "data" variable is storying it, component is still not showing suggestions list. 我已经测试了以下代码(来自jQuery UI ),尽管servlet发送了一个JSON对象,并且“ data”变量在讲述它,但组件仍未显示建议​​列表。

Also I tried the component with a simple list as source ( like here ), and it worked fine. 我还尝试了一个带有简单列表作为源的组件( 例如here ),并且工作正常。

Have you any idea on what would be happening? 您有什么想法吗?

<script>
$(function() {
         var cache = {};
            $( "#bear" ).autocomplete({
                minLength: 2,
                source: function( request, response ) {

                var term = request.term;                
                if ( term in cache ) {
                     response( cache[ term ] );
                     return;
                }

                $.getJSON( "/animals/MaintainMamals?operation=14", request, function( data, status, xhr ) {
                  cache[ term ] = data;
                  response( data );
                });

              }
            });
          });
</script>

<form>
    <div class="ui-widget">
       <label for="bear">Bear name (type a piece of name): </label>
       <input id="bear" name="bear" class="text ui-widget-content ui-corner-all"/>
    </div>
</form>

Json object used in testing (I tried the stuff with a simple jSon built with just a String refering to "name" property, with the same [bad] results): 测试中使用的Json对象(我​​尝试了一个简单的jSon(仅使用String引用“ name”属性,但结果相同)的东西):

[
  {
    "id": 1234567,
    "name": "Yogi Bear",
    "activity": {
      "handler": {
        "interfaces": [
          {}
        ],
        "constructed": true,
        "persistentClass": {},
        "getIdentifierMethod": {
          "clazz": {},
          "slot": 2,
          "name": "getCod",
          "returnType": {},
          "parameterTypes": [],
          "exceptionTypes": [],
          "modifiers": 1,
          "root": {
            "clazz": {},
            "slot": 2,
            "name": "getId",
            "returnType": {},
            "parameterTypes": [],
            "exceptionTypes": [],
            "modifiers": 1,
            "override": false
          },
          "override": false
        },
        "setIdentifierMethod": {
          "clazz": {},
          "slot": 3,
          "name": "setId",
          "returnType": {},
          "parameterTypes": [
            {}
          ],
          "exceptionTypes": [],
          "modifiers": 1,
          "root": {
            "clazz": {},
            "slot": 3,
            "name": "setId",
            "returnType": {},
            "parameterTypes": [
              {}
            ],
            "exceptionTypes": [],
            "modifiers": 1,
            "override": false
          },
          "override": false
        },
        "overridesEquals": false,
        "entityName": "com.zoo.Activity",
        "id": 7105,
        "initialized": false,
        "readOnly": false,
        "unwrap": false
      }
    }
  }
]

The autocomplete widget expects each item in your array to have a label property, a value property, or both. 自动完成小部件希望数组中的每个项目都具有label属性, value属性或同时具有二者。 Since your data does not have either, you can either: 由于您的数据既没有,也可以:

  1. Tweak your server-side data source to return items that meet that criteria, or 调整服务器端数据源以返回满足该条件的项目,或者
  2. Transform the data from your server-side code to match the criteria after you make the request. 发出请求后,从服务器端代码转换数据以匹配条件。

I can only provide an answer to #2 since I don't see your server-side code, so here's how you would do that: 由于看不到您的服务器端代码,因此我只能提供#2的答案,因此您将按照以下方式进行操作:

$(function() {
    var cache = {};

    $( "#bear" ).autocomplete({
        minLength: 2,
        source: function( request, response ) {
            var term = request.term;                
            if (term in cache) {
                 response(cache[ term ]);
                 return;
            }

            $.getJSON("/animals/MaintainMamals?operation=14", request, function (data, status, xhr) {
                /* Add a `label` property to each item */
                $.each(data, function (_, item) {
                    item.label = item.name;
                });

                cache[term] = data;
                response(data);
            });
        }
    });
});

Here's an example that fakes an AJAX request--other than that it should be similar to your situation. 这是一个伪造AJAX请求的示例 -不同于您的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM