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:
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);
Comment