Javascript 用于筛选的Web2py下拉菜单

Javascript 用于筛选的Web2py下拉菜单,javascript,jquery,html,web2py,Javascript,Jquery,Html,Web2py,对html/javascript/web2py还不熟悉,但我读了很多书,看到了许多不同的排序方法和一些过滤方法。然而,我还没有找到与我类似的东西。 现在,我正在创建一个类似于Craigslist的网站,在那里你可以发布项目,我正在尝试制作一个可以过滤的下拉菜单。例如,如果我单击car,它将只显示类别中包含关键字car的帖子 现在,您可以创建一篇文章,并且IS_IN_SET已经为您提供了类别。然而,这就是我迷路的地方。我不知道如何从IS_IN_集合中获取关键字,因此我可以使用这些单词进行筛选 这是

对html/javascript/web2py还不熟悉,但我读了很多书,看到了许多不同的排序方法和一些过滤方法。然而,我还没有找到与我类似的东西。 现在,我正在创建一个类似于Craigslist的网站,在那里你可以发布项目,我正在尝试制作一个可以过滤的下拉菜单。例如,如果我单击car,它将只显示类别中包含关键字car的帖子

现在,您可以创建一篇文章,并且IS_IN_SET已经为您提供了类别。然而,这就是我迷路的地方。我不知道如何从IS_IN_集合中获取关键字,因此我可以使用这些单词进行筛选

这是用db.py表示的

db.define_table('posts',  
Field('title', requires=IS_NOT_EMPTY()),  
Field('interests'),  
Field('category', requires=IS_IN_SET(['Computer', 'Electronics', 'Cars', 'Video Games'])),  
在我的默认/索引中,我创建了

 <select>  
    <option value='0'>(Select Category)</option><!--added-->
    <option value="Computer">Computer</option>
    <option value="Electronics">Electronics</option>
    <option value="Cars">Cars</option>
    <option value="Video Games">Video Games</option>
    </select>
但我不知道接下来该怎么办。 我了解到您可以使用IS_IN_DB创建一个下拉过滤器列表。 我试过使用,但我很确定这是错误的。。。。 db.category.requires=IS_在_DBdb中,'category.id','%tCategory's'

寻找解决此问题的任何建议/提示


多谢各位

这是一个展示Web2py强大和简单的解决方案。我还添加了一个表单来输入新的帖子。测试

在MODEL models/posts.py中:

post_categories = ['Computer', 'Electronics', 'Cars', 'Video Games']

db.define_table('posts',
    Field('title', requires=IS_NOT_EMPTY()),  
    Field('interests'),  
    Field('category', requires=IS_IN_SET(post_categories)),
    Field('body', "text")
    )
在视图/default/posts.html中:

<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
{{include 'web2py_ajax.html'}}

<div>
 <select id="post-select">  
    <option value='0'>(Select Category)</option>

    {{for item in post_categories:}}

    <option value="{{=item}}">{{=item}}</option>

    {{pass}}

 </select>
</div>

{{=form}}

{{=LOAD('default','posts_load', vars=dict(category=item_selected), target='posts-ajax', ajax=True)}}

<script>
$(document).ready(function() { 

    // pre-sets the option
    $('#post-select option[value="{{=item_selected}}"]').attr('selected', 'selected');

    $('#post-select').change(function(event) {

        var itemSelected = $(this).val();

        var urlOfLoadedComponent = "{{=URL(c='default', f='posts_load')}}";

        /* either one of these works but the first is better because it uses ajax */

        // refreshes the component with the new subset of records
        web2py_component( urlOfLoadedComponent + '?category=' + itemSelected, target='posts-ajax');

        //window.location.replace('http:/localhost/posts?category=' + itemSelected);
    });
});
</script>

现在,我正在尝试使用表单标记样式,这样一旦单击它,它就会转到视图调用showByCategory,在那里它应该对其进行过滤,但我不确定如何解决这个问题。此外,我还尝试了您在模型post_类别中使用的内容=[‘计算机’、‘电子产品’、‘汽车’、‘视频游戏’],但是当我尝试在索引中使用时,这些值没有显示出来。很抱歉,我的代码中出现了一些错误。所有设备都经过了固定和测试。如果你有任何问题,请告诉我。
{{for post in post_rows:}}
<div class="post">
  Title: <div class="post_title">{{=post.title}}</div>
  Post:  <blockquote class="post_body">{{=post.body}}</blockquote>
</div>
{{pass}}
"""
Usage:

http:/localhost/default/posts
-- or --
http:/localhost/default/posts?category=Computer
"""
def posts():

    # if category is specified in the url vars, use it otherwise use 'Computer'
    item_selected = request.vars.get('category', 'Computer')

    # or you could use the first one in the list:
    #item_selected = request.vars.get('category', post_categories[0])

    """
    creates the form
    processes new posts; posts() function called again on form submit via ajax
    the response.js refreshes form after ajax post
    """
    form=SQLFORM(db.posts).process()
    response.js = "jQuery('#posts-ajax').get(0).reload();"

    """
    you don't need to pass "post_categories = post_categories" in the dict
    because the view can see the variables defined in the models it runs
    """
    return dict(
                form = form,
                item_selected = item_selected
                )

def posts_load():

    category = request.vars.get('category', '')

    if category:

        post_rows = db(db.posts.category==category).select()

    else:

        post_rows = db(db.posts).select()

    return post_rows