使用Python和webapp2的Jquery自动完成

2024-05-20 21:28:44 发布

您现在位置:Python中文网/ 问答频道 /正文

重用jquery自动完成:https://jqueryui.com/autocomplete/#remote

我以类似的方式工作,调用远程数据源:这是我的代码

class Products(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'application/json'
        data = ['cat','dog','bird', 'wolf']
        data = json.dumps(data) 
        self.response.out.write(data)

app = webapp2.WSGIApplication([
    ('/', MainPage),
    ('/products', Products),
], debug=True)

和JS

^{pr2}$

我有自动完成似乎与2个最小类型的头工作。但当测试时,它没有进行自动完成/正确的搜索。也就是说,不管我怎么搜索,它都会查询列表中的所有4项。在


Tags: httpsselfcomjsondata远程remoteresponse
1条回答
网友
1楼 · 发布于 2024-05-20 21:28:44

使用URL源时,Jquery不过滤列表。它将querystring中的搜索项作为term变量传递。远程源的文档如下:http://api.jqueryui.com/autocomplete/#option-source

您需要根据term request参数返回处理程序中过滤的数据。换句话说,将您的产品处理程序更改为类似这样的内容:

class Products(webapp2.RequestHandler):
    def get(self):
        term = self.request.get('term', None)
        self.response.headers['Content-Type'] = 'application/json'
        data = ['cat', 'dog', 'bird', 'wolf']
        if term:
            data = [i for i in data if i.find(term) >= 0 ]
        data = json.dumps(data)
        self.response.out.write(data)

以下是基于jquery ui自动完成示例的完整工作示例:

^{pr2}$

相关问题 更多 >