Announcement

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

    Advise for databound ComboBoxItem with additional Criteria

    Hi Isomorphic,

    could you advise how to implement the following:
    - DataBound Country-Dropdown, valueField=country_id (easy, prerequisite)
    - DataBound Zipcode-Dropdown, where the selected item of the Country-Dropdown should be an additional criteria to the entered text in the Zipcode-Dropdown.

    I did the following, as this was the only way I could inject the criteria:
    Code:
    final ComboBoxItemZipcode zipcodeCBIZ = new ComboBoxItemZipcode("ZIPCODE");
    zipcodeCBIZ.setAddUnknownValues(true);
    zipcodeCBIZ.addChangedHandler(new ChangedHandler() {
    	@Override
    	public void onChanged(ChangedEvent event) {
    		Criterion currentCountryCriteria = new Criterion("COUNTRY_ID", OperatorId.EQUALS,
    				countryCBIC.getValueAsString());
    		Criterion currentZipcodeCriteria = new Criterion("ZIPCODE", OperatorId.STARTS_WITH,
    				zipcodeCBIZ.getValueAsString());
    		AdvancedCriteria ac = new AdvancedCriteria();
    		ac.buildCriterionFromList(OperatorId.AND, new Criterion[] { currentCountryCriteria,
    				currentZipcodeCriteria });
    		zipcodeCBIZ.setOptionCriteria(ac);
    //		zipcodeCBIZ.getOptionDataSource().invalidateCache();
    //		zipcodeCBIZ.invalidateDisplayValueCache();
    	}
    });
    When you look at the server-log now, there are always 3(!) criteria:

    Code:
    === 2013-01-09 12:17:30,678 [c-10] INFO  SQLDataSource - [builtinApplication.fetchSuggestionList] Performing fetch operation with
    	criteria: {criteria:[{fieldName:"COUNTRY_ID",operator:"equals",value:"246"},{fieldName:"ZIPCODE",operator:"startsWith",value:"123"},{fieldName:"ZIPCODE",operator:"iContains",value:"123"}],operator:"and",_constructor:"AdvancedCriteria"}	values: {criteria:[{fieldName:"COUNTRY_ID",operator:"equals",value:"246"},{fieldName:"ZIPCODE",operator:"startsWith",value:"123"},{fieldName:"ZIPCODE",operator:"iContains",value:"123"}],operator:"and",_constructor:"AdvancedCriteria"}
    1x ZIPCODE.startsWith, ZIPCODE.iContains, COUNTRY_ID.equals. I would't care for the iContains, which is added automatically, even though I set "setTextMatchStyle(TextMatchStyle.STARTS_WITH);" (see class code below), but what also happens is that the suggestion box doesn't show values it should show.
    Example: I enter "12", suggestion-list is 12043, 12045, 12047, 12049, 12051, 12053, 12055, 12057, 12059, 12099, 12101, 12103, 12105, ....
    when I enter an additional "0" afterwards, the list gets empty, even though there should be data shown. An additional DB-fetch is not issued. I tried around with invalidateCache (see code above), but it didn't work out, either (still no fetch!).

    It gets even worse with setAddUnknownValues(false), because then the ChangedHandler isn't called anymore (which is correct according to the docs http://www.smartclient.com/smartgwte...wnValues%28%29, but then my check for the selected country isn't possible at all.

    Could you advise
    a. How to solve the use-case best (for both setAddUnknownValues(false/true)),
    b. If the current behaviour with the emptying suggestionbox and the iContains-criteria is an error.

    Thanks in advance,
    Blama









    Code for ComboBoxItemZipcode.java
    Code:
    public final class ComboBoxItemZipcode extends ComboBoxItem {
    	final private DataSource zipcodeDS = DataSource.get(DatasourceEnum.V_ZIPCODE.getValue());
    
    	public ComboBoxItemZipcode(String name) {
    		super(name);
    		setOptionDataSource(zipcodeDS);
    		setOptionOperationId("fetchSuggestionList");
    		setCachePickListResults(false);
    		setValueField("ZIPCODE");
    		setSortField("ZIPCODE");
    		setTextMatchStyle(TextMatchStyle.STARTS_WITH);
    		
    		//setEmptyPickListHeight(0);
    		setShowPickerIcon(false);
    		setAddUnknownValues(true);
    		setDefaultToFirstOption(false);
    		setBrowserSpellCheck(false);
    
    		ListGridField zipcode = new ListGridField("ZIPCODE");
    		ListGridField citysuggestion = new ListGridField("CITYSUGGESTION");
    		setPickListFields(zipcode, citysuggestion);
    		setPickListHeaderHeight(0);
    		setPickListWidth(200);
    		setPickListHeight(200);		
    	}
    }

    #2
    Hi Isomorphic,

    I don't know how I missed it, I just found "setPickListFilterCriteriaFunction", which should be the solution to my problem. I'll try it and let you know.

    Sorry, now "reading RTFM",
    Blama

    Comment


      #3
      Hi,

      I could solve the problem. Big thumbs up to the great features SmartGWT provides. My solution is as follows:
      Code:
      final ComboBoxItemZipcode zipcodeCBIZ = new ComboBoxItemZipcode("ZIPCODE") {
      	{
      		//setAddUnknownValues(false);
      		//setDataFetchMode(FetchMode.LOCAL);
      		setPickListFilterCriteriaFunction(new FormItemCriteriaFunction() {
      			@Override
      			public Criteria getCriteria(FormItemFunctionContext itemContext) {
      				Criterion currentCountryCriteria = new Criterion("COUNTRY_ID",
      						OperatorId.EQUALS, countryCBIC.getValueAsString());
      				if (itemContext.getFormItem().getValue() != null) {
      					Criterion currentZipcodeCriteria = new Criterion("ZIPCODE",
      							OperatorId.ISTARTS_WITH, itemContext.getFormItem()
      									.getValue().toString());
      					Criterion currentCityCriteria = new Criterion("ORTSVORSCHLAG",
      							OperatorId.ISTARTS_WITH, itemContext.getFormItem()
      									.getValue().toString());
      					Criterion currentCityCriteria2 = new Criterion("ORTSVORSCHLAG",
      							OperatorId.ICONTAINS, " "
      									+ itemContext.getFormItem().getValue().toString());
      					AdvancedCriteria zipcodeOrCity = new AdvancedCriteria();
      					zipcodeOrCity.buildCriterionFromList(OperatorId.OR,
      							new Criterion[] { currentZipcodeCriteria,
      									currentCityCriteria, currentCityCriteria2 });
      					AdvancedCriteria countryAndZipcodeOrCity = new AdvancedCriteria();
      					countryAndZipcodeOrCity.buildCriterionFromList(OperatorId.AND,
      							new Criterion[] { currentCountryCriteria, zipcodeOrCity });
      					return countryAndZipcodeOrCity;
      				} else
      					return currentCountryCriteria;
      			}
      		});
      	}
      };
      In order to support "setAddUnknownValues(false);" I'll do as suggested here http://forums.smartclient.com/showthread.php?p=57399#2 and use a validator instead.

      Best regards,
      Blama

      Comment

      Working...
      X