[PATCH 33/51] dynatable: Tweak dynatable to work with DRF ordering

Damien Lespiau damien.lespiau at intel.com
Sat Sep 12 01:55:06 AEST 2015


The dynatable plugin doesn't send its ordering parameters in the way
Django Rest Framework expects. Tweak a bit how dynatable crafts its Ajax
URL so we can sort series.

Signed-off-by: Damien Lespiau <damien.lespiau at intel.com>
---
 lib/packages/jquery/jquery.dynatable.js | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)

diff --git a/lib/packages/jquery/jquery.dynatable.js b/lib/packages/jquery/jquery.dynatable.js
index 7397110..d8d0179 100644
--- a/lib/packages/jquery/jquery.dynatable.js
+++ b/lib/packages/jquery/jquery.dynatable.js
@@ -214,7 +214,23 @@
     // TODO: Wrap this in a try/rescue block to hide the processing indicator and indicate something went wrong if error
     this.processingIndicator.show();
 
-    if (this.settings.features.sort && !$.isEmptyObject(this.settings.dataset.sorts)) { data[this.settings.params.sorts] = this.settings.dataset.sorts; }
+    if (this.settings.features.sort &&
+        !$.isEmptyObject(this.settings.dataset.sorts)) {
+        var sorts = this.settings.dataset.sorts,
+            keys = this.settings.dataset.sortsKeys,
+            resolved_sorts = [];
+
+        for (var k = 0; k < keys.length; k++) {
+            var key = keys[k];
+
+            if (sorts[key] === -1)
+                resolved_sorts.push('-' + key);
+            else
+                resolved_sorts.push(key);
+        }
+
+        data[this.settings.params.sorts] = resolved_sorts.join(',');
+    }
     if (this.settings.features.paginate && this.settings.dataset.page) {
       var page = this.settings.dataset.page,
           perPage = this.settings.dataset.perPage;
@@ -905,7 +921,16 @@
     this.init = function() {
       var sortsUrl = window.location.search.match(new RegExp(settings.params.sorts + '[^&=]*=[^&]*', 'g'));
       if (sortsUrl) {
-        settings.dataset.sorts = utility.deserialize(sortsUrl)[settings.params.sorts];
+        var tokens = sortsUrl[0].slice(settings.params.sorts.length+1).split(",");
+
+        for (var t = 0; t < tokens.length; t++) {
+          var token = tokens[t];
+
+          if (token.charAt(0) === '-')
+            settings.dataset.sorts[token.slice(1)] = -1;
+          else
+            settings.dataset.sorts[token] = 1;
+        }
       }
       if (!settings.dataset.sortsKeys.length) {
         settings.dataset.sortsKeys = utility.keysFromObject(settings.dataset.sorts);
-- 
2.1.0



More information about the Patchwork mailing list