Announcement

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

    How do you get the PickTreeItem display value when used with DataSource

    I'm using SmartGWT 2.4. I'm setting up a form for users to enter news items and I'm trying to emulate something like what Quora has in regards to adding categories. To accomplish this, I've created a hidden SelectItem to store the actual values, a ToolStrip to contains buttons for added categories, and an IPickTreeItem with the categories datasource for the use to select one of our hierarchical categories to add.

    When the user selects a category from the IPickTreeItem, I want to add the selected id to the hidden SelectItem (not a problem) and add a Button to the ToolStrip with the name of the selected category. That last is the major problem. I can't seem to get the selected category name.

    I've tried catching both ChangeEvent and ChangedEvent. I expected that either getDisplayValue() or getSelectedRecord() would give me what I need, but the first comes back as an empty String and the second comes back as null.

    Here's the code that I'm using:
    Code:
    DataSource ds = new CategoryDataSourceFactory().getInstance();
    
    		CanvasItem canvasItem = new CanvasItem("", "Categories");
    		canvasItem.setTitleOrientation(TitleOrientation.TOP);
    		canvasItem.setEndRow(true);
    		canvasItem.setShouldSaveValue(false);
    
    		final ToolStrip categoriesToolStrip = new ToolStrip();
    		categoriesToolStrip.setVisible(false);
    		categoriesToolStrip.setVertical(false);
    
    		canvasItem.setCanvas(categoriesToolStrip);
    		final SelectItem categoriesSelectItem = new SelectItem("categories", "");
    		categoriesSelectItem.setVisible(false);
    		categoriesSelectItem.setShowTitle(false);
    		final IPickTreeItem categoryPickTreeItem = new IPickTreeItem("");
    		categoryPickTreeItem.setShouldSaveValue(false);
    		categoryPickTreeItem.setDisplayField("name");
    		categoryPickTreeItem.setValueField("id");
    		categoryPickTreeItem.setLoadDataOnDemand(false);
    		ds.fetchData();
    		categoryPickTreeItem.addChangedHandler(new ChangedHandler() {
    			public void onChanged(ChangedEvent event) {
    				final String selectedItemId = event.getValue().toString();
    				String selectedItemName = categoryPickTreeItem.getDisplayValue();
    				String[] currentValues = categoriesSelectItem.getValues();
    				
    				if (Arrays.binarySearch(currentValues, selectedItemId) == -1){
    					String[] updatedValues = new String[currentValues.length + 1];
    					for (int i = 0; i < currentValues.length; i++){
    						updatedValues[i] = currentValues[i];
    					}
    					updatedValues[currentValues.length] = selectedItemId;
    					categoriesSelectItem.setValues(updatedValues);
    					final ToolStripButton categoryButton = new ToolStripButton(selectedItemName);
    					categoryButton.setIconOrientation("right");
    					categoryButton.setIcon("http://spike.wharton.upenn.edu/images/delete_x.png");
    
    					categoriesToolStrip.addButton(categoryButton);
    					categoryButton.addClickHandler(new ClickHandler() {
    						@Override
    						public void onClick(ClickEvent event) {
    							String[] currentValues = categoriesSelectItem.getValues();
    							String updatedValues[] = new String[currentValues.length - 1];
    							boolean foundMatch = false;
    							for (int i = 0; i < currentValues.length; i++){
    								if (foundMatch){
    									updatedValues[i - 1] = currentValues[i];
    								} else{
    									if (currentValues[i].equals(selectedItemId)){
    										foundMatch = true;
    									} else{
    										updatedValues[i] = currentValues[i];
    									}
    								}
    							}
    							categoriesSelectItem.setValues(updatedValues);
    							categoryButton.setVisible(false);
    						}
    					});
    					categoryPickTreeItem.setValue((Object) null);
    				}
    
    			}
    		});
    		categoryPickTreeItem.setEmptyDisplayValue("Select Category");
    		categoryPickTreeItem.setShowTitle(false);
    		categoryPickTreeItem.setDataSource(ds);
    		categoryPickTreeItem.setEndRow(true);
    		form.setItems(canvasItem, categoriesSelectItem, categoryPickTreeItem);

    #2
    One thing I've found, at least with the hosted browser, that I can't reference categoryPickTreeItem directly in the ChangedEvent. I've switched it to event.getItem() and now getDisplayValue() is returning the id of the selected category. Which is better than an empty string, but still not good.

    I feel like this should be really easy. All I want is what is physically showing in the PickTreeItem. Can anyone help?

    Thanks.

    Comment


      #3
      you can call try event.getItem().getAttributeAsMap("valueMap") and it'll give you a map of value -> displayedValue, presuming one exists.

      i hit this before. kind of annoying.

      Comment


        #4
        Could use some input from Isomorphic on this one because none of the proposed solutions above are quite right.

        In a databound IPickTreeItem, in the ChangedHandler:

        - evt.getItem().getAttributeAsMap("valueMap") returns null
        - pickTreeItem.getSelectedRecord() returns null
        - evt.getItem().getDisplayValue() returns the *previous* display value, even though this is on the "onChanged" (not "onChange") handler.

        So, what is the proper way to get the display value (or, better yet, any arbitrary DataSourceField) in an IPickTreeItem?

        Comment

        Working...
        X