formatResult和formatItem选项在JQuery自动填充中有什么作用? [英] What does formatResult and formatItem options do in JQuery Autocomplete?

查看:402
本文介绍了formatResult和formatItem选项在JQuery自动填充中有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有点混淆,formatResult和formatItem在JQuery Autocomplete插件中做了什么?

Am a bit confused here, what does the formatResult and formatItem do in the JQuery Autocomplete plugin?

我有一个函数返回一个逗号分隔的字符串(从Django),但是我的自动完成功能无法将该字符串拆分为单独的条目/行,我该如何使用自动完成功能来实现?

I have a function that is returning a comma separated string (from Django), but my autocomplete function is unable to split the string into individual entries/rows, how can i achieve this using autocomplete functions?

例如,返回的结果如下所示,这是自动填充显示的: -
[one,oneTwo,Onethree anotherOne]

e.g the returned result looks like this and this what autocomplete is showing :- ["one","oneTwo", "Onethree", "anotherOne"]

我想要在自动填充字段中显示如下: -

I want when showing in the autocomplete field to have it split like this:-

one
oneTwo
Onethree
anotherOne

我有一种感觉,我可以使用formatResult和formatItem,但我不知道如何,没有好的例子在那里!!

I have a feeling i can use the formatResult and formatItem but i dont know how, there are no good examples out there !!

我的html模板中的代码:

my code in the html template:

 function autoFill(){
           $("#tags").autocomplete("/taglookup/", {
        width: 320,
        max: 4,
        highlight: false,
        multiple: true,
        multipleSeparator: " ",
        scroll: true,
        scrollHeight: 300
         });
       }

我使用Dajango处理GET请求。

Am using Dajango to process the GET request.

Gath

推荐答案

formatItem 项目显示在下拉列表中,而 formatResult 格式化选项后在输入框中显示的项。

formatItem formats an item for display in the dropdown list, while formatResult formats the item for display in the inputbox once it is selected.

默认情况下,自动填充需要从格式如下的指定网址获取数据:

By default, autocomplete expects to get data from the specified url formatted as:

foo|bar|baz|bing
zaz|ding|blop|grok

其中每行都是一行数据;每行是它传递给 formatItem formatResult 的数据。您可能希望以最小的阻力和返回数据的路径。

where each line is a row of data; each row being the data that it passes to formatItem and formatResult. You may want to take the path of least resistance and return data in the way it likes.

如果要使用不符合自动完成假设的数据,需要使用(无证据的,据我所知)解析选项来识别一个函数来解析你的ajax请求的结果。在我看来,你的django视图返回一个数组,而不是返回一个格式化的字符串。要将数组格式化为jqu​​ery,请执行以下操作:

If you want to use data that doesn't fit autocomplete's assumptions, you'll need to use the (undocumented, as far as I can tell) parse option to identify a function to parse the results of your ajax request. It appears to me that your django view is returning an array rather than returning a formatted string. To format your array as jquery would like it:

return HttpResponse('|'.join(your_array), mimetype='text/plain')

以下是使用非标准自动填充数据(JSON)执行自动完成的示例:

Here is an example of doing autocomplete using non-standard autocomplete data (JSON):

<script type="text/javascript"> 
  format_item = function (item, position, length){ 
    return item.title; 
  } 

 // Handle data from ajax request 
 prep_data = function(data){ 
   tmp = $.evalJSON(data); 
   parsed_data = []; 
   for (i=0; i < tmp.length; i++) { 
     obj = tmp[i]; 
     // Other internal autocomplete operations expect 
     // the data to be split into associative arrays of this sort 
     parsed_data[i] = { 
        data: obj , 
        value: obj.isbn13, 
        result: obj.title 
     }; 
   } 
   return parsed_data 
 } 

 $(document).ready(function(){ 
   $("#fop").autocomplete({ 
          url : "{% url book-search %}", 
          // undocumented 
          parse: prep_data, 
          formatItem: format_item, 
          }); 
 }) 

</script>

这篇关于formatResult和formatItem选项在JQuery自动填充中有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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