custom filter in grid

97 views
Skip to first unread message

Carlos Cesar Caballero Díaz

unread,
Aug 10, 2017, 6:55:23 PM8/10/17
to web...@googlegroups.com
Hi guys, I have a datetime field but I need (using the grid) filter it
as a date field, so, if I put something like:

mydatetime = 2017-04-04

I need to show all records with datetimes that belongs to this day.

Greetings.

黄祥

unread,
Aug 10, 2017, 8:15:26 PM8/10/17
to web2py-users
in grid search form field you can use :

table.table_datetime >= "2017-04-04 00:00:00" and table.table_datetime <= "2017-04-04 24:00:00"
table.table_datetime >= "2017-04-04 00:00" and table.table_datetime <= "2017-04-04 24:00"
table.table_datetime >= "2017-04-04 00" and table.table_datetime <= "2017-04-04 24"

or

table.table_datetime >= "2017-04-04 00:00:00" and table.table_datetime < "2017-04-05 00:00:00"
table.table_datetime >= "2017-04-04 00:00" and table.table_datetime < "2017-04-05 00:00"
table.table_datetime >= "2017-04-04 00" and table.table_datetime < "2017-04-05 00"

best regards,
stifan

Carlos Cesar Caballero Díaz

unread,
Aug 11, 2017, 10:28:56 AM8/11/17
to web...@googlegroups.com
Sitfan, thanks for your answer, I know about that, but is annoying for the users to put datetime values twice and they are asking about use only the day, they don't need to filter in that grid by date and time, just by date.

Greetings.

El 10/08/17 a las 20:15, 黄祥 escribió:

黄祥

unread,
Aug 11, 2017, 4:07:54 PM8/11/17
to web2py-users
just an idea why not create the new field with the date type with default value = request.now? so that user can search through the new date field

best regards,
stifan

Carlos Cesar Caballero Díaz

unread,
Aug 14, 2017, 10:36:34 AM8/14/17
to web...@googlegroups.com

Yes, that's a possible solution, but I don't want to duplicate information that is already there, I just want to find a way to use it in the way I need.

Greetings.


El 11/08/17 a las 16:07, 黄祥 escribió:
just an idea why not create the new field with the date type with default value = request.now? so that user can search through the new date field

best regards,
stifan

Leonel Câmara

unread,
Aug 14, 2017, 11:34:55 AM8/14/17
to web2py-users
Here's one way to do it. Note that when I start having this kind of problems I usually stop using the grid and make a custom solution.  
  
In the grid, the searchable argument, can be a function. In that case it gets the fields and keywords and its responsibility is to generate and return a query. You can disable advanced_search and then only have a search field. Then you will have a searchable function which will detect if one of the keywords entered in the search is a date, if that is the case then you will make a query where only records from that date are acceptable.

Your controller function would be something like this:

def mygrid():
   
import datetime


   
def searchable(fields, keywords):
        query
= db.my_table.id > 0:
       
for keyword in keywords:
            date
= None
           
try:
                date
= datetime.datetime.strptime(keyword,'%Y-%m-%d')
           
except ValueError:
               
pass
           
if date:
                query
&= db.my_table.created_on == date
           
else:
               
# this is just an example
                query
&= db.my_table.name.like('%' + keyword + '%', case_sensitive=False)
       
return query


   
return {'grid': SQLFORM.grid(db.my_table, searchable=searchable, advanced_search=False)}


Carlos Cesar Caballero Díaz

unread,
Aug 15, 2017, 10:29:42 AM8/15/17
to web...@googlegroups.com

Thanks Leonel, I will go with this.


Greetings.


El 14/08/17 a las 11:34, Leonel Câmara escribió:
Reply all
Reply to author
Forward
0 new messages