简体   繁体   English

如何在jqueryui自动完成功能中访问对象响应(而不是列表)?

[英]How do I access an object response (rather than a list) in jqueryui autocomplete?

I have a jqueryui autocomplete function I am trying to implement and the following returned results: 我有一个要尝试实现的jqueryui自动完成功能,并且返回以下结果:

{'returned_results':[{'label':'red','value':'Fred'},{'label':'blue','value':'Marie'}] }

If it was just a list of objects the below code would work but since I am using Flask it will not let me jsonify a list, so I need to put the results in a dictionary/object. 如果只是对象列表,则下面的代码将起作用,但是由于我使用的是Flask,因此不会让我对列表进行json加密,因此我需要将结果放入字典/对象中。 How do I access 'returned_results'? 如何访问“ returned_results”?

function tickerFormatter(){
   $.ui.autocomplete.prototype._renderItem = function(ul,item){
       var re = new RegExp(this.term,"i");
       var l = item.label;
       var v = item.value;
       .... 
       return $("<li></li>")
       .data('item.autocomplete',item)
       .append('<a>' + l + ':&nbsp;' + v + '</a>')
       .appendTo(ul);
   };
} 

$(function(){
    tickerFormatter();
     $("#tickers").autocomplete({
    minLength: 1,
    source: '/my_api?',
    select: function(event,ui){
      ....

    }
   })

  });

You can do one of two things: 您可以执行以下两项操作之一:

  1. Change source to use the function form : 更改source以使用function形式

     $("#tickers").autocomplete({ minLength: 1, source: function(request, callback) { $.getJSON('/my_api', request, function(data) { callback(data.returned_results); }); } }); 
  2. Return your own custom response, using make_response : 使用make_response返回您自己的自定义响应:

     import json from flask import make_response # Then in your controller response = make_response(json.dumps(list_of_results)) response['Content-Type'] = 'application/json' return response 

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

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