Announcement

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

    FormItemValueFormatter getSelectedRecord behavior

    hi Isomorphic,

    After the update smartgwt from 2.5 to 3.1, I have encountered a different behavior at FormItemValueFormatter - getSelectedRecord.

    The function getSelectedRecord returns a record although none has been selected.

    In smartgwt version 2.5, no text will be displayed initially.

    To reproduce the problem, please paste the code in the test program buildinds.
    Code:
            DynamicForm animaldynamicForm = new DynamicForm();
        	final SelectItem animalsSel = new SelectItem("animalsSel");
        	animalsSel.setTitle("Animals Selection");
        	animalsSel.setWidth(200);
        	animalsSel.setOptionDataSource(DataSource.get("animals"));
        	animalsSel.setPickListFields(new ListGridField("lifeSpan"), new ListGridField("diet"));
        	animalsSel.setValueFormatter(new FormItemValueFormatter() {
    			
    			@Override
    			public String formatValue(Object value, Record record, DynamicForm form, FormItem item) {
    				if (item.getSelectedRecord() != null){
    					return item.getSelectedRecord().getAttribute("lifeSpan") + " (" + item.getSelectedRecord().getAttribute("diet") + ")" ;
    				}
    				return null;
    			}
    		});
    
        	animaldynamicForm.setItems(animalsSel);
        	animaldynamicForm.draw();
    Used:
    OS: MacOS 10.8
    Browser: FF 9
    smartgwt: SNAPSHOT_v8.3d_2012-10-10/PowerEdition Deployment 2012-10-10
    gwt: 2.4

    best regards,
    timo

    #2
    Hi Timo
    The reason this isn't working as expected is that you're usage is quite unorthodox.
    It's best to think of the "selected" record as the record for which the value-field value matches the current value for the item.

    You have an optionDataSource specified for your supplyItem but the field name (animalsSel) isn't a field in that dataSource, and you have no explicitly specified valueField for the item.

    This means that getSelectedRecord() is going to be trying to find a record where the field value for "animalsSel" matches the current value of the item. Since that value is unset on all records in the dataSource, it is returning the first record in the data set as a match for the item having no specified value.
    This behavior is not really incorrect.

    You should be able to fix this easily by specifying an explicit valueField for your item. You should set it to the item you want to extract the record value from on selection, or if you're doing something custom to get values back, at least specify a unique field, such at the Primary key field for the dataSource.

    Comment


      #3
      hi Isomorphic,

      perfect, thanks for the detailed explanation!!
      cheers,
      timo

      Comment

      Working...
      X