this problem has driven me crazy and I really need you guys help.
I have a datagrid showing data that is queried from mysql. I am trying to add a new row to my datagrid if the users add new record to the database. In other words, I want datagrid to reflect the uploaded data in the database. The datagrid seems to work ONLY on the first record I added. If I try to add a second record, the new row will be added but it ALSO change the first row to the newly updated record. I know it’s a bit hard to understand so I draw a picture here.
DATAGRID
id name number
1 test1 111
2 test2 222
Lets say I add a new record and the datagrid will be
id name number
1 test1 111
2 test2 222
3 test3 333 //new record, works fine.
but if I add another record, it will become
id name number
1 test1 111
2 test2 222
4 test4 444 //old record, the values change to the newest record.
4 test4 444 //new record, but also change the previous record
If I add another record again, it will become
id name number
1 test1 111
2 test2 222
5 test5 555 //first record, the values change to the newest record.
5 test5 555 //second record, the values change to the newest record.
5 test5 555 //newest record, change the previous 2 records
I have done anything I could think of to debug but still no luck.
The following are my codes:
// my php data service
<s:CallResponder id="addResult" result="addResult_resultHandler(event)" />
<script>
//The handler after the user click ADD button
protected function addBtn_clickHandler(event:MouseEvent):void
{
var newData:object = new Object();
newData.test1=formField1.text;
newData.test2=formField2.text;
newData.test3=formField3.text;
newData.test4=formField4.text;
addResult.token=Service.createJob(newData);
}
//the result handler for data service.
protected function addResult_resultHandler(event:ResultEvent):void
{
//jobData is my dataProvider in my datagrid component
//not sure why addItem() would mess me up so badly...
//newData is an Object created after user click the update
//button
jobDG.jobData.addItem(newData);
}
Mxml
<components:dataGridPanel id="jobDG"
jobData="{getJobsResult.lastResult}"/>
My datagrid component
mxml
<s:DataGrid id="dg" editable="true" x="5" y="3" width="734" height="253">
<s:columns>
<s:ArrayList>
<s:GridColumn dataField="test1" headerText="text1">
</s:GridColumn>
<s:GridColumn dataField="test2" headerText="text2">
</s:GridColumn>
<s:GridColumn dataField="test3" headerText="text3">
</s:GridColumn>
<s:GridColumn dataField="test4" headerText="text4">
</s:GridColumn>
</s:ArrayList>
</s:columns>
<s:typicalItem>
<fx:Object text1="aaaaa" text2="bbbbb" text3="ccccc" text4="dddddd">
</fx:Object>
</s:typicalItem>
<s:AsyncListView list="{jobData}" />
</s:DataGrid>
I am really desperate now. Any helps would be appreciated.
I agree with the other commenters that your posted code is unlikely to be what you’re actually using, but I think it does show the seeds of what your problem is. I think you have an instance variable called newData that you instantiate at the top of your Class, rather than in the addBtn_clickHandler function.
The first time it works, because you’ve never added it to the collection before. When you click the button again, I suspect you’re simply changing the properties on the same object, then adding it to the collection in your result handler (rather than looking at the event’s data to get the new object that has been returned from the database).