Announcement

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

    ComboBoxItem.getSelectedRecord() with valueField duplicates

    ComboBoxItem.getSelectedRecord() does not return correct record when valueField contains duplicate entries.
    I am using SmartGwt Pro 6.3.2013, but the issue dates back to SmartGwt 2.2. The use case is that we are using an optionDataSource to look up a user. This user may have several entries if he possesses several roles. We wish to populate secondary formfields with values from the selected record pertaining to roles. Even if the optiondataSource has a unique primary key, the getSelectedRecord() on the ComboBoxItem will always return the first entry in the picklist matching the valuefield chosen.

    I have attached a small sample to reproduce the issue.

    I have also coded a workaround in jsni by overriding ComboBoxItem.getSelectedRecord(), but I am uncomfortable with this solution, as it may break at any time. Would you consider including something along the line of the following if it does not break any other functionality?

    Code:
    	@Override
    	public ListGridRecord getSelectedRecord() {
    		if(JSOHelper.getAttribute(this.getJsObj(),"pickList") == null) {
    			// Prevent premature fetch if picklist has not been opened
    			return null;
    		} else {
    			return jsniGetSelectedRecord();
    		}
    	}
    	
        public native ListGridRecord jsniGetSelectedRecord() /*-{
        var self = this.@com.smartgwt.client.core.DataClass::getJsObj()();
        var ret = null;
        if(self._value != null && self.optionDataSource) {
        	if(self.pickList && self.pickList.data) {
        		ret = self.pickList.getSelectedRecord();
      			if (ret != null) {
      				if (ret.valueField != self._value) {
    // $wnd.isc.ComboBoxItem.logWarn("Stale selectedRecord on " + self.getID() + " Record valueField:"  + ret.valueField + " Item value:" + self._value);
      					ret = null;
      				}
      			}
        	}
        }
        if(ret == null || ret === undefined) return null;
        var retVal = @com.smartgwt.client.core.RefDataClass::getRef(Lcom/google/gwt/core/client/JavaScriptObject;)(ret);
        if(retVal == null) {
            retVal = @com.smartgwt.client.widgets.grid.ListGridRecord::new(Lcom/google/gwt/core/client/JavaScriptObject;)(ret);
        }
        return retVal;
    }-*/;
    Regards
    Hans Petter Simonsen - Evry Norway
    Attached Files

    #2
    The valueField is required to be unique. We are not saving a PK value as the value of the FormItem, we're saving the valueField value. We do not try to track the PK value as an additional temporary value for disambiguation.

    Since you have a PK, you can just make the valueField be the PK.

    Note, your JSNI override doesn't really make sense - it doesn't address the problem you've described.

    Comment


      #3
      Hi Isomorphic,
      I think you may be misunderstanding, since neither this issue nor the solution depends on a primary key being present (or tracked).
      The actual selected record in the picklist is available in javascript pickList.getSelectedRecord(), but I think the actual implementation of the ComboBoxItem.getSelectedRecord() uses pickList.data.find() with the value as a key.
      You will surely see if you care to try it that my solution solves the problem by returning the actual record clicked in the picklist, as opposed to the present implementation, not requiring neither a primary key, nor uniqueness of values.

      Comment


        #4
        The valueField must be unique, that's why the current implementation uses find(), because that's correct given the constraint that the valueField is unique.

        Your attempted fix is not correct.

        First of all the overall approach would only work if data happened to be fully loaded.

        Second the code does not make sense unless your valueField is actually named "valueField" (record.valueField should be record[valueField] to access an arbitrarily-named field).

        So again, just fulfill the constraint that valueField must be unique - an attempted "patch" here is not going to do what you expect, even if you correct your code.

        Comment


          #5
          Thank you for your correction to access arbitrary valueField, Isomorphic. So far, my solution works as expected, and the selected record will always be loaded, or you could not select it.
          I rest my case.

          Comment


            #6
            ??

            Your solution was also "working as expected" before when the code was nonsense!

            In case anyone else comes across this thread: do not use this user-provided code sample, it does not succeed in allowing the valueField to be non-unique, it just hacks one case and breaks elsewhere.

            Instead, make sure the valueField is unique.

            Comment

            Working...
            X