Web2py下拉菜单过滤

时间:2016-02-04 10:34:47

标签: javascript jquery html web2py

html / javascript / web2py 的新手,但我一直在阅读很多内容,并看到了许多不同的排序方式和一些过滤方法。但是,我还没找到与我类似的东西 现在,我正在创建一个类似于Craigslist的网站,您可以在其中发布项目,并且我尝试制作可以过滤的下拉菜单。例如,如果我点击car,它将只显示在类别中包含关键字Car的帖子。

现在,你可以创建一个帖子,(IS_IN_SET)已经有了你的类别。然而,这是我迷路的地方。我不确定如何从(IS_IN_SET)获取关键字,以便我可以使用这些词来过滤。

这是在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_IN_DB(分贝,&#39; category.id&#39;&#39;%(t类别)■&#39)

寻找有关尝试解决此问题的任何建议/提示。

谢谢。

1 个答案:

答案 0 :(得分:0)

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

IN 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")
    )

视图(views / 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>

视图(views / default / posts_load.html):

{{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}}

IN CONTROLLER(controllers / default.py):

"""
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