Good one.

You earned a browser bookmark.

Thanks.

On Wednesday, March 7, 2012 4:44:00 AM UTC-5, peter wrote:
>
> As I have commented before, sqlform.grid has a particular way of 
> searching. Its combination of search and 'queries' is very powerful. 
> However the search does not really work in the way a casual web user 
> might expect. If entered - Bob Dylan - in the search box and clicked 
> search, then one get 'invalid query'. I eventually worked out how to 
> change the search widget, so as I believe this is currently 
> undocumented I thought I would share it. The semantics I have chosen 
> for search are that it will return records where any of the specified 
> fields (fielda and fieldb in the example) contain all of the words in 
> the search field. i.e if the search term is -Bob Dylan- it will return 
> records that have both Bob and Dylan in the fieldA or both in fieldb. 
>
> Define a search function that creates the form and a function that 
> does the search 
>
> def search_form(self,url): 
>     form = FORM('', 
>   
> INPUT(_name='search_text',_value=request.get_vars.search_text, 
>                _style='width:200px;', 
>                _id='searchText'), 
>          INPUT(_type='submit',_value=T('Search')), 
>          INPUT(_type='submit',_value=T('Clear'), 
>          _onclick="jQuery('#search_text').val('');"), 
>          _method="GET",_action=url) 
>
>     return form 
>
> def search_query(tableid,search_text,fields): 
>     words= search_text.split(' ') if search_text else [] 
>     query=tableid<0#empty query 
>     for field in fields: 
>         new_query=tableid>0 
>         for word in words: 
>             new_query=new_query&field.contains(word) 
>         query=query|new_query 
>     return query 
>
> Then in the function that has the sqlform.grid call, before the call 
> add 
>
>     search_text=request.get_vars.search_text 
>     query=search_query(db.tablea.id,search_text, 
> [db.tablea.fielda,db.tablea.fieldb]) 
>     ... 
> # the query could now be combined with other queries. 
>
>     table=SQLFORM.grid(query, search_widget=search_form........) 
>
>     return dict(table=table) 
>
>
>
>
>
>
>
>
>

Reply via email to