Here is the code I wrote :
ListGrid tata = new ListGrid();
Criteria c = new Criteria();
c.addCriteria("Property3", new Integer(32074602));
tata.filterData(c);
but the tata contains data that do not match the filter.
Here is the code I wrote :
ListGrid tata = new ListGrid();
Criteria c = new Criteria();
c.addCriteria("Property3", new Integer(32074602));
tata.filterData(c);
but the tata contains data that do not match the filter.
Without seeing more code, and your datasource definition in particular its going to be hard to provide any assistance. Does your DataSource definition have a primary key defined?
See the help posting guidelines here.
Have a look at this sample which uses a ListGrid and filters data using a Criteria.
Sanjiv
Code : I cannot filter data using criteria (part 1)
Here is the code. I’m sorry, it’s very long but I adapted the application “pattern reuse” provided in the showcase to create this example. So I hope you’ll be able to recognize it :
package com.controle.gwt.client;
import java.util.ArrayList;
import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.Canvas;
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.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.controle.gwt.client.data.AddClassACompositionDS;
import com.controle.gwt.client.data.AddClassADS;
import com.controle.gwt.client.data.AddClassBDS;
import com.controle.gwt.client.data.AddClassCDS;
import com.controle.gwt.client.data.CountryXmlDS;
import com.controle.gwt.client.data.ItemSupplyXmlDS;
public class PatternReuse implements EntryPoint {
public void onModuleLoad() {
final DataSource addclassaDS = AddClassADS.getInstance();
final DataSource addclassbDS = AddClassBDS.getInstance();
final DataSource addclasscDS = AddClassCDS.getInstance();
final DataSource addclassaCompositionDS = AddClassACompositionDS.getInstance();
final ArrayList<DataSource> datasourcesAToPutInTabs = new ArrayList<DataSource>();
datasourcesAToPutInTabs.add(addclassaCompositionDS);
final ArrayList<DataSource> datasourcesBToPutInTabs = new ArrayList<DataSource>();
datasourcesBToPutInTabs.add(addclassaCompositionDS);
final ComplexEditor cEditor = new ComplexEditor(addclassaDS);
SelectItem dsSelect = new SelectItem();
dsSelect.setName("datasource");
dsSelect.setShowTitle(false);
dsSelect.setValueMap("classascen1", "classbscen1", "classcscen1");
dsSelect.addChangedHandler(new ChangedHandler() {
public void onChanged(ChangedEvent event) {
String ds = (String) event.getValue();
if (ds.equalsIgnoreCase("classascen1")) {
cEditor.setDatasource(addclassaDS);
cEditor.setDataSourceTabs(datasourcesAToPutInTabs);
cEditor.showTab();
}
else if (ds.equalsIgnoreCase("classbscen1")) {
cEditor.setDatasource(addclassbDS);
cEditor.setDataSourceTabs(datasourcesBToPutInTabs);
cEditor.showTab();
} else {
cEditor.setDatasource(addclasscDS);
cEditor.hideTab();
}
}
});
DynamicForm form = new DynamicForm();
form.setValue("datasource", "Select a DataSource");
form.setItems(dsSelect);
Window window = new Window();
window.setTitle("Introduction");
Label label = new Label();
label.setContents("TEMP DESCRIPTION");
label.setWidth100();
label.setHeight100();
label.setPadding(5);
label.setValign(VerticalAlignment.TOP);
window.setHeight(130);
window.setWidth100();
window.addItem(label);
VLayout layout = new VLayout(15);
layout.setWidth100();
layout.setHeight("80%");
layout.addMember(window);
layout.addMember(form);
layout.addMember(cEditor);
layout.draw();
}
}
package com.controle.gwt.client;
import java.util.ArrayList;
import com.controle.gwt.client.data.*;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceFloatField;
import com.smartgwt.client.data.fields.DataSourceIntEnumField;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.CriteriaPolicy;
import com.smartgwt.client.types.FetchMode;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.controle.gwt.client.CompoundEditor;
public class ComplexEditor extends CompoundEditor {
private ArrayList<DataSource> datasourcetabs = new ArrayList<DataSource>(); // datasource;
public ComplexEditor(DataSource datasource) {
super(datasource);
tata.setDataSource(compo);
tata.setAutoFetchData(false);
}
TabSet compositionsTabSet = new TabSet();
ListGrid tata = new ListGrid();
AddClassACompositionDS compo = AddClassACompositionDS.getInstance();
public void findTabByGridId(Integer gridId) {
Criteria c = new Criteria();
c.addCriteria("classaID", gridId);
removeAllTabs();
Tab temptab = null;
temptab = new Tab(compo.getTitle());
tata.fetchData(c);
temptab.setPane(tata);
compositionsTabSet.addTab(temptab);
}
protected void onInit() {
super.onInit();
addMember(compositionsTabSet);
grid.addRecordClickHandler(new RecordClickHandler() {
public void onRecordClick(RecordClickEvent event) {
form.clearErrors(true);
form.editRecord(event.getRecord());
saveButton.enable();
ListGridRecord selectedRecord = event.getRecord();
Integer gridId = 0;
if (null != selectedRecord) {
gridId = selectedRecord.getAttributeAsInt("subclassa");
}
System.out.println("gridId = " + gridId + "\n");
findTabByGridId(gridId);
}
});
}
public ArrayList<DataSource> getDataSourceTabs() {
return datasourcetabs;
}
public void setDataSourceTabs(ArrayList<DataSource> datasourcetabs) {
this.datasourcetabs = datasourcetabs;
Tab temptab;
removeAllTabs();
for(int i = 0; i < datasourcetabs.size(); i++) {
temptab = new Tab(datasourcetabs.get(i).getTitle());
temptab.setPane(new CompoundEditor(datasourcetabs.get(i)));
compositionsTabSet.addTab(temptab);
}
showTab();
}
public void hideTab() {
hideMember(compositionsTabSet);
}
public void showTab() {
showMember(compositionsTabSet);
}
public void removeAllTabs() {
for(int i = compositionsTabSet.getTabs().length - 1; i >= 0; i--) {
compositionsTabSet.removeTab(compositionsTabSet.getTab(i));
}
}
}
Code : I cannot filter data using criteria (part 2)
package com.controle.gwt.client;
import com.controle.gwt.client.data.AddClassADS;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.IButton;
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.grid.ListGrid;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
public class CompoundEditor extends HLayout {
private DataSource datasource;
protected DynamicForm form;
protected ListGrid grid;
protected IButton saveButton;
private static CompoundEditor instance = null;
public CompoundEditor(DataSource datasource) {
this.datasource = datasource;
}
public static CompoundEditor getInstance(DataSource datasource) {
if (instance == null) {
instance = new CompoundEditor(datasource);
}
return instance;
}
protected void onInit() {
super.onInit();
this.form = new DynamicForm();
form.setDataSource(datasource);
saveButton = new IButton("Save");
saveButton.setLayoutAlign(Alignment.CENTER);
saveButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
form.saveData();
}
});
VLayout editorLayout = new VLayout(5);
editorLayout.addMember(form);
editorLayout.addMember(saveButton);
editorLayout.setWidth(280);
grid = new ListGrid();
grid.setWidth(500);
grid.setHeight(350);
grid.setDataSource(datasource);
grid.setShowResizeBar(true);
grid.setAutoFetchData(true);
grid.addRecordClickHandler(new RecordClickHandler() {
public void onRecordClick(RecordClickEvent event) {
form.clearErrors(true);
form.editRecord(event.getRecord());
saveButton.enable();
}
});
addMember(grid);
addMember(editorLayout);
}
public DataSource getDatasource() {
return datasource;
}
public void setDatasource(DataSource datasource) {
this.datasource = datasource;
grid.setDataSource(datasource);
form.setDataSource(datasource);
saveButton.disable();
grid.fetchData();
}
public void setGrid(ListGrid grid) {
this.grid = grid;
}
public ListGrid getGrid() {
return grid;
}
}
package com.controle.gwt.client.data;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.OperationBinding;
import com.smartgwt.client.data.fields.*;
import com.smartgwt.client.types.DSOperationType;
import com.google.gwt.core.client.GWT;
public class AddClassACompositionDS extends DataSource {
private static AddClassACompositionDS instance = null;
public static AddClassACompositionDS getInstance() {
if (instance == null) {
instance = new AddClassACompositionDS("addclassacompositionDS");
}
return instance;
}
public static AddClassACompositionDS getInstance(String datasource) {
if (instance == null) {
instance = new AddClassACompositionDS(datasource);
}
return instance;
}
public AddClassACompositionDS(String id) {
setTitle("subclassa");
setID(id);
setRecordXPath("/List/classa/subclassa");
DataSourceIntegerField pkField = new DataSourceIntegerField("classaID");
pkField.setPrimaryKey(true);
//Datafields
DataSourceIntegerField Property3= new DataSourceIntegerField("Property3", "Property3", 10, true);
DataSourceFloatField Property4= new DataSourceFloatField("Property4", "Property4", 5);
DataSourceTextField Property1= new DataSourceTextField("Property1", "Property1");
DataSourceIntEnumField Property2= new DataSourceIntEnumField("Property2", "Property2", 10, true);
setFields(pkField, Property3, Property4, Property1, Property2);
setDataURL("ds/test_data/classa.xml");
if(GWT.isScript()) {
setClientOnly(true);
}
}
}
package com.controle.gwt.client.data;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.fields.*;
import com.google.gwt.core.client.GWT;
public class AddClassADS extends DataSource {
private static AddClassADS instance = null;
public static AddClassADS getInstance() {
if (instance == null) {
instance = new AddClassADS("addclassaDS");
}
return instance;
}
public AddClassADS(String id) {
setTitle("classa");
setID(id);
setRecordXPath("/List/classa");
DataSourceIntegerField pkField = new DataSourceIntegerField("classaID");
pkField.setPrimaryKey(true);
//Datafields
DataSourceIntEnumField Property3= new DataSourceIntEnumField("Property3", "Property3", 10, true);
DataSourceFloatField Property4= new DataSourceFloatField("Property4", "Property4", 5);
DataSourceTextField Property1= new DataSourceTextField("Property1", "Property1");
DataSourceIntEnumField Property2= new DataSourceIntEnumField("Property2", "Property2", 10, true);
DataSourceIntegerField classC = new DataSourceIntegerField("subclassa", "classC");
classC.setValueXPath("subclassa/classaID");
classC.setForeignKey(id + ".classaID");
setFields(pkField, classC, Property3, Property4, Property1, Property2);
setDataURL("ds/test_data/classa.xml");
if(GWT.isScript()) {
setClientOnly(true);
}
}
public AddClassADS(String id, String recordXPath, String dataURL) {
setTitle("classa");
setID(id);
setRecordXPath(recordXPath);
DataSourceIntegerField pkField = new DataSourceIntegerField("classaID");
pkField.setHidden(true);
pkField.setPrimaryKey(true);
//Datafields
DataSourceIntEnumField Property3= new DataSourceIntEnumField("Property3", "Property3", 10, true);
DataSourceFloatField Property4= new DataSourceFloatField("Property4", "Property4", 5);
DataSourceTextField Property1= new DataSourceTextField("Property1", "Property1");
DataSourceIntEnumField Property2= new DataSourceIntEnumField("Property2", "Property2", 10, true);
DataSourceIntegerField classC = new DataSourceIntegerField("subclassa", "classC");
classC.setValueXPath("subclassa/classaID");
classC.setForeignKey("addclassacompositionDS.classaID");
classC.setHidden(true);
setFields(pkField, classC, Property3, Property4, Property1, Property2);
setDataURL(dataURL);
if(GWT.isScript()) {
setClientOnly(true);
}
}
}
<List>
<classa>
<classaID>1234</classaID>
<Property3>58074602</Property3>
<Property1>Office Paper Products</Property1>
<Property2>808</Property2>
<Property4>0.24</Property4>
<subclassa>
<classaID>9375</classaID>
<Property3>32074602</Property3>
<Property1>Booh</Property1>
<Property2>8</Property2>
<Property4>9.24</Property4>
</subclassa>
</classa>
<classa>
<Property3>541375</Property3>
<Property1>That's a raise</Property1>
<Property2>102</Property2>
<Property4>0.4</Property4>
<subclassa>
<classaID>1234</classaID>
<Property3>9385629</Property3>
<Property1>To be defined</Property1>
<Property2>2</Property2>
<Property4>22.65</Property4>
</subclassa>
</classa>
<classa>
<Property3>8421035</Property3>
<Property1>Pretty sick</Property1>
<Property2>613</Property2>
<Property4>5.74</Property4>
</classa>
<classa>
<Property3>72644</Property3>
<Property1>Still online !</Property1>
<Property2>426</Property2>
<Property4>84.2</Property4>
</classa>
</List>
Too much code to look through really, but what may be the issue is that a DataSource that is not clientOnly passes filter criteria to the server and expects the server to return only matching data.
You’re right, now it works.
Thanks very much for your help.