同一 Pyramid 页面上的 JQueryUI 自动完成的多个实例

发布于 2024-12-07 17:05:51 字数 1069 浏览 0 评论 0原文

我有一个有效的 JQueryUI 自动完成输入小部件,可以与我的 Pyramid 后端很好地配合。自动完成将其 request.params['term'] 发布到与其所在页面相同的 URL,并且 Pyramid 使用 request_param='term' 作为发送的路由谓词我的数据库查询的术语值可查看可调用。

但我现在遇到的问题是,我需要向同一页面添加更多自动完成小部件。但是,我无法使用相同的路由谓词 term,因为两个小部件都会发布 term,因此 Pyramid 不知道将帖子发送到哪里。

我知道您可以在 Pyramid 中创建自定义谓词,但据我所知,我可以用来创建自定义谓词的 2 个自动完成小部件没有什么独特之处。

为了提供一些上下文,下面是路由/视图定义。这个定义实际上是非法的,因为 Pyramid 正确地拒绝了相同的谓词,但它证明了我的困境:

config.add_route('search_programs','/search/programs')
config.add_view(
        'haystack.search.search_programs',
        route_name='search_programs',
        renderer='templates/search_programs.jinja2')
config.add_view(
        'haystack.search.programtype_autocomplete',
        route_name='search_programs',
        request_param='term', renderer='json')
config.add_view(
        'haystack.search.majorgenre_autocomplete',
        route_name='search_programs',
        request_param='term', renderer='json')

如果自动完成小部件有一个选项来指定用于发布的任意键,而不是每次都只指定“术语”,那就太棒了。任何帮助都会很棒。

I have a working JQueryUI Autocomplete input widget working nicely with my Pyramid backend. The autocomplete posts its request.params['term'] to the same URL as the page its on, and Pyramid uses request_param='term' as a route predicate to send the term value to my database query view-callable.

The problem I have now though, is that I need to add more autocomplete widgets to the same page. However, I can't use the same route predicate term because both widgets post term and so Pyramid doesn't know where to send the post.

I know you can make custom predicates in Pyramid, but as far as I can see, there is nothing unique about the 2 autocomplete widgets with which I can make a custom predicate.

To give some context, below is the route/view definition. The definition is actually illegal because Pyramid rightly refused to identical predicates, but it serves to demonstrate my dilemma:

config.add_route('search_programs','/search/programs')
config.add_view(
        'haystack.search.search_programs',
        route_name='search_programs',
        renderer='templates/search_programs.jinja2')
config.add_view(
        'haystack.search.programtype_autocomplete',
        route_name='search_programs',
        request_param='term', renderer='json')
config.add_view(
        'haystack.search.majorgenre_autocomplete',
        route_name='search_programs',
        request_param='term', renderer='json')

What would be awesome is if the autocomplete widget had an option to specify an arbitrary key for posting, instead of just 'term' every time. Any help would be awesome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

提赋 2024-12-14 17:05:51

您需要为每个小部件显式设置自动完成,并在帖子中指出特定的网址和/或自定义数据。

这是来自 JQueryUI 文档上的 远程 JSONP 数据源 示例的剪辑:

$( "#a-particular-auto-complete-widget" ).autocomplete({
    source: function( request, response ) {
        $.ajax({ 
                 /* specifiy your specific url here - could be a different 
                  * route for each different source....
                  */
                 url: "http://someurl.com/search/programs/majorgenre",
                 dataType: "jsonp",
                 data: { featureClass: "P",
                         style: "full",
                         maxRows: 12,
                         /* ...or add a custom piece of data to 
                          * indicate how it should be handled by 
                          * your Pyramid view(s).
                          */
                         customParam: "majorgenre",
                         name_startsWith: request.term
                       }
 /* see docs for rest of code... */
 .... 

然后您可以在 Pyramid 中按照你想要的方式处理它。我可能只会将一个视图映射到这条路线(而不用担心 ajax customParam):

config.add_route('search_programs','/search/programs/{autocomplete}')

然后在视图中:

def autocomplete_handler_view(request):
    autocomplete_type = request.matchdict.get('autocomplete', None)
    if autocomplete == 'majorgenre':
        return handle_majorgenre(request.params['term'])

    elif autocomplete == 'programtype':
        return handle_programtype(request.params['term'])

You need to set your autocomplete for each widget explicitly and indicate a specific url and/or a custom piece of data in the post.

This is a clip from the Remote JSONP datasource example on the JQueryUI documentation:

$( "#a-particular-auto-complete-widget" ).autocomplete({
    source: function( request, response ) {
        $.ajax({ 
                 /* specifiy your specific url here - could be a different 
                  * route for each different source....
                  */
                 url: "http://someurl.com/search/programs/majorgenre",
                 dataType: "jsonp",
                 data: { featureClass: "P",
                         style: "full",
                         maxRows: 12,
                         /* ...or add a custom piece of data to 
                          * indicate how it should be handled by 
                          * your Pyramid view(s).
                          */
                         customParam: "majorgenre",
                         name_startsWith: request.term
                       }
 /* see docs for rest of code... */
 .... 

Then you can handle it in Pyramid however you want. I would probably just map one view to this route (and not bother with an ajax customParam):

config.add_route('search_programs','/search/programs/{autocomplete}')

And then in the view:

def autocomplete_handler_view(request):
    autocomplete_type = request.matchdict.get('autocomplete', None)
    if autocomplete == 'majorgenre':
        return handle_majorgenre(request.params['term'])

    elif autocomplete == 'programtype':
        return handle_programtype(request.params['term'])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文