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