9 Replies Latest reply on Sep 17, 2007 3:52 PM by AnthonyPetty

    datagrid not updating

    AnthonyPetty
      I have a datagrid in a custom mxml component that uses an arrayCollection for it's dataprovider. The control gets the data originally perfectly, but when the data is updated, the datagrid control does not update until I force a refresh of the interface (make it invisible, then visible again).

      Here is how it works:
      I get the data via an HTTPService in the main mxml application. The custom component has public arrayCollections that the data is parsed into.
      Example:
      myWizardComponent.initDG_wav=new ArrayCollection(DGArray_wav);

      The DGArray_wav is an array from the httpservice. myWizardComponent.initDG_wav is a public arrayCollection.

      As I said, it works perfectly the first time - but if the httpservice updates the data, it does not update in the datagrid control the second time without manually refreshing the user interface.

      I'm sure there is something simple that I'm doing wrong, but I don't know what it is. Can anyone point me in the right direction?

      (The reason I did not post the full code here is because it is long, and in several different files.)
        • 1. Re: datagrid not updating
          Senor_Roberto
          When you say the HTTPService updates the data, can you post just this bit of code?

          Is DGArray_wav the result of an HTTPService of type 'object'?

          Can you post the code where you initially bind and also where you update the dataProvider of the DataGrid?

          See how you've got new ArrayCollection(DGArray_wav) - does this line execute each time the service is updated?

          Cheers







          • 2. Re: datagrid not updating
            AnthonyPetty Level 1
            The following function is called via the result event from the mxml for the httpservice...
            I'm a fairly new flex programer, so I understand that the following code may be... um... messy... Sorry about that.


            This is the mxml...
            <mx:HTTPService id="feedRequest3" resultFormat="e4x" url="downloads_xml.php" useProxy="false" result="load_data_downloads();" fault="httpserviceFault(event);"/>



            Here is the function...
            public function load_data_downloads():void {
            var myXML:XML = XML(feedRequest3.lastResult);
            //Loop through the XML (once for each file (aka item))
            var DGArray_wav:Array = new Array();
            var DGArray_mp3:Array = new Array();
            var DGArray_zip:Array = new Array();
            var DGArray_other:Array = new Array();
            for each (var myitem:XML in myXML.item) {
            try {
            if (myitem.file_type == "wav") {
            DGArray_wav.push({TrackNumber:myitem.track_number, TrackName:myitem.song_name, Time:myitem.track_length, DownloadSize:myitem.file_size + " " + myitem.file_size_units, DownloadType:myitem.file_type, FullFileName:myitem.full_filename});
            } else if (myitem.file_type == "mp3") {
            DGArray_mp3.push({TrackNumber:myitem.track_number, TrackName:myitem.song_name, Time:myitem.track_length, DownloadSize:myitem.file_size + " " + myitem.file_size_units, DownloadType:myitem.file_type, FullFileName:myitem.full_filename});
            } else if (myitem.file_type == "zip") {
            DGArray_zip.push({TrackNumber:myitem.track_number, TrackName:myitem.song_name, Time:myitem.track_length, DownloadSize:myitem.file_size + " " + myitem.file_size_units, DownloadType:myitem.file_type, FullFileName:myitem.full_filename});
            } else {
            DGArray_other.push({TrackNumber:myitem.track_number, TrackName:myitem.song_name, Time:myitem.track_length, DownloadSize:myitem.file_size + " " + myitem.file_size_units, DownloadType:myitem.file_type, FullFileName:myitem.full_filename});
            }
            } catch (err:Error) {
            Alert.show("Sorry, but something didn't work correctly.\n\nError: " + err.message);
            }
            }
            initDG_wav=new ArrayCollection(DGArray_wav);
            myWizardComponent.initDG_wav=new ArrayCollection(DGArray_wav);
            initDG_mp3=new ArrayCollection(DGArray_mp3);
            myWizardComponent.initDG_mp3=new ArrayCollection(DGArray_mp3);
            initDG_zip=new ArrayCollection(DGArray_zip);
            myWizardComponent.initDG_zip=new ArrayCollection(DGArray_zip);
            initDG_other=new ArrayCollection(DGArray_other);
            myWizardComponent.initDG_other=new ArrayCollection(DGArray_other);
            myWizardComponent.custid = custid;
            myWizardComponent.updateData();
            }




            As I said before, the myWizardComponent.initDG_wav (and mp3, zip, and other) are public ArrayCollections in the custom mxml component.

            Hope this sheds some more light on the situation. Thanks in advance for the help.

            Also, sorry about the poor code formating - I don't know how to keep the tabbing and line breaking in these forums.
            • 3. Re: datagrid not updating
              Senor_Roberto Level 1
              It's ok, this helps but we're not there yet.

              Can you show me the code in myWizardComponent.updateData() ?

              The part I'm most interested in is where you set the dataProvider for the data grid.

              Cheers
              • 4. Re: datagrid not updating
                AnthonyPetty Level 1
                This is the public function in the myWizardComponent. I added this function to manually update the datagrid's sources since it wouldn't do the update automatically. This takes care of my problem, but without it, the datagrid will only update the first time the data loads, not if it changes. I am hoping to not use this function, as it seems like over kill to me. I thought with an ArrayCollection as the dataprovider, the datagrid was supposed to update automatically if the data changed.



                public function updateData():void{
                if (cnvsAPosition == 0) {
                if (curLoadedDGData == "MP3") {
                dataGridA.dataProvider = initDG_mp3;
                } else {
                dataGridA.dataProvider = initDG_wav;
                }
                } else {
                if (curLoadedDGData == "MP3") {
                dataGridB.dataProvider = initDG_mp3;
                } else {
                dataGridB.dataProvider = initDG_wav;
                }
                }
                }
                • 5. Re: datagrid not updating
                  AnthonyPetty Level 1
                  Oh, I assume you are wondering where I've setup my dataProvider code at.....

                  In myWizardComponent, I setup the dataProvider of my datagrids on a conditional basis - but it is set and works on the initial load of the data. So, as long as that dataProvider doesn't change, when the original data changes in the ArrayCollection, shouldn't the datagrid change too?
                  • 6. Re: datagrid not updating
                    Senor_Roberto Level 1
                    Just to be clear, when you say 'when the data updates' what do you mean exactly?

                    Do you mean when it changes on the client or when something changes on the server? If on the server, how does the client know the server data has change?

                    Are you re-requesting the data each time and calling load_data_downloads() each time it updates?
                    • 7. Re: datagrid not updating
                      AnthonyPetty Level 1
                      The client changes something, with runs a new request via the httpservice. The results from the second request don't update in the datagrid correctly. I know the ArrayCollection is updating, because I can manually make it update the datagrid, but it should work automatically and it doesnt.

                      Sorry for the lack of clarity.
                      • 8. Re: datagrid not updating
                        Senor_Roberto Level 1
                        I think the reason it's not working is because every time there is a data update, i.e. every time the HTTP service is called and load_data_downloads() is called, you're assigning new collection references (i.e. new ArrayCollection(DGArray_wav)) to the property which is bound to the dataProvider and not directly to the dataProvider.

                        So when the inital bind happens between initDG_mp3 and dataProvider, I think it binds by value not by reference (someone correct me if I'm wrong). So when you go and then update initDG_mp3, you're updating the source data but not the bound data (does that make sense?).

                        Try binding to the dataProvider directly as an experiment and see if it works, then we can work back from there.








                        • 9. Re: datagrid not updating
                          AnthonyPetty Level 1
                          That makes sense to me - I'll try it when I get a chance tomorrow or the next day. Thanks for all your help so far.