It's best here to take a step back and explain what you're actually trying to do - our previous responses were based on what we thought you might be trying to do.
Announcement
Collapse
No announcement yet.
X
-
Hi Isomorphic,
First I would like to mention:
No matter how many I have the fields and the criteria, I always get two criteria in the "setter".
In my test case I get these two criteria:
Code:{operator=and, criteria=[{operator=and, criteria=[{operator=and, criteria=[{operator=and, criteria=[{operator=and, criteria=[{operator=and, criteria=[{fieldName=commonName, operator=iContains, value=ara}, {fieldName=commonName, operator=iContains, value=cam}]}, {fieldName=scientificName, operator=iContains, value=cam}]}, {fieldName=scientificName, operator=iContains, value=dro}]}, {fieldName=scientificName, operator=iContains, value=dro}]}, {fieldName=information, operator=iContains, value=can}]}, {fieldName=information, operator=iContains, value=any}], $H=3}
Code:{fieldName=information, operator=iContains, value=any}
But it doesn't matter a lot because I've found a solution for it (it will work until you change something in that area).
"fieldMap" for my test case contains next values:
Code:{commonName=ara cam, scientificName=cam dro, information=can any}
For the second field(scientificName) in the filter editor , the following value should be displayed: cam dro.
For the third field(information) in the filter editor, the following value should be displayed: can any.
I want my filter to have the same behaviour as a filter for the ListGrid after user triggers filtering.
Only my filter in this case uses the iContains operator for each field and for each String entered, therefore it removes the duplicate values.
Code:package com.smartgwt.sample.client; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.client.GWT; import com.smartgwt.client.Version; import com.smartgwt.client.bean.BeanFactory; import com.smartgwt.client.core.KeyIdentifier; import com.smartgwt.client.data.AdvancedCriteria; import com.smartgwt.client.data.Criterion; import com.smartgwt.client.data.SortSpecifier; import com.smartgwt.client.types.OperatorId; import com.smartgwt.client.types.SortDirection; import com.smartgwt.client.types.TextMatchStyle; 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.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.FormItemCanEditCriterionPredicate; import com.smartgwt.client.widgets.form.FormItemCriterionGetter; import com.smartgwt.client.widgets.form.FormItemCriterionSetter; import com.smartgwt.client.widgets.form.fields.FormItem; import com.smartgwt.client.widgets.form.fields.TextItem; import com.smartgwt.client.widgets.grid.ListGrid; import com.smartgwt.client.widgets.grid.ListGridField; import com.smartgwt.client.widgets.layout.VLayout; public class BuiltInDS extends VLayout implements EntryPoint { private IButton recreateBtn; public void onModuleLoad() { GWT.create(BeanFactory.FormItemMetaFactory.class); KeyIdentifier debugKey = new KeyIdentifier(); debugKey.setCtrlKey(true); debugKey.setKeyName("D"); Page.registerKey(debugKey, new PageKeyHandler() { public void execute(String keyName) { SC.showConsole(); } }); setWidth100(); setHeight100(); recreateBtn = new IButton("Recreate"); recreateBtn.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { new MyWindow().show(); } }); addMember(recreateBtn); new MyWindow().show(); draw(); } private class MyWindow extends Window { public MyWindow() { setWidth(600); setHeight(300); setMembersMargin(0); setModalMaskOpacity(70); setTitle(" (" + Version.getVersion() + "/" + Version.getSCVersionNumber() + ")"); setShowMinimizeButton(false); setIsModal(true); setShowModalMask(true); centerInPage(); final ListGrid lg = new ListGrid() { { setShowFilterEditor(true); setDataSource("animals"); ListGridField commonNameLGF = new ListGridField("commonName"); commonNameLGF.setFilterEditorProperties(new FilterTextItem("commonName")); ListGridField scientificNameLGF = new ListGridField("scientificName"); scientificNameLGF.setFilterEditorProperties(new FilterTextItem("scientificName")); ListGridField informationLGF = new ListGridField("information"); informationLGF.setFilterEditorProperties(new FilterTextItem("information")); setFields(commonNameLGF, scientificNameLGF, informationLGF); setInitialSort(new SortSpecifier("commonName", SortDirection.ASCENDING)); fetchData(); } }; addItem(lg); } } private class FilterTextItem extends TextItem { public FilterTextItem(final String name) { super(name); setCriterionGetter(new FormItemCriterionGetter() { @Override public Criterion getCriterion(DynamicForm form, FormItem item, TextMatchStyle textMatchStyle) { return this.getCriterion(form, item); } @Override public Criterion getCriterion(DynamicForm form, FormItem item) { Object obj = form.getValue(name); if (obj == null) { return null; } else { String value = String.valueOf(obj); String[] s = value.split(" "); AdvancedCriteria finalCriteria = new AdvancedCriteria(OperatorId.AND); for (int i = 0; i < s.length; i++) { finalCriteria.addCriteria(new Criterion(name, OperatorId.ICONTAINS, s[i])); } return finalCriteria; } } }); setCriterionSetter(new FormItemCriterionSetter() { @Override public void setCriterion(DynamicForm form, FormItem item, Criterion criterion) { Criterion[] allCriteria = criterion.getCriteria(); List<String> valueList = new ArrayList<String>(); for (Criterion c : allCriteria) { String values = c.getValues().toString(); values = values.replaceAll("\\{", "").replaceAll("\\}", "").replaceAll("\\[", "").replaceAll("\\]", ""); values = values.replaceAll("operator=\\w+, ", "").replaceAll("criteria=", "").replaceAll("fieldName=", ""); values = values.replaceAll(", value", ""); valueList.addAll(Arrays.asList(values.split(","))); } Map<String, String> fieldMap = new HashMap<String, String>(); for (String s : valueList) { String fieldName = s.replaceAll(" {0,1}(.*)=.*", "$1"); String value = s.replaceAll(".*=(.*)", "$1"); if (value != null && value.contains(" ")) value.replaceAll(" ", ""); if (fieldMap.containsKey(fieldName)) { String oldValue = fieldMap.get(fieldName); if (!oldValue.contains(value)) fieldMap.put(fieldName, oldValue + " " + value); } else fieldMap.put(fieldName, value); } Set<String> fieldName = fieldMap.keySet(); // Set value for each field somehow??? for (String s : fieldName) item.setAttribute(s, fieldMap.get(s)); } }); setCanEditCriterionPredicate(new FormItemCanEditCriterionPredicate() { @Override public boolean canEditCriterion(DynamicForm form, FormItem item, Criterion criterion) { return true; } }); } } }
PavoLast edited by pavo123; 24 Jan 2019, 01:03.
Comment
-
Hi Isomorphic,
yes it is! But I think I've found a bug in this area.
In "getter", after I trigers filtering second time, "form.getValue(name)" is null and "item.getValue()" is not null!
Code:package com.smartgwt.sample.client; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.client.GWT; import com.smartgwt.client.Version; import com.smartgwt.client.bean.BeanFactory; import com.smartgwt.client.core.KeyIdentifier; import com.smartgwt.client.data.AdvancedCriteria; import com.smartgwt.client.data.Criterion; import com.smartgwt.client.data.ResultSet; import com.smartgwt.client.data.SortSpecifier; import com.smartgwt.client.types.OperatorId; import com.smartgwt.client.types.SortDirection; import com.smartgwt.client.types.TextMatchStyle; 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.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.FormItemCanEditCriterionPredicate; import com.smartgwt.client.widgets.form.FormItemCriterionGetter; import com.smartgwt.client.widgets.form.FormItemCriterionSetter; import com.smartgwt.client.widgets.form.fields.FormItem; import com.smartgwt.client.widgets.form.fields.TextItem; import com.smartgwt.client.widgets.grid.ListGrid; import com.smartgwt.client.widgets.grid.ListGridField; import com.smartgwt.client.widgets.layout.VLayout; public class BuiltInDS extends VLayout implements EntryPoint { private IButton recreateBtn; public void onModuleLoad() { GWT.create(BeanFactory.FormItemMetaFactory.class); KeyIdentifier debugKey = new KeyIdentifier(); debugKey.setCtrlKey(true); debugKey.setKeyName("D"); Page.registerKey(debugKey, new PageKeyHandler() { public void execute(String keyName) { SC.showConsole(); } }); setWidth100(); setHeight100(); recreateBtn = new IButton("Recreate"); recreateBtn.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { new MyWindow().show(); } }); addMember(recreateBtn); new MyWindow().show(); draw(); } private class MyWindow extends Window { public MyWindow() { setWidth(600); setHeight(300); setMembersMargin(0); setModalMaskOpacity(70); setTitle(" (" + Version.getVersion() + "/" + Version.getSCVersionNumber() + ")"); setShowMinimizeButton(false); setIsModal(true); setShowModalMask(true); centerInPage(); final ListGrid lg = new ListGrid() { { setShowFilterEditor(true); setDataSource("animals"); ResultSet rsProperties = new ResultSet(); rsProperties.setUseClientFiltering(false); setDataProperties(rsProperties); ListGridField commonNameLGF = new ListGridField("commonName"); commonNameLGF.setFilterEditorProperties(new FilterTextItem("commonName")); ListGridField scientificNameLGF = new ListGridField("scientificName"); scientificNameLGF.setFilterEditorProperties(new FilterTextItem("scientificName")); ListGridField informationLGF = new ListGridField("information"); informationLGF.setFilterEditorProperties(new FilterTextItem("information")); setFields(commonNameLGF, scientificNameLGF, informationLGF); setInitialSort(new SortSpecifier("commonName", SortDirection.ASCENDING)); fetchData(); } }; addItem(lg); } } private class FilterTextItem extends TextItem { public FilterTextItem(final String name) { super(name); setCriterionGetter(new FormItemCriterionGetter() { @Override public Criterion getCriterion(DynamicForm form, FormItem item, TextMatchStyle textMatchStyle) { return this.getCriterion(form, item); } @Override public Criterion getCriterion(DynamicForm form, FormItem item) { Object objDFField = form.getValue(name); if (objDFField == null) { // SC.logWarn("DF-Field: " + name + ". Value is null"); Object objFormItem = item.getValue(); if (objFormItem == null) { SC.logWarn("FormItem: " + name + ". Value is null and Field is also null, returning null. OK."); return null; } if (objDFField == null && objFormItem != null) { SC.logWarn("FormItem: " + name + ". Value is: " + objFormItem.toString() + " and DF-Field value is null. SHOULD NOT HAPPEN."); return null; } } String value = String.valueOf(objDFField); String[] s = value.split(" "); AdvancedCriteria finalCriteria = new AdvancedCriteria(OperatorId.AND); for (int i = 0; i < s.length; i++) { finalCriteria.addCriteria(new Criterion(name, OperatorId.ICONTAINS, s[i])); } // SC.logWarn("Field: " + name + ". Return final criteria: " + finalCriteria.getValues()); return finalCriteria; } }); setCriterionSetter(new FormItemCriterionSetter() { @Override public void setCriterion(DynamicForm form, FormItem item, Criterion criterion) { Criterion[] allCriteria = criterion.getCriteria(); List<String> valueList = new ArrayList<String>(); for (Criterion c : allCriteria) { String values = c.getValues().toString(); values = values.replaceAll("\\{", "").replaceAll("\\}", "").replaceAll("\\[", "").replaceAll("\\]", ""); values = values.replaceAll("operator=\\w+, ", "").replaceAll("criteria=", "").replaceAll("fieldName=", ""); values = values.replaceAll(", value", ""); valueList.addAll(Arrays.asList(values.split(","))); } Map<String, String> fieldMap = new HashMap<String, String>(); for (String s : valueList) { String fieldName = s.replaceAll(" {0,1}(.*)=.*", "$1"); String value = s.replaceAll(".*=(.*)", "$1"); if (value != null && value.contains(" ")) value.replaceAll(" ", ""); if (fieldMap.containsKey(fieldName)) { String oldValue = fieldMap.get(fieldName); if (!oldValue.contains(value)) fieldMap.put(fieldName, oldValue + " " + value); } else fieldMap.put(fieldName, value); } if (fieldMap.containsKey(item.getFieldName())) { // SC.logWarn("Sets " + item.getFieldName() + " value: " + fieldMap.get(item.getFieldName())); form.setValue(item.getFieldName(), fieldMap.get(item.getFieldName())); } } }); setCanEditCriterionPredicate(new FormItemCanEditCriterionPredicate() { @Override public boolean canEditCriterion(DynamicForm form, FormItem item, Criterion criterion) { List<String> valueList = new ArrayList<String>(); @SuppressWarnings("unchecked") String values = ((Map<String, String>) criterion.getValues()).toString(); values = values.replaceAll("\\{", "").replaceAll("\\}", "").replaceAll("\\[", "").replaceAll("\\]", ""); values = values.replaceAll("operator=\\w+, ", "").replaceAll("criteria=", "").replaceAll("fieldName=", ""); values = values.replaceAll(", value", ""); valueList.addAll(Arrays.asList(values.split(","))); Map<String, String> fieldMap = new HashMap<String, String>(); for (String s : valueList) { String fieldName = s.replaceAll(" {0,1}(.*)=.*", "$1"); String value = s.replaceAll(".*=(.*)", "$1"); if (value != null && value.contains(" ")) value.replaceAll(" ", ""); if (fieldMap.containsKey(fieldName)) { String oldValue = fieldMap.get(fieldName); if (!oldValue.contains(value)) fieldMap.put(fieldName, oldValue + " " + value); } else fieldMap.put(fieldName, value); } if (fieldMap.containsKey(item.getFieldName())) { // SC.logWarn("canEditCriterion() returns TRUE for " + item.getFieldName() + " and criterion " + fieldMap.toString()); return true; } else { // SC.logWarn("canEditCriterion() returns FALSE for " + item.getFieldName() + " and criterion " + fieldMap.toString()); return false; } } }); } } }
Pavo
Comment
-
We've made a DynamicForm change which effectively compresses or flattens the criteria received in this case - as of tomorrow's builds, this test-case will work with the following example code:
Code:setCriterionSetter(new FormItemCriterionSetter() { @Override public void setCriterion(DynamicForm form, FormItem item, Criterion criterion) { Criterion[] criteria = criterion.getCriteria(); String result = ""; for (Criterion c : criteria) { String value = c.getValueAsString(); if (value != null && value != "") { if (result.length() > 0) result += " "; result += value; } } item.setValue(result); } }); setCanEditCriterionPredicate(new FormItemCanEditCriterionPredicate() { @Override public boolean canEditCriterion(DynamicForm form, FormItem item, Criterion criterion) { String itemName = item.getName(); if (criterion.getFieldName() == itemName) { return true; } Criterion[] criteria = criterion.getCriteria(); for (Criterion crit : criteria) { if (crit.getFieldName() != itemName) { return false; } } return true; } });
Comment
-
Hi Isomorphic,
please see this issue that might be related to changes in this area.
Thank you & Best regards
Blama
Comment
-
Hi Isomorphic,
please take a look at this test case. I've found a new bug.
Code:package com.smartgwt.sample.client; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.client.GWT; import com.smartgwt.client.Version; import com.smartgwt.client.bean.BeanFactory; import com.smartgwt.client.core.KeyIdentifier; import com.smartgwt.client.data.AdvancedCriteria; import com.smartgwt.client.data.Criterion; import com.smartgwt.client.data.ResultSet; import com.smartgwt.client.data.SortSpecifier; import com.smartgwt.client.types.OperatorId; import com.smartgwt.client.types.SortDirection; import com.smartgwt.client.types.TextMatchStyle; 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.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.FormItemCanEditCriterionPredicate; import com.smartgwt.client.widgets.form.FormItemCriterionGetter; import com.smartgwt.client.widgets.form.FormItemCriterionSetter; import com.smartgwt.client.widgets.form.fields.FormItem; import com.smartgwt.client.widgets.form.fields.TextItem; import com.smartgwt.client.widgets.grid.ListGrid; import com.smartgwt.client.widgets.grid.ListGridField; import com.smartgwt.client.widgets.layout.VLayout; public class BuiltInDS extends VLayout implements EntryPoint { private IButton recreateBtn; public void onModuleLoad() { GWT.create(BeanFactory.FormItemMetaFactory.class); KeyIdentifier debugKey = new KeyIdentifier(); debugKey.setCtrlKey(true); debugKey.setKeyName("D"); Page.registerKey(debugKey, new PageKeyHandler() { public void execute(String keyName) { SC.showConsole(); } }); setWidth100(); setHeight100(); recreateBtn = new IButton("Recreate"); recreateBtn.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { new MyWindow().show(); } }); addMember(recreateBtn); new MyWindow().show(); draw(); } private class MyWindow extends Window { public MyWindow() { setWidth(600); setHeight(300); setMembersMargin(0); setModalMaskOpacity(70); setTitle(" (" + Version.getVersion() + "/" + Version.getSCVersionNumber() + ")"); setShowMinimizeButton(false); setIsModal(true); setShowModalMask(true); centerInPage(); final ListGrid lg = new ListGrid() { { setShowFilterEditor(true); setDataSource("animals"); ResultSet rsProperties = new ResultSet(); rsProperties.setUseClientFiltering(false); setDataProperties(rsProperties); ListGridField commonNameLGF = new ListGridField("commonName"); commonNameLGF.setFilterEditorProperties(new FilterTextItem("commonName")); ListGridField scientificNameLGF = new ListGridField("scientificName"); scientificNameLGF.setFilterEditorProperties(new FilterTextItem("scientificName")); ListGridField informationLGF = new ListGridField("information"); informationLGF.setFilterEditorProperties(new FilterTextItem("information")); setFields(commonNameLGF, scientificNameLGF, informationLGF); setInitialSort(new SortSpecifier("commonName", SortDirection.ASCENDING)); fetchData(); } }; addItem(lg); } } private class FilterTextItem extends TextItem { public FilterTextItem(final String name) { super(name); setCriterionGetter(new FormItemCriterionGetter() { @Override public Criterion getCriterion(DynamicForm form, FormItem item, TextMatchStyle textMatchStyle) { return this.getCriterion(form, item); } @Override public Criterion getCriterion(DynamicForm form, FormItem item) { Object objFormItem = item.getValue(); if (objFormItem == null) return null; String value = String.valueOf(objFormItem); String[] s = value.split(" "); AdvancedCriteria finalCriteria = new AdvancedCriteria(OperatorId.AND); for (int i = 0; i < s.length; i++) { finalCriteria.addCriteria(new Criterion(name, OperatorId.ICONTAINS, s[i])); } return finalCriteria; } }); setCriterionSetter(new FormItemCriterionSetter() { @Override public void setCriterion(DynamicForm form, FormItem item, Criterion criterion) { Criterion[] criteria = criterion.getCriteria(); String result = ""; for (Criterion c : criteria) { String value = c.getValueAsString(); if (value != null && value != "") { if (result.length() > 0) result += " "; result += value; } } item.setValue(result); } }); setCanEditCriterionPredicate(new FormItemCanEditCriterionPredicate() { @Override public boolean canEditCriterion(DynamicForm form, FormItem item, Criterion criterion) { String itemName = item.getName(); if (itemName != null && criterion.getFieldName() != null && criterion.getFieldName().equals(itemName)) { return true; } Criterion[] criteria = criterion.getCriteria(); for (Criterion crit : criteria) { if (itemName != null && crit.getFieldName() != null && !(crit.getFieldName().equals(itemName))) { return false; } } return true; } }); } } }
Pavo
Comment
-
This isn't a bug - your canEditCriterion() implementation returns true if the crit and item names are the same, but doesn't return false if they're different. Since your method returns true by default, you can just change your first condition to something like this:
Code:String itemName = item.getName(); if (itemName != null) { if (criterion.getFieldName() != null && !criterion.getFieldName().equals(itemName)) { return false; } }
Last edited by Isomorphic; 19 Feb 2019, 23:43.
Comment
-
Right - that's because your setCriterion() implementation doesn't deal with being passed a single criterion (which is what an "and" crit with one subcrit is flattened to) - just add this at the top of your setCriterion():
Code:if (criterion.getFieldName() != null) { result = criterion.getValueAsString(); } else { ... loop over sub-criteria }
Comment
Comment