Announcement

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

    Problem with ComboBoxItem, empty string descriptions & ChangedHandlers

    Problem with ComboBoxItem and empty string descriptions - ChangedHandler is being called early.


    I have included an example of this -

    The first drop down just changes a description field on a record you select to “” … if this isn’t done the problem doesn’t show up.

    The second drop down box has a changedHandler …. and it is fired [early] as soon as it is dropped down - you do not need to select a record first!


    Note: it doesn’t need to be changed to an empty string in the same session … the data can already be like that …. it just made it a bit easier (I think) to demonstrate it in my test case.


    In an ideal world I wouldn’t have datasets with empty strings present - but sadly I do do at the moment in some places.


    The file is designed to just overwrite the BuiltInDS.java file in the BuiltInDS sample and work with no other changes.

    Code:
    Code:
    package com.smartgwt.sample.client;
    
    import com.google.gwt.core.client.EntryPoint;
    import com.smartgwt.client.data.DataSource;
    import com.smartgwt.client.util.SC;
    import com.smartgwt.client.widgets.form.DynamicForm;
    import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
    import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
    import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
    import com.smartgwt.client.widgets.grid.ListGridRecord;
    
    public class BuiltInDS implements EntryPoint {
    
    	@Override
    	public void onModuleLoad() {
    		final DynamicForm form = new DynamicForm();
    		form.setWidth(500);
    
    		final DataSource ds = DataSource.get("supplyItem");
    		
    		final ComboBoxItem comboBoxItem = new ComboBoxItem();
    		comboBoxItem.setName("filteredCombo");
    		comboBoxItem.setAddUnknownValues(false);
    		comboBoxItem.setOptionDataSource(ds);
    		comboBoxItem.setDisplayField("description");
    		comboBoxItem.setValueField("itemID");
    		comboBoxItem.addChangedHandler(new ChangedHandler() {
    			@Override
    			public void onChanged(ChangedEvent event) {
    				ListGridRecord rec = comboBoxItem.getSelectedRecord();
    				rec.setAttribute("description", "");
    				ds.updateData(rec);
    			}
    		});
    
    		ComboBoxItem comboBox2 = new ComboBoxItem();
    		comboBox2.setName("filteredCombo2");
    		comboBox2.setTitle("Choose an item (ComboBox)");
    		comboBox2.setAddUnknownValues(false);
    		comboBox2.setOptionDataSource(ds);
    		comboBox2.setDisplayField("description");
    		comboBox2.setValueField("itemID");
    		comboBox2.addChangedHandler(new ChangedHandler() {
    			@Override
    			public void onChanged(ChangedEvent event) {
    				SC.say("Changed!");
    			}
    		});
    
    		form.setFields(comboBoxItem, comboBox2);
    
    		form.draw();
    	}
    
    }

    My version is: v10.0p_2014-10-01/Pro Deployment (built 2014-10-01)

    Version 4.1 [Pro/2014-10-04] does not show this behaviour.




    Thank-you very much in advance.

    #2
    The problem here appears to be that you are directly modifying a cached record (your setAttribute() call), which is incorrect usage.

    Try changing the code to instead create a new Record and set just the primaryKey and changed values on that Record and you should see the problem go away.

    Comment


      #3
      Thanks for such a quick reply.

      Sorry about the incorrect demonstration - I was trying to make it easily demonstrable in a single file and I am still learning ;-).


      I have included another example.. with a single comobox.


      Though after deploying the war this requires you to load an sql editor:


      java -jar sqltool.jar --inlineRc "URL=jdbc:hsqldb:hsql://localhost/isomorphic"


      And run the SQL command:

      Code:
      UPDATE animals SET COMMONNAME = '' WHERE SCIENTIFICNAME = 'Pongo pygmaeus';
      COMMIT;

      Code:
      package com.smartgwt.sample.client;
      
      import com.google.gwt.core.client.EntryPoint;
      import com.smartgwt.client.data.DataSource;
      import com.smartgwt.client.util.SC;
      import com.smartgwt.client.widgets.form.DynamicForm;
      import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
      import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
      import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
      
      public class BuiltInDS implements EntryPoint {
      
      	@Override
      	public void onModuleLoad() {
      		final DynamicForm form = new DynamicForm();
      		form.setWidth(500);
      
      		final DataSource ds = DataSource.get("animals");
      
      		ComboBoxItem comboBox2 = new ComboBoxItem();
      		comboBox2.setName("filteredCombo2");
      		comboBox2.setTitle("Choose an item (ComboBox)");
      		comboBox2.setAddUnknownValues(false);
      		comboBox2.setOptionDataSource(ds);
      		comboBox2.setDisplayField("commonName");
      		comboBox2.setValueField("scientificName");
      		comboBox2.addChangedHandler(new ChangedHandler() {
      			@Override
      			public void onChanged(ChangedEvent event) {
      				SC.say("Changed!");
      			}
      		});
      
      		form.setFields(comboBox2);
      
      		form.draw();
      	}
      
      }




      ** OR **

      To save going into a command line editor you could also add the button bellow:

      Code:
      		ButtonItem btn = new ButtonItem();
      		btn.setTitle("Add new");
      		btn.addClickHandler(new ClickHandler() {
      			@Override
      			public void onClick(ClickEvent event) {
      				Record record = new Record();
      				record.setAttribute("scientificName", "hedgehog");
      				record.setAttribute("commonName", "");
      				ds.addData(record);
      			}
      		});
      Would that be to correct usage to add a simple record ? or is setAttribute the wrong method ?

      If after pushing the button you refresh the whole page - before dropping down the combo - the problem still presents.

      Thank-you for your assistance.

      Comment


        #4
        This is a slightly tricky situation as our logic attempts to map the user-entered value to the display value for the matching record to handle the case where the user types a full value rather than selecting a value from the pickList.
        However we agree that this behavior doesn't make sense in this case where it has the potential to unexpectedly populate what was an empty item, so we've made some changes to special case the empty string and avoid this.

        Please try the next nightly build, dated Oct 11 or above (5.0 or 5.1 branch)

        Regards
        Isomorphic Software

        Comment


          #5
          Thank-you very much - that seems to have fixed my problem. :-)

          Comment

          Working...
          X