PDA

View Full Version : SimpleComboBox setValue destroys all other options



darkling235
16 Mar 2011, 10:42 AM
I have a list of options placed into my SimpleCombobox from a ListStore. The basic idea is I'm copying values from a grid into a formPanel for editing. Everything in the panel works except the SimpleComboBox. When I try to use setValue on the ComboBox, all the options are destroyed and the only selection option that remains is the one I just set manually.

This is my panel. The main code can be found in setRecord. This is where I assign the record values to the fields. Everything is working except the comboboxes. I've been beating my head against the wall on this one all day. Any help would be great. Thanks




import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelKeyProvider;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.LabelField;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.form.SimpleComboValue;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.scalebase.management.client.interfaces.DatabaseValidation;
import com.scalebase.management.client.interfaces.DatabaseValidationAsync;
import com.scalebase.management.client.model.InstanceData;
import com.scalebase.management.client.model.KeyColumn;
import com.scalebase.management.client.model.Table;
import com.scalebase.management.client.util.StringModel;

public class EditSplitTables extends FormPanel
{
private Button cancel;
private Button save;
private Table record;
private Window window;
private ListStore<Table> store;

private LabelField tableField;
private LabelField tableTypeField;
private LabelField algorithmField;
private LabelField keyField;
private ComboBox<Table> tableName;
private SimpleComboBox<String> tableType;
private SimpleComboBox<String> algorithm;
private ComboBox<KeyColumn> key;
private ListStore<Table> tableNameStore;
private ListStore<KeyColumn> keyStore;
private ListStore<SimpleComboValue<String>> tableTypeStore;
private ListStore<SimpleComboValue<String>> algorithmStore;


public EditSplitTables()
{
FormLayout layout = new FormLayout();
layout.setLabelSeparator("");
setLayout(layout);
tableField = new LabelField("Table Name");
tableName = new ComboBox<Table>();
tableName.setDisplayField("name");

tableTypeField = new LabelField("Table Type");
tableType = new SimpleComboBox<String>();
tableType.setValueField("tableType");
tableType.setDisplayField("tableType");
tableTypeStore = new ListStore<SimpleComboValue<String>>();
tableTypeStore.add(new StringModel("tableType", "SHARD"));
tableTypeStore.add(new StringModel("tableType", "GLOBAL"));
tableTypeStore.add(new StringModel("tableType", "MASTER"));
tableTypeStore.add(new StringModel("tableType", "REPLICATED"));
tableTypeStore.setKeyProvider(new ModelKeyProvider(){

@Override
public String getKey(ModelData model)
{
return model.get("tableType");
}});
tableType.setStore(tableTypeStore);
tableTypeField.addListener(Events.SelectionChange, new SelectionChangedListener<SimpleComboValue<String>>(){


@Override
public void selectionChanged(
SelectionChangedEvent<SimpleComboValue<String>> se)
{
if(se.getSelectedItem().getValue().equals("SHARD"))
{
algorithm.enable();
key.enable();
}
else
{
key.clearSelections();
algorithm.clearSelections();
algorithm.disable();
key.disable();
}

}});

algorithmField = new LabelField("Algorithm");
algorithm = new SimpleComboBox<String>();
algorithm.setValueField("algorithm");
algorithm.setDisplayField("algorithm");
algorithmStore = new ListStore<SimpleComboValue<String>>();
algorithmStore.add(new StringModel("algorithm", "HASH"));
algorithmStore.add(new StringModel("algorithm", "RANGE"));
algorithmStore.add(new StringModel("algorithm", "LIST"));
algorithm.setStore(algorithmStore);

keyField = new LabelField("Key");
key = new ComboBox<KeyColumn>();
key.setDisplayField("name");
//key.setUseQueryCache(false);

cancel = new Button("Cancel");
cancel.addSelectionListener(new SelectionListener<ButtonEvent>(){

@Override
public void componentSelected(ButtonEvent ce)
{
hide();
}});
cancel.setStyleName("modalCancelButton");
save = new Button("Verify and Save");
save.addSelectionListener(new SelectionListener<ButtonEvent>(){

@Override
public void componentSelected(ButtonEvent ce)
{
save();
}});
save.setStyleName("modalSaveButton");
add(tableField);
add(tableName);
add(tableTypeField);
add(tableType);
add(algorithmField);
add(algorithm);
add(keyField);
add(key);
add(cancel);
add(save);
}

public ListStore<Table> getTableNameStore() {
return tableNameStore;
}


public void setTableNameStore(ListStore<Table> tableNameStore) {
this.tableNameStore = tableNameStore;
tableName.setStore(tableNameStore);
}



public ListStore<KeyColumn> getKeyStore() {
return keyStore;
}


public void setKeyStore(ListStore<KeyColumn> keyStore) {
this.keyStore = keyStore;
key.setStore(keyStore);
}


public void setRecord(Table r)
{
this.record = r;
if(record != null)
{
Table t = tableNameStore.findModel("name", r.getName());
tableName.setValue(t);
SimpleComboValue<String> s = tableTypeStore.findModel(r.getTableType());
tableType.setValue(s);
KeyColumn c = keyStore.findModel("name", r.getKey());
key.setValue(c);
SimpleComboValue<String> al = algorithmStore.findModel(r.getType());
algorithm.setValue(al);
if(s.get("tableType").equals("SHARD") == false)
{
key.clearSelections();
algorithm.clearSelections();
algorithm.disable();
key.disable();
}
else
{
algorithm.enable();
key.enable();
}
}
else
{
tableName.setValue(null);
tableType.setValue(null);
key.setValue(null);
algorithm.setValue(null);
}
}

public void setWindow(Window w)
{
window = w;
}

public void setStore(ListStore<Table> data)
{
store = data;
}


public void hide()
{
if(window != null)
window.hide();
}

public void show()
{
if(window != null)
window.show();
}

public void save()
{

Table item = new Table();
item.setName(tableName.getValue().getName());
item.setType(algorithm.getValue().getValue());
item.setTableType(tableType.getValue().getValue());
item.setKey(key.getValue().getName());
if(record == null)
{
store.add(item);
}
else
{
Table oldItem = store.findModel(record);
oldItem.setName(item.getName());
oldItem.setType(item.getType());
oldItem.setKey(item.getKey());
oldItem.setTableType (item.getTableType());
store.update(item);
}
store.commitChanges();
//grid.getStore().getLoader().load();
hide();



}


}







import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.widget.form.SimpleComboValue;

public class StringModel extends SimpleComboValue<String>
{
public StringModel(String name, String value)
{
set(name, value);
}
}

sven
16 Mar 2011, 10:57 AM
combo.setTriggerAction(TriggerAction.ALL);

darkling235
16 Mar 2011, 12:04 PM
Thanks that seemed to work but what does it do? I was a little vague on what that changes after reading the API.

rmthamer
28 Mar 2011, 8:01 PM
Thanks so much for this answer... I too have been struggling with this one.

WesleyMoy
31 Mar 2011, 1:53 PM
When you type into a combo box, it filters the options available in the dropdown list. When you select an option, the list is filtered down until there is only that one option available.

Say you've got a store with the following items:


Hell
Hellmann
Hello
Bob


If you type "He", then you'll see "Hell", "Hellmann", and "Hello" appear in the dropdown. If you type "Hell", you'll still see all three options.

If you type "Hello", all the options will disappear other than "Hello". This is what is happening in your case. You can prove it by selecting an option, selecting the text in the combo box, and clearing it. All your options will be available again.

Setting the trigger action to ALL prevents this filtering. Regardless of what you type or choose, all the options in your store will appear.

(This would be good info to have in the JavaDoc.)