热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Web2py下拉菜单过滤-Web2pyDropdownmenuthatfilters

Newtohtmljavascriptweb2py,butIbeenreadingalotandsawmanydifferentwaysintosortingand

New to html/Javascript/web2py, but I been reading a lot and saw many different ways into sorting and some ways to filtering. However, I haven't found something that is similar to mine.
Right now, I'm creating a website that is similar to Craigslist where you can post items and I'm attempting to make a drop down menu that can filter. For an example, if I click car, it will only show posts that has the keyword Car in the category.

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

Right now, you can create a post and (IS_IN_SET) will already have the categories there for you. However, this is where I'm getting lost. I'm not sure how to get the keywords from (IS_IN_SET) so I am able to use those words to filter.

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

This is in db.py

这是在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'])),  

In my default/index, I created

在我的默认/索引中,我创建了

 

But I don't know where to go from here. I read that you can use IS_IN_DB to create a drop down filter list. I tried using, but I'm pretty sure this is wrong.... db.category.requires=IS_IN_DB(db,'category.id','%(Tcategory)s')

但我不知道从哪里开始。我读到你可以使用IS_IN_DB创建一个下拉过滤器列表。我尝试过使用,但我很确定这是错误的.... db.category.requires = IS_IN_DB(db,'category.id','%(Tcategory)s')

Looking for any advice/tips in trying to solve this problem.

寻找任何建议/提示,试图解决这个问题。

Thank you.

1 个解决方案

#1


0  

Here is a solution that shows the power and simplicity of Web2py. I also added a form to enter new posts. Tested.

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

IN MODEL (models/posts.py):

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

IN VIEW (views/default/posts.html):

在视图中(views / default / posts.html):


{{include 'web2py_ajax.html'}}

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

IN VIEW (views/default/posts_load.html):

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

{{for post in post_rows:}}
Title:
{{=post.title}}
Post:
{{=post.body}}
{{pass}}

IN CONTROLLER (controllers/default.py):

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

推荐阅读
author-avatar
日韩潮衣一手临终批发
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有