fixed bug 28712: record selection lost in p&e grid if data has multiple pages
authorAsier Lostalé <asier.lostale@openbravo.com>
Mon, 23 Feb 2015 11:17:39 +0100
changeset 26041 d061c9e837aa
parent 26040 1bddb73d8788
child 26042 b699e6284720
fixed bug 28712: record selection lost in p&e grid if data has multiple pages

In P&E grid with data displayed in several pages, if part of the selection was
not in the 1st page two errors occured:

-If after selecting a record in 2nd page another one in the 1st page was selected,
selection of record in 2nd page was lost
-If record in 2nd page was selected by using a filter, the filter was removed, and
the p&e was submitted, the selection was not sent to backend

The problem is in SC grid.getSelectedRecords which returns records only in current
page.

To solve this a cache of selected/unselected records is completelly maintained for p&e
grids without using getSelectedRecords, this is used also to send the selected recors
on p&e submission.
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-pickeditgrid.js
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/process/ob-pick-and-execute-grid.js
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-pickeditgrid.js	Mon Feb 23 04:09:31 2015 +0000
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-pickeditgrid.js	Mon Feb 23 11:17:39 2015 +0100
@@ -11,7 +11,7 @@
  * under the License.
  * The Original Code is Openbravo ERP.
  * The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2014 Openbravo SLU
+ * All portions are Copyright (C) 2014-2015 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -95,7 +95,10 @@
     var allProperties = {},
         grid = this.canvas.viewGrid,
         allRows, len, i, selection, tmp;
-    selection = grid.getSelectedRecords() || [];
+
+    // if available, use grid.pneSelectedRecords because getSelectedRecords can
+    // return inaccurate values in case of records selected in different pages
+    selection = grid.pneSelectedRecords || grid.getSelectedRecords() || [];
     len = selection.length;
     allRows = grid.data.allRows || grid.data.localData || grid.data;
     allProperties._selection = [];
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/process/ob-pick-and-execute-grid.js	Mon Feb 23 04:09:31 2015 +0000
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/process/ob-pick-and-execute-grid.js	Mon Feb 23 11:17:39 2015 +0100
@@ -11,7 +11,7 @@
  * under the License.
  * The Original Code is Openbravo ERP.
  * The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2011-2014 Openbravo SLU
+ * All portions are Copyright (C) 2011-2015 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -76,6 +76,7 @@
 
     this.selectedIds = [];
     this.deselectedIds = [];
+    this.pneSelectedRecords = [];
     this.lastValidatedValues = [];
 
     // the getValuesAsCriteria function of the edit form of the filter editor should always be called with 
@@ -308,23 +309,33 @@
       this.discardEdits(recordIdx);
     }
 
-    this.selectionUpdated(record, this.getSelectedRecords());
+    this.pneSelectionUpdated(record, state);
 
     this.Super('selectionChanged', arguments);
     this.view.theForm.markForRedraw();
   },
 
-  selectionUpdated: function (record, recordList) {
-    var i, j, len = recordList.length,
-        prevSelectedLen = this.selectedIds.length,
-        recordId, found;
+  // A new record has been selected/unselected: keep track of it.
+  // this.getSelectedRecords cannot be trusted because in case of several pages,
+  // selection only in latest received page is returned
+  pneSelectionUpdated: function (record, selected) {
+    var recordId = record.id,
+        found, i;
 
-    // Look for deselected records (records in selectedIds not present in recordList)
-    for (i = 0; i < prevSelectedLen; i++) {
-      recordId = this.selectedIds[i];
+    if (selected) {
+      if (!this.pneSelectedRecords.find('id', recordId)) {
+        // this method can be invoked more than once per selection, ensure we only 
+        // add the record once
+        this.selectedIds.push(recordId);
+        this.pneSelectedRecords.push(record);
+      }
+      this.deselectedIds.remove(recordId);
+    } else {
+      // this method can be invoked more than once per selection, ensure we only 
+      // add the record once: can't use find on a simple array, let's iterate over it
       found = false;
-      for (j = 0; j < len; j++) {
-        if (recordId === recordList[j].id) {
+      for (i = 0; i < this.deselectedIds.length; i++) {
+        if (recordId === this.deselectedIds[i]) {
           found = true;
           break;
         }
@@ -332,19 +343,12 @@
       if (!found) {
         this.deselectedIds.push(recordId);
       }
+      this.selectedIds.remove(recordId);
+      this.pneSelectedRecords.remove(this.pneSelectedRecords.find('id', recordId));
     }
 
-    this.selectedIds = [];
-
-    for (i = 0; i < len; i++) {
-      this.selectedIds.push(recordList[i].id);
-      // Remove the record from deselectedIds
-      this.deselectedIds.remove(recordList[i].id);
-    }
     // refresh it all as multiple lines can be selected
     this.markForRedraw('Selection changed');
-
-    this.Super('selectionUpdated', arguments);
   },
 
   cellEditEnd: function (editCompletionEvent, newValue, ficCallDone, autoSaveDone) {