热门标签 | 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

推荐阅读
  • express工程中的json调用方法
    本文介绍了在express工程中如何调用json数据,包括建立app.js文件、创建数据接口以及获取全部数据和typeid为1的数据的方法。 ... [详细]
  • Python的参数解析argparse模块的学习
    本文介绍了Python中参数解析的重要模块argparse的学习内容。包括位置参数和可选参数的定义和使用方式,以及add_argument()函数的详细参数关键字解释。同时还介绍了命令行参数的操作和可接受数量的设置,其中包括整数类型的参数。通过学习本文内容,可以更好地理解和使用argparse模块进行参数解析。 ... [详细]
  • 本文介绍了DataTables插件的官方网站以及其基本特点和使用方法,包括分页处理、数据过滤、数据排序、数据类型检测、列宽度自动适应、CSS定制样式、隐藏列等功能。同时还介绍了其易用性、可扩展性和灵活性,以及国际化和动态创建表格的功能。此外,还提供了参数初始化和延迟加载的示例代码。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • SpringBoot uri统一权限管理的实现方法及步骤详解
    本文详细介绍了SpringBoot中实现uri统一权限管理的方法,包括表结构定义、自动统计URI并自动删除脏数据、程序启动加载等步骤。通过该方法可以提高系统的安全性,实现对系统任意接口的权限拦截验证。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 推荐系统遇上深度学习(十七)详解推荐系统中的常用评测指标
    原创:石晓文小小挖掘机2018-06-18笔者是一个痴迷于挖掘数据中的价值的学习人,希望在平日的工作学习中,挖掘数据的价值, ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 第四章高阶函数(参数传递、高阶函数、lambda表达式)(python进阶)的讲解和应用
    本文主要讲解了第四章高阶函数(参数传递、高阶函数、lambda表达式)的相关知识,包括函数参数传递机制和赋值机制、引用传递的概念和应用、默认参数的定义和使用等内容。同时介绍了高阶函数和lambda表达式的概念,并给出了一些实例代码进行演示。对于想要进一步提升python编程能力的读者来说,本文将是一个不错的学习资料。 ... [详细]
  • 本文讨论了如何在codeigniter中识别来自angularjs的请求,并提供了两种方法的代码示例。作者尝试了$this->input->is_ajax_request()和自定义函数is_ajax(),但都没有成功。最后,作者展示了一个ajax请求的示例代码。 ... [详细]
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社区 版权所有