Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expand filtering options #23

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions fa/jquery/pyramid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
from fa.jquery.renderers import ellipsys
import logging

import json
from sqlalchemy import or_, and_

_ = TranslationStringFactory('fa_jquery')

class ModelView(Base):
Expand Down Expand Up @@ -73,19 +76,35 @@ def get_page(self, **kwargs):
if 'searchField' in params:
field = fields.get(params['searchField'], None)
if field:
op = params['searchOper']
value = params['searchString']
if op == 'cn':
value = '%%%s%%' % value
filter = field.ilike(value)
else:
filter = field==value
filter = self.get_page_search_filter(field, params['searchOper'], params['searchString'])
collection = collection.filter(filter)
if 'filters' in params:
filters = json.loads(params['filters'])
clauses = []
for rule in filters['rules']:
field = fields.get(rule['field'], None)
if field:
filter = self.get_page_search_filter(field, rule['op'], rule['data'])
clauses.append(filter)
if filters['groupOp'] == 'AND':
collection = collection.filter(and_(*clauses))
else:
collection = collection.filter(or_(*clauses))
kwargs.update(collection=collection)
if 'items_per_page' not in kwargs:
kwargs.update(items_per_page=int(self.request.GET.get('rows', 20)))
return Base.get_page(self, **kwargs)

def get_page_search_filter(self, field, op, value):
if op == 'cn':
value = '%%%s%%' % value
filter = field.ilike(value)
elif op == 'ne':
filter = field!=value
else:
filter = field==value
return filter

def render_xhr_format(self, fs=None, **kwargs):
resp = Base.render_xhr_format(self, fs=fs, **kwargs)
if fs and self.request.POST and 'field' not in self.request.GET:
Expand Down Expand Up @@ -113,7 +132,8 @@ def update_grid(self, grid, *args, **kwargs):
metadatas = ('width', 'align', 'fixed', 'search', 'stype', 'searchoptions')
for field in grid.render_fields.values():
metadata = dict(search=0, sortable=1, id=field.key, name=field.key)
searchoptions = dict(sopt=['eq', 'cn'])
searchoptions = dict(sopt=['eq', 'ne', 'cn'])
limitedsearch = False
if field.is_relation:
metadata.update(width=100, sortable=0)
elif isinstance(field.type, (utils.Color, utils.Slider)):
Expand All @@ -125,10 +145,18 @@ def update_grid(self, grid, *args, **kwargs):
metadata.update(search=1)
elif isinstance(field.type, (fatypes.Date, fatypes.Integer)):
metadata.update(width=70, align='center')
metadata.update(search=1)
limitedsearch = True
elif isinstance(field.type, fatypes.DateTime):
metadata.update(width=120, align='center')
metadata.update(search=1)
limitedsearch = True
elif isinstance(field.type, fatypes.Boolean):
metadata.update(width=30, align='center')
metadata.update(search=1)
limitedsearch = True
if limitedsearch:
searchoptions = dict(sopt=['eq', 'ne'])
if metadata['search']:
metadata['searchoptions'] = searchoptions
metadata = dict(json=dumps(metadata))
Expand Down
2 changes: 1 addition & 1 deletion fa/jquery/resources/fa.jqgrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ $.fa.extend({
options['pager_id'] = '#'+pager.attr('id');
table.jqGrid(settings);
table.jqGrid('navGrid', options.pager_id,
{search:true});
{search:true}, {},{},{},{multipleSearch:true});
$('#add_'+table.attr('id'))
.unbind('click')
.click(function() { editRow(); });
Expand Down