Fixes issue 28537: Grid is loaded properly after scrolling it and sorting it
authorAugusto Mauch <augusto.mauch@openbravo.com>
Wed, 07 Jan 2015 16:02:36 +0100
changeset 25733 6dd2893296e2
parent 25732 2722f629170e
child 25734 d9352a85fef4
Fixes issue 28537: Grid is loaded properly after scrolling it and sorting it

In this changeset [1] a change was done to prevent modifying improperly the totalRows property of the grid after scrolling. This code was meant to be executed after scrolling the grid, and to check it the lastScrollTop property and the getScrollTop() function of the grid body were compared. This check works properly when event that triggered the datasource request was the grid being filtered or a new page being fetched due to scrolling the grid.

The problem was that the check did not work properly when the datasource request was triggered due to having sorted the grid. In this case, the totalRows property should be left as is. To fix this, a new flag is used to control when the datasource request has been triggered by a sorting event.

[1] https://code.openbravo.com/erp/devel/pi/rev/c51dce7e9fd3
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js	Wed Jan 07 12:42:34 2015 +0100
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js	Wed Jan 07 16:02:36 2015 +0100
@@ -238,7 +238,8 @@
     },
 
     transformData: function (newData, dsResponse) {
-      var i, length, timeFields, responseToFilter, newTotalRows;
+      var i, length, timeFields, responseToFilter, responseToSort = false,
+          newTotalRows;
 
       // when the data is received from the datasource, time fields are formatted in UTC time. They have to be converted to local time
       if (dsResponse && dsResponse.context && (dsResponse.context.operationType === 'fetch' || dsResponse.context.operationType === 'update' || dsResponse.context.operationType === 'add')) {
@@ -259,6 +260,10 @@
         responseToFilter = true;
       }
 
+      if (dsResponse.context && dsResponse.context._dsRequest && dsResponse.context._dsRequest.params && dsResponse.context._dsRequest.params.isSorting) {
+        responseToSort = true;
+      }
+
       if (this.localData && !responseToFilter) {
         length = this.localData.length;
         newTotalRows = dsResponse.totalRows;
@@ -278,7 +283,8 @@
         }
 
         // detects if the request was issued due to having scrolled up
-        if (this.grid.body.lastScrollTop !== undefined && this.grid.body.lastScrollTop > this.grid.body.getScrollTop()) {
+        // this does not apply when the grid has just been sorted, as the previous local data is discarded
+        if (!responseToSort && this.grid.body.lastScrollTop !== undefined && this.grid.body.lastScrollTop > this.grid.body.getScrollTop()) {
           // in that case, set the totalRows of the response to the length of the localData, to avoid
           // setting the totalRows of the grid to an invalid value
           dsResponse.totalRows = this.localData.length;
@@ -2232,6 +2238,11 @@
       params[OB.Constants.WHERE_PARAMETER] = null;
     }
 
+    if (this.isSorting) {
+      params.isSorting = true;
+      delete this.isSorting;
+    }
+
     if (!isExporting) {
       first = true;
       selectedProperties = '';
@@ -4017,6 +4028,11 @@
         me.view.updateSubtabVisibility();
       });
     }
+  },
+
+  setSort: function (sortSpecifiers, forceSort) {
+    this.isSorting = true;
+    this.Super('setSort', arguments);
   }
 });