[table] use explicit add_onload() to setup table sorting (closes #2013493) stable
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Fri, 14 Oct 2011 09:04:39 +0200
branchstable
changeset 7952 48330faf4cd7
parent 7948 deb596dc343a
child 7953 a37531c8a4a6
child 7957 5da7ede69063
[table] use explicit add_onload() to setup table sorting (closes #2013493)
web/data/cubicweb.ajax.js
web/data/jquery.tablesorter.js
web/views/tableview.py
--- a/web/data/cubicweb.ajax.js	Thu Oct 13 11:49:19 2011 +0200
+++ b/web/data/cubicweb.ajax.js	Fri Oct 14 09:04:39 2011 +0200
@@ -257,10 +257,6 @@
 }
 
 function _postAjaxLoad(node) {
-    // find sortable tables if there are some
-    if (typeof(Sortable) != 'undefined') {
-        Sortable.sortTables(node);
-    }
     // find textareas and wrap them if there are some
     if (typeof(FCKeditor) != 'undefined') {
         buildWysiwygEditors();
--- a/web/data/jquery.tablesorter.js	Thu Oct 13 11:49:19 2011 +0200
+++ b/web/data/jquery.tablesorter.js	Fri Oct 14 09:04:39 2011 +0200
@@ -874,5 +874,3 @@
 Sortable.sortTables = function() {
    jQuery("table.listing").tablesorter({textExtraction: cubicwebSortValueExtraction});
 };
-
-jQuery(document).ready(Sortable.sortTables);
--- a/web/views/tableview.py	Thu Oct 13 11:49:19 2011 +0200
+++ b/web/views/tableview.py	Fri Oct 14 09:04:39 2011 +0200
@@ -24,7 +24,7 @@
 
 from cubicweb import NoSelectableObject, tags
 from cubicweb.selectors import nonempty_rset
-from cubicweb.utils import make_uid, json_dumps
+from cubicweb.utils import make_uid, js_dumps, JSString
 from cubicweb.view import EntityView, AnyRsetView
 from cubicweb.uilib import toggle_action, limitsize, htmlescape
 from cubicweb.web import jsonize, component, facet
@@ -45,6 +45,10 @@
     table_widget_class = TableWidget
     table_column_class = TableColumn
 
+    tablesorter_settings = {
+        'textExtraction': JSString('cubicwebSortValueExtraction'),
+        }
+
     def form_filter(self, divid, displaycols, displayactions, displayfilter,
                     paginate, hidden=True):
         try:
@@ -84,6 +88,15 @@
                 displaycols = range(len(self.cw_rset.syntax_tree().children[0].selection))
         return displaycols
 
+    def _setup_tablesorter(self, divid):
+        req = self._cw
+        req.add_js('jquery.tablesorter.js')
+        req.add_onload('''$(document).ready(function() {
+    $("#%s table.listing").tablesorter(%s);
+});''' % (divid, js_dumps(self.tablesorter_settings)))
+        req.add_css(('cubicweb.tablesorter.css', 'cubicweb.tableview.css'))
+
+
     def call(self, title=None, subvid=None, displayfilter=None, headers=None,
              displaycols=None, displayactions=None, actions=(), divid=None,
              cellvids=None, cellattrs=None, mainindex=None,
@@ -98,8 +111,8 @@
         :param displayactions: if True, display action menu
         """
         req = self._cw
-        req.add_js('jquery.tablesorter.js')
-        req.add_css(('cubicweb.tablesorter.css', 'cubicweb.tableview.css'))
+        divid = divid or req.form.get('divid') or 'rs%s' % make_uid(id(self.cw_rset))
+        self._setup_tablesorter(divid)
         # compute label first  since the filter form may remove some necessary
         # information from the rql syntax tree
         if mainindex is None:
@@ -108,7 +121,6 @@
         hidden = True
         if not subvid and 'subvid' in req.form:
             subvid = req.form.pop('subvid')
-        divid = divid or req.form.get('divid') or 'rs%s' % make_uid(id(self.cw_rset))
         actions = list(actions)
         if mainindex is None:
             displayfilter, displayactions = False, False