In prior articles of this series, we covered the following:
In this article we shall continue where we left off by adding paging functionality to our Adobe Flex REST grid client.
We have revised our code to deal with dynamic paging. The below code does the following:
Our revised application view and code is shown below.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.controls.Alert;
[Bindable]
private var sresults:ArrayCollection;
private var numresults:String;
public function handleXml(event:ResultEvent):void
{
var i:int;
var npages:Array;
numresults = event.result.results.resultsummary.count;
txtResultStatus.text = numresults + " items fit your search criteria";
if (numresults == "0")
{
sresults = null;
cboPages.visible = false;
lblPage.visible = false;
npages = new Array(1);
npages[0] = '';
cboPages.dataProvider = npages;
}
else
{
sresults = event.result.results.table.row;
cboPages.visible = true;
if (cboPages.selectedIndex == 0){ //new search request
npages = new Array(int(int(numresults) / 20));
for (i = 0; i <= int(int(numresults) / 20); i++)
{
if ((i + 1) * 20 > int(numresults))
{
npages[i] = (i * 20 + 1) + ' - ' + numresults;
}
else {
npages[i] = (i * 20 + 1) + ' - ' + (i + 1) * 20;
}
}
cboPages.dataProvider = npages;
}
lblPage.visible = true;
}
}
public function handleFault(event:FaultEvent):void
{
Alert.show(event.fault.faultString, "Error");
}
public function search_click():void {
xmlRpc.request.offset = 0;
cboPages.selectedIndex = 0;
xmlRpc.send();
}]]>
</mx:Script>
<mx:HTTPService id="xmlRpc"
url="http://localhost:8080/pagila_php.php"
result="handleXml(event)"
fault="handleFault(event)">
<mx:request>
<query>{search.text}</query>
<maxrecs>20</maxrecs>
<offset>{int(cboPages.selectedIndex)*20}</offset>
</mx:request>
</mx:HTTPService>
<mx:VBox>
<mx:HBox id="HBoxUser" width="100%">
<mx:Label text="Search Terms" textAlign="right" fontWeight="bold" color="white" /> <mx:TextInput id="search" width="300" height="22" />
<mx:Label text="Page:" id="lblPage" textAlign="right" fontWeight="bold" color="white" visible="false" />
<mx:ComboBox id="cboPages" width="90" visible="false" change="xmlRpc.send()">
<mx:dataProvider>
<mx:Array><mx:String></mx:String></mx:Array>
</mx:dataProvider>
</mx:ComboBox>
<mx:Button x="130" y="95"
label="Search"
click="search_click()"
width="160" height="22" />
<mx:Label id="txtResultStatus" fontWeight="bold" color="white" />
</mx:HBox>
</mx:VBox><mx:DataGrid id="grdResult" dataProvider="{sresults}" editable="false" variableRowHeight="true"/>
</mx:Application>