Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Sort specifiers removed by update on a ListGrid DataSource sorted on a datetime field

    Hi

    I get an issue by using smartgwt pro 4.1 (nightly build (v9.1p_2015-03-24/Pro Deployment 2015-03-24)). Actually I am not quite sure if this is a bug or a feature :).

    I have a data bound ListGrid in paging mode sorted on a datetime field.
    The ListGrid get unsorted (the sort specifiers are deleted) after an update or add operation on the ListGrid DataSource.
    Is this deliberate?

    The sort specifiers retain after update or add operation when sorting on fields of type text or long.
    I would like keeping the sort specifiers when sorting on fields of any type. Is this possible?

    This occurs not only in that nightly build.

    I would appreciate if someone checks this.

    Ts. Vasilev

    #2
    The fact that the grid unsorts after a record is updated or added is intentional, and is part of a behavior to try to keep records in the viewport if the user just modified them - see ResultSet.updatePartialCache.

    This doesn't drop the sort specifiers, it just stops applying them temporarily. Calling resort() re-applies them.

    If you're seeing behavior that seems to disagree with the above, we'll need a minimal, ready-to-run test case, then we can take a look.

    Comment


      #3
      Thanks for the reply.

      here is a sample application that illustrates the problem. I am seeing the issue with SmartGWT 4.1 Pro (v9.1p_2015-06-05/Pro Deployment built 2015-06-05).

      Steps to reproduce the behaviour using the supplied test case:

      The ListGrid is initialized with sorting on a Datetime field (nextShipment). Select any row and click on the "Change Units" button to update the data. This updates the record and preserves the SortSpecifiers which is the behaviour we want. The same behaviour can be observed if the grid is sorted on a TextField.

      Next, use the DynamicForm to update the Units field of some record in the ListGrid, with the grid sorted by a Datetime field. This time, the update operation will cause the grid to lose its sort specifiers. However, if the grid is sorted by a text/numeric/boolean field, the sort specifiers are preserved.

      Code:
      package com.smartgwt.sample.client;
      
      import java.util.ArrayList;
      import java.util.Arrays;
      import java.util.HashSet;
      import java.util.Set;
      
      import com.google.gwt.core.client.EntryPoint;
      import com.google.gwt.user.client.Timer;
      import com.smartgwt.client.data.DataSource;
      import com.smartgwt.client.data.SortSpecifier;
      import com.smartgwt.client.types.SortDirection;
      import com.smartgwt.client.widgets.IButton;
      import com.smartgwt.client.widgets.Label;
      import com.smartgwt.client.widgets.events.ClickEvent;
      import com.smartgwt.client.widgets.events.ClickHandler;
      import com.smartgwt.client.widgets.form.DynamicForm;
      import com.smartgwt.client.widgets.grid.ListGrid;
      import com.smartgwt.client.widgets.grid.ListGridRecord;
      import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
      import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
      import com.smartgwt.client.widgets.grid.events.ViewStateChangedEvent;
      import com.smartgwt.client.widgets.grid.events.ViewStateChangedHandler;
      import com.smartgwt.client.widgets.layout.HLayout;
      import com.smartgwt.client.widgets.layout.VLayout;
      
      public class ListGridTest implements EntryPoint {
          
          @Override
          public void onModuleLoad() {
                
                final CustomLabel activeSortLabel = new CustomLabel();
                
                final DataSource supplyItemDS = ItemSupplyXmlDS.getInstance(); 
                  
                final Set<String> unitValues = new HashSet<String>(Arrays.asList("Roll", "Ea", "Pkt", "Set", "Tube", "Pad", "Ream", "Tin", "Bag", "Ctn", "Box"));
                
                // dynamic form 
                final DynamicForm updateForm = new DynamicForm();
                updateForm.setIsGroup(true);
                updateForm.setGroupTitle("Update Grid");
                updateForm.setDataSource(supplyItemDS);
                updateForm.setNumCols(6);
                updateForm.setHeight(200);
                
                // list grid
                final ListGrid testGrid = new ListGrid();
                testGrid.setWidth(800);
                testGrid.setHeight(200);
                testGrid.setDataSource(supplyItemDS);
                testGrid.setAutoFetchData(true);
                testGrid.setDataPageSize(10); 
                
                testGrid.setInitialSort(new SortSpecifier[]{  
                        new SortSpecifier("nextShipment", SortDirection.DESCENDING)  
                });
                
                testGrid.addRecordClickHandler(new RecordClickHandler() {
      
                  @Override
                  public void onRecordClick(RecordClickEvent event) {
                      updateForm.reset();
                      updateForm.editSelectedData(testGrid);
                  }
                    
                });
                
                activeSortLabel.updateContents(testGrid.getSort());
                
                testGrid.addViewStateChangedHandler(new ViewStateChangedHandler() {
      
                  @Override
                  public void onViewStateChanged(ViewStateChangedEvent event) {
                      activeSortLabel.updateContents(testGrid.getSort());     
                  }
                    
                });
                
      
                IButton updateButton = new IButton("Update Record");
                updateButton.setWidth(120);
                updateButton.addClickHandler(new ClickHandler() {
      
                  @Override
                  public void onClick(ClickEvent event) {         
                      updateForm.saveData();
                  }
                });
                
                
                IButton sortByItem = new IButton("Sort by Item");
                sortByItem.setWidth(120);
                sortByItem.addClickHandler(new ClickHandler() {
      
                  @Override
                  public void onClick(ClickEvent event) {
                      
                      testGrid.setSort(new SortSpecifier[]{  
                              new SortSpecifier("itemName", SortDirection.ASCENDING)  
                      });
      
                  }
                    
                });
                
                IButton sortByShipmentDate = new IButton("Sort by Shipment Date");
                sortByShipmentDate.setWidth(120);
                sortByShipmentDate.addClickHandler(new ClickHandler() {
                    
                    @Override
                    public void onClick(ClickEvent event) {
                        
                        testGrid.setSort(new SortSpecifier[]{  
                                new SortSpecifier("nextShipment", SortDirection.DESCENDING)  
                        });
                        
                    }
                });
                
                IButton changeUnitsButton = new IButton("Change Units");  
                changeUnitsButton.addClickHandler(new ClickHandler() { 
                    
                    public void onClick(ClickEvent event) {
                        
                        ListGridRecord selectedRecord = testGrid.getSelectedRecord();
                        
                        if(selectedRecord != null) {  
                            
                            ListGridRecord newRecord = new ListGridRecord();  
                            newRecord.setAttribute("itemID", selectedRecord.getAttribute("itemID"));  
                            
                            String oldUnit = selectedRecord.getAttribute("units");
                            
                            Set<String> notSelected = new HashSet<String>(unitValues);
                            
                            if (oldUnit != null && !oldUnit.isEmpty()) {
                                Set<String> currentValue = new HashSet<String>();
                                currentValue.add(oldUnit);
                                notSelected.removeAll(currentValue);
                            } 
                             
                            String newUnit = new ArrayList<String>(notSelected).iterator().next();
                            newRecord.setAttribute("units", newUnit);  
                            
                            testGrid.updateData(newRecord);
                            
                            new Timer() {  
                                public void run() {  
                                   updateForm.reset();
                                   updateForm.editSelectedData(testGrid);   
                                }  
                            }.schedule(100);
                        }  
      
                    }             
                });
                
            
                HLayout sortButtonsLayout = new HLayout(10);
                sortButtonsLayout.addMember(sortByItem);
                sortButtonsLayout.addMember(sortByShipmentDate);
                sortButtonsLayout.addMember(activeSortLabel);
                
                VLayout listGridLayout = new VLayout(10);
                listGridLayout.addMember(testGrid);
                listGridLayout.addMember(changeUnitsButton);
                
                VLayout dynFormLayout = new VLayout(10);
                dynFormLayout.addMember(updateForm);
                dynFormLayout.addMember(updateButton);
                dynFormLayout.setWidth(280);
           
                HLayout compLayout = new HLayout(5);
                compLayout.addMember(listGridLayout);
                compLayout.addMember(dynFormLayout);
                        
                VLayout mainLayout = new VLayout(10);
                mainLayout.addMember(sortButtonsLayout);
                mainLayout.addMember(compLayout);
                 
                mainLayout.setLeft(10);  
                mainLayout.setTop(10);  
                mainLayout.draw();
                         
          }
          
          
          class CustomLabel extends Label {
              
              CustomLabel () {
                  setContents("");
                  setWidth(200);  
                  setHeight(20);
                  setPadding(2);
                  setBackgroundColor("ffffff");
                  setBorder("1px solid #6a6a6a");
              }
              
              public void updateContents(final SortSpecifier[] sortSpecifiers) {
                  
                  final String sortSpec = sortFieldsToString(sortSpecifiers);
                  
                  if (sortSpec != null && !sortSpec.isEmpty()) {
                      setContents("<b>Sort by:</b> " + sortSpec);
                      setBackgroundColor("ffffff");
                  } else {
                      setContents("<b>No sorting specified!</b>");
                      setBackgroundColor("ff0000");
                  }
                  
              }
              
              public String sortFieldsToString(final SortSpecifier[] sortSpecifiers) {
                  
                  StringBuffer sb = new StringBuffer();
                  
                  if (sortSpecifiers != null) {
                      for (SortSpecifier ss : sortSpecifiers) {
                          sb.append(ss.getField() + " ");
                      }
                  } 
                  
                  return sb.toString(); 
              }
                
          }
          
      }
      As data source, I slightly modified ds/test_data/supplyItem.data.xml by adding date values to most of the nodes, but the behaviour is also reproducible with the stock file from the Showcase.

      While preparing this post I noticed this thread which looks very much like describing the same bug. Will the fix also be backported to 4.1p? I can not migrate to SmartGWT 5.0 just yet.

      Best regards

      Comment


        #4
        This was fixed recently in later versions, and we've just back-ported that fix to 4.1 today - please retest with a build dated June 11 or later.

        Comment


          #5
          I have just tested with the latest nightly SmartGWT 4.1 Pro (v9.1p_2015-06-11/Pro) and the issue has been fixed.

          Thank you!

          Comment

          Working...
          X