EDIT - I have updated the mxml code to only show the needed code for the program to run the update.
I am new to Flex and have an issue I hope you all can help me out with.
I have researched this issue all over and most people say use the .refresh() method to update a datagrid after updating or adding a new record. This does not do anything.
So, my issue is this. I have a mySQL database hosted on 000webhost.com, I am connecting via PHP, I have 1 mxml file and 2 php files (I know I can use just 1 but I am learning and this was easier for me to use for troubleshooting). The issue is no matter what I change on the datagrid the update does not show on the datagrid unless I close out of IE and reopen it. I can refresh or open the page in another window and the update does not show. I have to exit entirely and restart in order for any updates or additions to show.
Here is my code. I hope someone can help.
Here is my licenseTracker.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
initialize="doSend()"
>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.List;
import mx.controls.Text;
import mx.events.CloseEvent;
import mx.events.ListEvent;
import mx.rpc.events.ResultEvent;
//Create array collection to hold data for datagrid
[Bindable]
private var datalist:ArrayCollection = new ArrayCollection;
private var objSend:Object = new Object;
//call hosted data php
public function doSend():void {
xmlFromDatabase.send();
}
//function for updating existing records
public function doUpdate():void {
objSend.updateAssetId = dgAssetId.text;
objSend.updateProgramData = dgProgram.text;
objSend.updateSerialData = dgSerial.text;
objSend.updateUserData = dgUser.text;
objSend.updateCostData = dgCost.text;
objSend.updateStatusData = dgStatus.text;
updateDatabase.send(objSend);
xmlFromDatabase.send();
doSend();
datalist.refresh();
}
protected function xmlFromDatabase_resultHandler(event:ResultEvent):void
{
datalist = event.result.data.row;
}
private function alertUpdateClickHandler(eventObj:CloseEvent):void
{
if(eventObj.detail==Alert.YES)
doUpdate();
else
{
//Do Nothing
}
}
private function itemClickEvent(event:ListEvent):void
{
//apply selected datagrid to string to textbox
dgAssetId.text=String(event.currentTarget.selectedItem.asset_id);
dgProgram.text=String(event.currentTarget.selectedItem.program);
dgSerial.text=String(event.currentTarget.selectedItem.serial);
dgUser.text=String(event.currentTarget.selectedItem.user_id);
dgCost.text=String(event.currentTarget.selectedItem.cost);
dgStatus.text=String(event.currentTarget.selectedItem.status_id);
}
]]>
</fx:Script>
<fx:Declarations>
<s:HTTPService url="http://mypage.com/data.php"
id="xmlFromDatabase"
showBusyCursor="true"
result="xmlFromDatabase_resultHandler(event)"
method="POST" />
<s:HTTPService url="http://mypage/update.php"
id="updateDatabase"
showBusyCursor="true"
method="POST"
resultFormat="text"
result='Alert.show("Record Successfully Updated. Record May Take A Minute To Update")'
/>
</fx:Declarations>
<s:VGroup paddingLeft="20" y="10" width="1000" horizontalAlign="center">
<s:Label width="670" height="32" color="#FF0000" fontFamily="Georgia" fontSize="24"
text="Software License Tracker " textAlign="center"/>
<!--Add Data grid-->
<mx:DataGrid id="dg" x="10" y="79" width="1000" dataProvider="{datalist}"
editable="false" itemClick="itemClickEvent(event)">
<mx:columns>
<mx:DataGridColumn headerText="Asset ID" dataField="asset_id" width="60"/>
<mx:DataGridColumn headerText="Program" dataField="program" width="200"/>
<mx:DataGridColumn headerText="Serial" dataField="serial" width="270"/>
<mx:DataGridColumn headerText="User" dataField="user" width="125"/>
<mx:DataGridColumn headerText="Cost" dataField="cost" width="50"/>
<mx:DataGridColumn headerText="Status" dataField="description" width="190" />
</mx:columns>
</mx:DataGrid>
</s:VGroup>
<!--Panel for adding new entries into datagrid-->
<s:Panel id="addFrame" x="10" y="263" width="443" height="210"
textAlign="left" title="Add/Edit Record">
<s:Label x="60" y="10" width="191" id="dgAssetId" />
<s:TextInput x="60" y="30" width="191" id="dgProgram" />
<s:TextInput x="60" y="58" width="191" id="dgSerial"/>
<s:TextInput x="60" y="84" width="191" id="dgUser" />
<s:TextInput x="60" y="112" width="191" id="dgCost"/>
<s:TextInput id="dgStatus" x="60" y="142" width="191"/>
<s:Label x="6" y="10" text="Asset ID"/>
<s:Label x="6" y="35" text="Program"/>
<s:Label x="6" y="62" text="Serial"/>
<s:Label x="6" y="89" text="User"/>
<s:Label x="6" y="117" text="Cost"/>
<s:Label x="6" y="147" text="Status"/>
<s:Button id="changeSubmit" x="312" y="33" label="Update" click='Alert.show("Are you sure you want to update the selected record?", "Update Record", 3, this, alertUpdateClickHandler);'/>
</s:Panel>
</s:Application>
my data.php file that requests data from the server:
<?php
//SQL Connection Info - update with your database, username & password
$host = "hostsite";
$username = "username";
$password = "password";
$db_name = "database";
$mysql_connection = mysql_connect($host, $username, $password);
mysql_select_db($db_name);
//Change this query as you wish for single or multiple records
$result = mysql_query("SELECT a.asset_id, a.program, a.serial, u.user_id, concat(u.first_name ,' ', u.last_name) as user, a.cost, s.description, s.status_id
FROM assets a
JOIN status s ON a.status = s.status_id
JOIN users u ON a.user_id = u.user_id
order by 1
");
//Get the number of rows
$num_row = mysql_num_rows($result);
//Start the output of XML
echo '<?xml version="1.0" encoding="iso-8859-1"?>';
echo "<data>";
echo '<num>' .$num_row. '</num>';
if (!$result) {
die('Query failed: ' . mysql_error());
}
/* get column metadata - column name -------------------------------------------------*/
$i = 0;
while ($i < mysql_num_fields($result)) {
$meta = mysql_fetch_field($result, $i);
$ColumnNames[] = $meta->name; //place col name into开发者_StackOverflow中文版 array
$i++;
}
$specialchar = array("&",">","<"); //special characters
$specialcharReplace = array("&",">","<"); //replacement
/* query & convert table data and column names to xml ---------------------------*/
$w = 0;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<row>";
foreach ($line as $col_value){
echo '<'.$ColumnNames[$w].'>';
$col_value_strip = str_replace($specialchar, $specialcharReplace, $col_value);
echo $col_value_strip;
echo '</'.$ColumnNames[$w].'>';
if($w == ($i - 1)) { $w = 0; }
else { $w++; }
}
echo "</row>";
}
if($num_row == "1"){
echo '<row></row>';
}
echo "</data>";
mysql_free_result($result);
mysql_close();
?>
here is my update.php
//SQL Connection Info - update with your database, username & password
$host = "hostsite";
$username = "username";
$password = "password";
$db_name = "database";
$mysql_connection = mysql_connect($host, $username, $password);
mysql_select_db($db_name);
$id = $_POST["updateAssetId"];
$program = $_POST["updateProgramData"];
$serial = $_POST["updateSerialData"];
$user = $_POST["updateUserData"];
$cost = $_POST["updateCostData"];
$status = $_POST["updateStatusData"];
$query = "UPDATE `database`.`assets` SET
`program` = '$program',
`serial` = '$serial',
`user_id` = '$user',
`cost` = '$cost',
`status` = '$status'
WHERE `asset_id` = '$id'";
if ( !mysql_query($query, $mysql_connection) ){
die('ERROR: '. mysql_error() );
}
/* echo "id: " . $id;
echo "Program: " . $program;
echo "Serial: " . $serial;
echo "User: " . $user;
echo "Cost: " . $cost;
echo "Status: " . $status; */
echo "UPDATE SUCCESSFUL, 1 Record Updated";
mysql_close();
?>
I have other php files for inserts and other functions but they should not be needed to fix this issue.
You seem to be confusing the differences be synchronous and Asynchronous code. Take, these lines, from your code
updateDatabase.send(objSend);
xmlFromDatabase.send();
doSend();
datalist.refresh();
First it calls updateDatabase.send(). This makes a remote request on your service.
Then it calls xmlFromDatabase.send() This also makes a remote request to your service.
Both of these are asynchronous calls. So you the xmlFromDatabase call is triggered before the results of the updateDatabase call are returned.
Then you call dataList.refresh(); which in essence does nothing because the dataList has not been changed yet.
Then at some point, your services will return back. There is no way to guarantee the order that the services will return values. The update database result handler will just show an alert. The xmlFromDatabase result handler will update the dataList.
I would guess that your xmlFromDatabase handler is being returned before the updateDatabase handler, therefore the datagrid never updates with new/updated data because it never gets new or updated data.
Move the xmlFromDatabase.send()
call to the updateDatabase result handler, and I suspect you'll see different results.
精彩评论