Hi Isomorphic,
please see below testcase.
Usecase is the following: I had a DynamicForm with a BooleanItem (for Reseller) and a SelectItem (for time dimension). Now I wanted to combine those two into one multiple-SelectItem in order to safe space.
Allowed are either "reseller", "one-Time-Entry" or "reseller"+"one-Time-Entry". Time entries itself are mutually exclusive.
This works just fine if "Reseller" is selected. It does not otherwise. Then it is possible to select many timeEntries.
As you can see in the log, it should also work in that case. The value passed to setValue() is correct - it just does not correctly display the current value as it should afterwards.
Am I doing everything correctly here (calling setValue in the ItemChangeHandler) and if so, what is going wrong here?
I also created an easier testcase (also included), that shows that setValue() is working as expected.
Tested with latest 5.1p and 6.1d (SNAPSHOT_v11.1d_2017-04-14).
Thank you & Best regards
Blama
please see below testcase.
Usecase is the following: I had a DynamicForm with a BooleanItem (for Reseller) and a SelectItem (for time dimension). Now I wanted to combine those two into one multiple-SelectItem in order to safe space.
Allowed are either "reseller", "one-Time-Entry" or "reseller"+"one-Time-Entry". Time entries itself are mutually exclusive.
This works just fine if "Reseller" is selected. It does not otherwise. Then it is possible to select many timeEntries.
As you can see in the log, it should also work in that case. The value passed to setValue() is correct - it just does not correctly display the current value as it should afterwards.
Am I doing everything correctly here (calling setValue in the ItemChangeHandler) and if so, what is going wrong here?
I also created an easier testcase (also included), that shows that setValue() is working as expected.
Tested with latest 5.1p and 6.1d (SNAPSHOT_v11.1d_2017-04-14).
Code:
package com.smartgwt.sample.client; import java.util.ArrayList; import java.util.LinkedHashMap; import com.google.gwt.core.client.EntryPoint; import com.smartgwt.client.Version; import com.smartgwt.client.core.KeyIdentifier; import com.smartgwt.client.util.Page; import com.smartgwt.client.util.PageKeyHandler; import com.smartgwt.client.util.SC; import com.smartgwt.client.widgets.IButton; import com.smartgwt.client.widgets.Label; import com.smartgwt.client.widgets.Window; import com.smartgwt.client.widgets.events.ClickEvent; import com.smartgwt.client.widgets.events.ClickHandler; import com.smartgwt.client.widgets.form.DynamicForm; import com.smartgwt.client.widgets.form.events.ItemChangeEvent; import com.smartgwt.client.widgets.form.events.ItemChangeHandler; import com.smartgwt.client.widgets.form.fields.SelectItem; import com.smartgwt.client.widgets.layout.VLayout; public class BuiltInDS implements EntryPoint { private VLayout mainLayout; private IButton recreateBtn; public void onModuleLoad() { KeyIdentifier debugKey = new KeyIdentifier(); debugKey.setCtrlKey(true); debugKey.setKeyName("D"); Page.registerKey(debugKey, new PageKeyHandler() { public void execute(String keyName) { SC.showConsole(); } }); mainLayout = new VLayout(20); mainLayout.setWidth100(); mainLayout.setHeight100(); recreateBtn = new IButton("Recreate"); recreateBtn.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { recreate(); } }); mainLayout.addMember(recreateBtn); recreate(); mainLayout.draw(); } private void recreate() { Window w = new Window(); w.setWidth("35%"); w.setHeight("35%"); w.setMembersMargin(0); w.setModalMaskOpacity(70); w.setTitle(" (" + Version.getVersion() + "/" + Version.getSCVersionNumber() + ")"); w.setTitle("Works with reseller selected, but not without" + w.getTitle()); w.setShowMinimizeButton(false); w.setIsModal(true); w.setShowModalMask(true); w.centerInPage(); w.setCanDragResize(true); final DynamicForm groupByDynamicForm = new GroupByDynamicForm(); w.addItem(groupByDynamicForm); Label l = new Label("Easier testcase below:"); w.addItem(l); LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>(); valueMap.put("year", "Year"); valueMap.put("quarterAndYear", "Quarter"); valueMap.put("monthAndYear", "Month"); valueMap.put("RESELLER", "Reseller"); final DynamicForm form2 = new DynamicForm(); final SelectItem si = new SelectItem("GROUPINGOPTIONS2", "TEST2"); si.setWidth(250); si.setValueMap(valueMap); si.setMultiple(true); form2.setFields(si); w.addItem(form2); IButton setOne = new IButton("Set year", new ClickHandler() { @Override public void onClick(ClickEvent event) { String[] one = new String[1]; one[0] = "year"; si.setValue(one); } }); w.addItem(setOne); IButton setTwo = new IButton("Set year+month", new ClickHandler() { @Override public void onClick(ClickEvent event) { String[] two = new String[2]; two[0] = "year"; two[1] = "monthAndYear"; si.setValue(two); } }); w.addItem(setTwo); w.show(); } private final class GroupByDynamicForm extends DynamicForm { private ArrayList<String> toStringAL(Object[] objArray) { if (objArray == null) return null; ArrayList<String> al = new ArrayList<String>(objArray.length); for (int i = 0; i < objArray.length; i++) al.add(objArray[i].toString()); return al; } private ArrayList<String> getNewGrouping(Object[] oldValues, Object[] newValues) { ArrayList<String> oV = toStringAL(oldValues); ArrayList<String> nV = toStringAL(newValues); // Change to 0, from 0, or de-selecting if (oV == null || nV == null || nV.size() < oV.size()) return nV; // Additionally selecting ArrayList<String> helper = new ArrayList<String>(nV); helper.removeAll(oV); String addedValue = helper.get(0); switch (addedValue) { case "RESELLER": case "USER": break; case "year": oV.remove("quarterAndYear"); oV.remove("monthAndYear"); break; case "quarterAndYear": oV.remove("year"); oV.remove("monthAndYear"); break; case "monthAndYear": oV.remove("year"); oV.remove("quarterAndYear"); break; } oV.add(addedValue); return oV; } public GroupByDynamicForm() { setNumCols(6); LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>(); valueMap.put("year", "Year"); valueMap.put("quarterAndYear", "Quarter"); valueMap.put("monthAndYear", "Month"); valueMap.put("RESELLER", "Reseller"); SelectItem si = new SelectItem("GROUPINGOPTIONS", "Group by"); si.setWidth(250); si.setValueMap(valueMap); si.setMultiple(true); addItemChangeHandler(new ItemChangeHandler() { @Override public void onItemChange(ItemChangeEvent event) { ArrayList<String> newValueList = getNewGrouping((Object[]) event.getOldValue(), (Object[]) event.getNewValue()); if (newValueList != null && !newValueList.isEmpty()) { event.cancel(); event.getItem().getForm().setValue("GROUPINGOPTIONS", newValueList.toArray(new String[newValueList.size()])); SC.logWarn("Set value: " + newValueList.toString()); if (event.getItem().getForm().getValue("GROUPINGOPTIONS") != null) SC.logWarn("Read value: " + toStringAL((Object[]) event.getItem().getForm().getValue("GROUPINGOPTIONS")).toString()); event.getItem().getForm().markForRedraw("MFR"); } else { event.getItem().clearValue(); SC.logWarn("New value: null"); } } }); setFields(si); } } }
Blama
Comment