Announcement

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

    #16
    Hi there.
    When I use comboBoxItem.setValue("value");
    the component appears ok on the browser but the getSelectedRecord returns null.
    If the user clicks on the combo it starts to work fine.
    The code below is an example.

    Code:
    	public void onModuleLoad() {
    		RootPanel panel = RootPanel.get();
    		Cidade cidade = new Cidade();
    		cidade.setNome("Araguari");
    		cidade.setId(1);
    		
    		ComboBoxItem cidadeCombo = new ComboBoxItem();
    		CidadeSuggestDS cidadeDataSource = new CidadeSuggestDS();
    		cidadeCombo.setOptionDataSource(cidadeDataSource);
    		ListGridRecord gr = new ListGridRecord();
    		gr.setAttribute("nome", cidade.getNome());
    		gr.setAttribute("chave", cidade.getId());
    		gr.setAttribute("municipio", cidade);
    		cidadeDataSource.addData(gr);
    		
    		cidadeCombo.setValue(cidade.getNome());
    		
    		DynamicForm form = new DynamicForm();
    		form.setItems(cidadeCombo);
    		panel.add(form);
    		if(cidadeCombo.getSelectedRecord()==null){
    			GWT.log("null");
    		}else{
    			GWT.log("not null");
    		}
    	}
    Code:
    public class CidadeSuggestDS extends BaseSuggestDS{
    	
    	public CidadeSuggestDS(){
    		 DataSourceTextField nameField = new DataSourceTextField("nome", "Nome");  
    		 nameField.setPrimaryKey(true);
    		 nameField.setCanFilter(true);
    
    		 setFields(nameField);  
    	     setClientOnly(true);    
    	}
    }
    when the user opens de module GWT.log shows null.
    If you click the combo it starts working.

    Am I missing something? Is this a bug?

    Comment


      #17
      Hi all
      A couple of quick updates:

      Lucasam - The issue here is one of timing -- the ComboBoxItem is fetching it's set of picklist values (possible records) from the dataSource. That's an asynchronous fetch.
      Until the fetch completes item.getSelectedRecord() won't get back a record from the DS.
      If you call item.getSelectedRecord() after the fetch has completed (you could use the dataArrivedHandler for this if you like), the record should be present.

      ect: on the code snippet you posted above - the behavior seems to be that each time you select a record from the pick list, the selected record itemID logged is one behind the current selection. This is basically a result of the blur handler firing before the value for the item has been updated -- the best way to write code to respond to a user updating the value is to add it to the changed handler - if you make that change you should see the correct itemID showing up.

      Thanks
      Isomorphic Software

      Comment


        #18
        ComboBoxItem.getSelectedRecord returning null

        Originally posted by Isomorphic View Post
        Hi all
        he best way to write code to respond to a user updating the value is to add it to the changed handler - if you make that change you should see the correct itemID showing up.

        Thanks
        Isomorphic Software
        I am using SmartGWT 3.1-p20130226 and GWT 2.5.0.

        I was trying to understand why my call to ComboBoxItem.getSelectedRecord was returning a null. I found this thread that had discussion around the topic so hopefully this is a good place to post this.

        I am calling ComboBoxItem.getSelectedRecord from the change handler and I'm still getting null.

        Here is the snippet of code I'm using for ComboBoxItem

        Code:
        final ComboBoxItem accountSelection = new ComboBoxItem();
        accountSelection.setAutoFetchData(true);
        accountSelection.setValueField("ID");
        accountSelection.setDisplayField("Name"); 
        accountSelection.setTitle("Select a Different Account");
        accountSelection.setOptionDataSource(DataSource.get("accounts"));
        accountSelection.addChangeHandler(new ChangeHandler() {
            public void onChange(ChangeEvent event) {
                Logger.log(this, "account selector event");
                Logger.log(this, "event.getValue()="+event.getValue());
                ListGridRecord record = accountSelection.getSelectedRecord();
               if (record == null)
                   Logger.log(this, "selected record is null");
               else
                   Logger.log(this, "selected record is not null");
            }
        }
        });
        When I select an item in the ComboBoxItem from UI the change handler fires but getSelectedRecrod returns null.

        The Logger.log method prints the log message to the SmartGWT log prepended by the class name.

        Here is the result found in the log:

        Code:
        15:56:57.452:IFCS6:INFO:Log:MVP1: viewer.forms.Items$2$1: account selector event
        15:56:57.453:IFCS6:INFO:Log:MVP1: viewer.forms.Items$2$1: event.getValue()=9
        15:56:57.454:IFCS6:INFO:Log:MVP1: viewer.forms.Items$2$1: selected record is null
        Hopefully you can see that the change event is firing and my code is getting control. I'm able to get the value (which is the PK) but the select record is null.

        I'm read through the JavaDoc on ComboBoxItem and so far I don't seem to understand what is needed to get the selected record. I need this record because it has the full information about this particular object/record that will be displayed elsewhere in my application.

        For completeness, here is the data source XML file for the accounts table:

        Code:
        <!-- Auto-generated from database table accounts -->
        
        <DataSource 
        	dbName="Mysql"
        	tableName="accounts"
        	ID="accounts"
        	dataSourceVersion="1"
        	generatedBy="v8.3p_2013-02-26/EVAL Deployment 2013-02-26"
        	serverType="sql"
        >
        	<fields>
        		<field primaryKey="true" name="ID" type="sequence"></field>
        		<field name="UserID" type="integer"></field>
        		<field name="Name" length="45" type="text"></field>
        		<field name="DateCreated" type="datetime"></field>
        		<field name="LastModified" type="datetime"></field>
        	</fields>
        </DataSource>
        What code am I missing that will allow me to get the full record of the ComboBoxItem selection?

        Comment


          #19
          You probably want the ChangedEvent not ChangeEvent. ChangeEvent is cancellable, so at the moment it fires, the new value has not become the official value of the FormItem yet.

          Sorry for the confusion, we did mention Change rather than Changed before.

          Comment


            #20
            what a difference a character makes

            Wow, that was subtle. I obviously didn't know the difference between the two types of handlers. After reading the documentation the difference is quite clear.

            Thank you for your fast and clear reply. I've been working on this for hours.

            I'll focus on RTFM (reading the fine manual). However, sometimes its a needle in a haystack!

            Thanks again.

            Comment

            Working...
            X