Announcement

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

    Setting databound selectedItem value

    Hi,

    Is there a simple way to programmatically set the selected index of a SelectItem?

    Currently I have (as I mentioned elsewhere before) a ListGrid and SelectItem bound to the same datasource. The only problem I'm having at the moment is that if the record that is selected in the SelectItem is removed from the ListGrid and hence the DataSource then the 'String' value for that record remains as the selected item in the SelectItem. The record itself, is of course removed so
    Code:
    selectItem.getSelectedRecord()
    returns null but
    Code:
    selectItem.getValue()
    returns a String representing that value.

    What I would really like to happen is when a value is removed from the ListGrid, if it is the currently selected value in the SelectItem then the first (non-null) item of the SelectItem should be selected instead. Normally I would do this in Java with a call to
    Code:
    setSelectedIndex(0)
    but there doesn't appear to be anything similar in SmartGWT.

    I am currently trying out various approaches to this within a DataArrivedHandler in the SelectItem where event.getData() returns the correct number of results.

    #2
    I fixed the inherent problem with this code within the DataArrivedHandler:
    Code:
    public void onDataArrived(DataArrivedEvent event){
      if(selectItem.getSelectedRecord() == null || !event.getData().contains(selectItem.getSelectedRecord())){
        Record first = event.getData().first();
        if(first != null)
          selectItem.setValue(first.getAttributeAsString("Name"));
        else
          selectItem.setValue("");
      }
    }
    However, the underlying problem remains, I am also attempting to store the value in the selectItem which I can do just fine by storing the data within selectItem.getSelectedRecord() but when loading this data back in simply calling
    Code:
    selectItem.setValue(record.getAttributeAsString("Name"));
    does not change the selected value of the SelectItem from the default (the value returned by record.getAttributeAsString("Name") is correct however).

    It seems somewhat counter-intuitive that the databound records are so separate from the values concerned in getValue() / setValue().

    Comment


      #3
      Can you clarify what you mean here:

      However, the underlying problem remains, I am also attempting to store the value in the selectItem which I can do just fine by storing the data within selectItem.getSelectedRecord() but when loading this data back in simply calling

      Code:

      selectItem.setValue(record.getAttributeAsString("Name"));


      does not change the selected value of the SelectItem from the default (the value returned by record.getAttributeAsString("Name") is correct however).
      We're having trouble following what's happening for you - are you saying that if you call setValue() on a selectItem, the setValue() fails (the item value doesn't change)?
      If so can you show us a test case demonstrating this?

      Comment


        #4
        Ok, so it appears the problem here was that I had an asynchronous rpc call in an override of show() that called setValue() (correctly) but then when the data arrived, the DataArrivedHandler kicked in and overwrote that data with the first value in the list for each piece of data that came back.

        It would be useful if I didn't have to use the DataArrivedHandler in the first place to work around what I believe to be a bug. I've written a standalone test case for this here: http://pastebin.com/h3PFHA4C. I tried to keep it as concise as possible.

        Use case to reproduce the bug:
        1. Click 'show dialog' button.
        2. Drag 2 or more values from the left ListGrid to the right.
        3. Select a record in the SelectItem.
        4. Select that same record in the right ListGrid
        5. Click 'Remove' button.

        Result:
        Record is removed from the DataSource, the ListGrid and partially from the SelectItem but the selected 'String' value remains in the SelectItem until it is manually changed.

        Comment

        Working...
        X