Created
May 12, 2012 12:37
-
-
Save moschlar/2666333 to your computer and use it in GitHub Desktop.
JSSortableTableBase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tablesorter_js = JSLink(link=url('/jquery.tablesorter.js'), | |
javascript=[jquery_js]) | |
class JSSortableTableBase(TableBase): | |
'''A TableBase that uses the jquery plugin tablesorter for table sorting''' | |
# javascript = [tablesorter_js] | |
# css_classes = ['tablesorter'] | |
# Any additional arguments to tablesorter, see http://tablesorter.com/docs/#Configuration | |
__tablesorter_args__ = {} | |
def _do_get_widget_args(self): | |
'''Inject javascript into widget_args since the above does not work''' | |
args = super(JSSortableTableBase, self)._do_get_widget_args() | |
# Insert tablesorter_js | |
try: | |
args['javascript'].append(tablesorter_js) | |
except KeyError: | |
args['javascript'] = [tablesorter_js] | |
# Insert tablesorter as css class for referencing it if no id specified while rendering | |
try: | |
args['css_classes'].append('tablesorter') | |
except KeyError: | |
args['css_classes'] = ['tablesorter'] | |
return args | |
def __call__(self, *args, **kw): | |
'''Intercept call to inject the js call''' | |
# Get selector | |
if 'id' in kw: | |
# either by id | |
selector = '#%s' % kw['id'] | |
else: | |
# or by class | |
selector = '.tablesorter' | |
self.__widget__.add_call(js_function('jQuery')(selector).tablesorter(self.__tablesorter_args__)) | |
return super(JSSortableTableBase, self).__call__(*args, **kw) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment