{"id":76,"date":"2009-06-16T19:29:33","date_gmt":"2009-06-16T17:29:33","guid":{"rendered":"http:\/\/www.zagura.ro\/index.php\/2009\/06\/16\/how-to-convince-the-smartgwt-listgrid-to-automatically-fetch-from-the-server-only-the-data-it-displays\/"},"modified":"2009-06-16T19:30:02","modified_gmt":"2009-06-16T17:30:02","slug":"how-to-convince-the-smartgwt-listgrid-to-automatically-fetch-from-the-server-only-the-data-it-displays","status":"publish","type":"post","link":"https:\/\/www.zagura.com\/blog\/index.php\/2009\/06\/16\/how-to-convince-the-smartgwt-listgrid-to-automatically-fetch-from-the-server-only-the-data-it-displays\/","title":{"rendered":"How to convince the SmartGWT ListGrid to automatically fetch from the server only the data it displays"},"content":{"rendered":"<p>Since I was looking all over the net how to do this and I only found pieces of information, here is an example:<\/p>\n<p>[java]<br \/>\npublic class OperationsGrid extends ListGrid{<br \/>\n\tOperationsGrid(){<br \/>\n\t\tsuper();<br \/>\n\t\tTreeGridField accNoField = new TreeGridField(&#8220;accountNo&#8221;, 150);<br \/>\n\t\tTreeGridField commentField = new TreeGridField(&#8220;comment&#8221;,  150);<\/p>\n<p>\t\tsetFields(accNoField,commentField);<br \/>\n\t\tsetHeight100();<br \/>\n\t\tsetWidth100();<br \/>\n\t\tsetAutoFetchData(true);<br \/>\n\t\tsetAlternateRecordStyles(true);<br \/>\n\t\tsetDataSource(OperationsDS.getInstance());<br \/>\n\t\tsetShowFilterEditor(true);<br \/>\n\t}<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>[java]<br \/>\npublic class OperationsDS extends DataSource{<br \/>\n\tstatic OperationsDS instance = null;<br \/>\n\t static OperationsDS getInstance(){<br \/>\n\t\tif(instance == null){<br \/>\n\t\t\tinstance = new OperationsDS();<br \/>\n\t\t}<br \/>\n\t\t return instance;<br \/>\n\t}<\/p>\n<p>\tOperationsDS(){<\/p>\n<p>\t\tsetDataURL(xpath);<br \/>\n\t\tDataSourceTextField noField = new DataSourceTextField(&#8220;accountNo&#8221;, &#8220;Account Number&#8221;, 128);<br \/>\n\t\tDataSourceTextField commentField = new DataSourceTextField(&#8220;comment&#8221;, &#8220;Comment&#8221;, 128);<\/p>\n<p>\t\tnoField.setRequired(true);<br \/>\n\t\tnoField.setPrimaryKey(true);<\/p>\n<p>\t\tsetFields(noField,commentField);<br \/>\n\t\tsetDataFormat(DSDataFormat.JSON);<br \/>\n\t\tsetRecordXPath(&#8220;response\/result&#8221;);<br \/>\n\t\tsetClientOnly(false);<br \/>\n\t}<br \/>\n\t @Override<br \/>\n\t protected Object transformRequest(DSRequest dsRequest){<br \/>\n\t\t String url = null;<\/p>\n<p>\t\t if(dsRequest.getOperationType().equals(DSOperationType.FETCH)){<br \/>\n\t\t\t dsRequest.setActionURL(getDataURL()+&#8221;&#038;start=&#8221;+dsRequest.getStartRow()+&#8221;&#038;end=&#8221;+dsRequest.getEndRow());<br \/>\n\t\t }<br \/>\n\t\t return super.transformRequest(dsRequest);<br \/>\n\t }<br \/>\n\t @Override<br \/>\n    protected void transformResponse(DSResponse response, DSRequest request,<br \/>\n            Object data) {<br \/>\n        DSOperationType operationType = request.getOperationType();<\/p>\n<p>        if (operationType == DSOperationType.FETCH) {<br \/>\n\t\t\tJSONArray info = XMLTools.selectObjects(data, &#8220;\/response\/resultInfo\/numTotalRows&#8221;);<br \/>\n\t\t\tInteger total = (int)Math.round(info.get(0).isNumber().doubleValue());<\/p>\n<p>\t\t\tinfo = XMLTools.selectObjects(data, &#8220;\/response\/resultInfo\/startRow&#8221;);<br \/>\n\t\t\tInteger start = (int)Math.round(info.get(0).isNumber().doubleValue());<\/p>\n<p>\t\t\tinfo = XMLTools.selectObjects(data, &#8220;\/response\/resultInfo\/endRow&#8221;);<br \/>\n\t\t\tInteger end = (int)Math.round(info.get(0).isNumber().doubleValue());<\/p>\n<p>\t\t\tif(total != null)<br \/>\n\t\t\t\tresponse.setTotalRows(total);<br \/>\n\t\t\tif(start != null)<br \/>\n\t\t\t\tresponse.setStartRow(start);<br \/>\n\t\t\tif(end != null)<br \/>\n\t\t\t\tresponse.setEndRow(end);<br \/>\n        } else {<br \/>\n            super.transformResponse(response, request, data);<br \/>\n        }<\/p>\n<p>    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>In this example the server outputs in JSON format and returns the the current start row, current end row and total number of rows in response\/resultInfo.<br \/>\nBasically you need to override transformRequest and transformResponse. In transformRequest you should add the start and end row to the requestURL and in transformResponse you should set the startRow, endRow, and totalRows, provided that you receive this information from your server. Also don&#8217;t forget to setAutoFetchData(true) on the grid.<\/p>\n<p>Hope this helps somebody.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since I was looking all over the net how to do this and I only found pieces of information, here is an example: [java] public class OperationsGrid extends ListGrid{ OperationsGrid(){ super(); TreeGridField accNoField = new TreeGridField(&#8220;accountNo&#8221;, 150); TreeGridField commentField = new TreeGridField(&#8220;comment&#8221;, 150); setFields(accNoField,commentField); setHeight100(); setWidth100(); setAutoFetchData(true); setAlternateRecordStyles(true); setDataSource(OperationsDS.getInstance()); setShowFilterEditor(true); } } [\/java] [java] public&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,15,10,13],"tags":[],"_links":{"self":[{"href":"https:\/\/www.zagura.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/76"}],"collection":[{"href":"https:\/\/www.zagura.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zagura.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zagura.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zagura.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=76"}],"version-history":[{"count":0,"href":"https:\/\/www.zagura.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/76\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zagura.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zagura.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zagura.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}