updateCaches() applies only client-side filtering to determine whether a record should be added to a grid. That's all the information it has, since SQL fragments are, by design, never exposed to the browser (this would be information leakage from a security perspective).
The example posted here is necessarily contrived, but more generally, for handling this with fixed criteria:
1. you could set the criteria instead via listGrid.initialCriteria, and this will be cumulative with anything the user types into a FilterEditor. If this is a security constraint, you can set the same criteria client and server.
2. you could drive the cache updates from the server instead, using dsResponse.addRelatedUpdates(), which ultimately calls updateCaches(). This way, you could ensure you are only delivering records that would already pass the server's whereClause.
Announcement
Collapse
No announcement yet.
X
-
ListGrid not updating when WhereClause set
My ListGrid doesn't updates well with updateCaches when the DataSource contains a <whereClause>.
Attached a minimal testcase:
DataSource1:Code:public class TestingModule implements EntryPoint { public void onModuleLoad() { final ListGrid countryList = new ListGrid(); countryList.setWidth(500); countryList.setAlternateRecordStyles(true); countryList.setDataSource(DataSource.get("worldDSExport")); countryList.setAutoFetchData(true); ListGridField pk = new ListGridField("pk"); pk.setHidden(true); ListGridField countryName = new ListGridField("countryName", "Country"); ListGridField capital = new ListGridField("capital", "Capital"); ListGridField continent = new ListGridField("continent", "Continent"); countryList.setFields(pk, countryName, capital, continent); countryList.setAutoFitData(Autofit.VERTICAL); countryList.setShowFilterEditor(true); countryList.setAutoFitMaxRecords(10); VLayout layout = new VLayout(15); layout.setHeight(300); HLayout formLayout = new HLayout(15); IButton addBtn = new IButton("Correct capital"); addBtn.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { Record updateRecord = new Record(); updateRecord.setAttribute("pk", 3); updateRecord.setAttribute("capital", "Washington"); DataSource.get("worldDSExport").updateData(updateRecord, new DSCallback() { @Override public void execute(DSResponse dsResponse, Object data, DSRequest dsRequest) { Criteria c = new Criteria(); c.addCriteria("countryName", "USA"); DataSource.get("worldDSExport").fetchData(c, new DSCallback() { @Override public void execute(DSResponse dsResponse, Object data, DSRequest dsRequest) { for (Record rec: dsResponse.getData()) { DSResponse simulatedResponse = new DSResponse(); simulatedResponse.setOperationType(DSOperationType.UPDATE); simulatedResponse.setDataSource("worldDSExport2"); simulatedResponse.setData(new ListGridRecord(rec.getJsObj())); System.out.println("PK: " + rec.getAttribute("pk")); DataSource.get("worldDSExport2").updateCaches(simulatedResponse); } } }); } }); } }); formLayout.addMember(addBtn); layout.addMember(formLayout); layout.addMember(countryList); final ListGrid countryList2 = new ListGrid(); countryList2.setWidth(500); countryList2.setAlternateRecordStyles(true); countryList2.setDataSource(DataSource.get("worldDSExport2")); countryList2.setAutoFetchData(false); ListGridField pk2 = new ListGridField("pk"); pk2.setHidden(true); ListGridField countryName2 = new ListGridField("countryName", "Country"); ListGridField capital2 = new ListGridField("capital", "Capital"); ListGridField continent2 = new ListGridField("continent", "Continent"); countryList2.setFields(pk2, countryName2, capital2, continent2); countryList2.setAutoFitData(Autofit.VERTICAL); countryList2.setShowFilterEditor(true); countryList2.setAutoFitMaxRecords(10); countryList2.fetchData(); layout.addMember(countryList2); layout.draw(); } }
DataSource2:Code:<DataSource ID="worldDSExport" tableName="worldDS" serverType="sql" > <fields> <field name="pk" type="integer" hidden="true" primaryKey="true" /> <field name="countryCode" type="text" required="true" /> <field name="countryName" type="text" required="true" /> <field name="capital" type="text" /> <field name="government" type="text" length="500" /> <field name="continent" type="text" > </field> <field name="independence" type="date" /> <field name="area" type="float" /> <field name="population" type="integer" /> <field name="gdp" type="float" /> <field name="member_g8" type="boolean" /> </fields> </DataSource>
Note that DataSource2 contains a whereClause.Code:<DataSource ID="worldDSExport2" tableName="worldDS" serverType="sql" > <fields> <field name="pk" type="integer" hidden="true" primaryKey="true" /> <field name="countryCode" type="text" required="true" /> <field name="countryName" type="text" required="true" /> <field name="capital" type="text" /> <field name="government" type="text" length="500" /> <field name="continent" type="text" > </field> <field name="independence" type="date" /> <field name="area" type="float" /> <field name="population" type="integer" /> <field name="gdp" type="float" /> <field name="member_g8" type="boolean" /> </fields> <operationBindings> <operationBinding operationType="fetch" > <whereClause> continent = 'Europe' </whereClause> </operationBinding> </operationBindings> </DataSource>
In the code, I am trying to update the second listgrid which uses datasource2. The whereClause is not respected. After pressing the button, all records are being shown, discarding the whereClause. So if we have a record with continent=America, it will be shown in listGrid2, although its whereClause says: continent=Europe.
Is this a bug or am I doing something wrong?
Using Smartgwt 3.1p (v9.0p_2013-09-25)Tags: None
Leave a comment: