8 Replies Latest reply on Aug 21, 2007 11:12 AM by ntsiii

    datagrid data update

    v.karailiev
      Hi!
      I'm new in flex and have a problem with datagrid.
      I'm using httpservice to get data. At some time I make new data request to check for changes. How can I compare the old and the new data and update the datagrid only if there are differences? I want this, because by every update datagrid loses its selection and scroll position.
        • 1. datagrid data update
          v.karailiev Level 1
          I have following code:

          [Bindable]private var currentData:Object = new Object;
          [Bindable]private var tmpData:Object = new Object;
          .....
          .....
          .....
          tmpData = driversListRequest.lastResult.list;

          if (currentData == tmpData) {
          Alert.show('==');
          }
          else {
          currentData = tmpData;
          Alert.show('!=');
          }

          But every time I get "!=" :(
          Any idea?
          • 2. Re: datagrid data update
            mitchgrrt
            Try looking in the debugger to see what is in currentData and tmpData. Maybe you will have to write your own comparison method, or compare objects that are a little bit down inside the tree of objects and proxies.

            Also you should be able to programmatically save and restore the selection and scroll position of the DataGrid before updating its data.
            • 3. Re: datagrid data update
              v.karailiev Level 1
              I think I do not need to write my own comparison method, because when I put currentData = tmpData; out of the if block it's OK.

              tmpData = driversListRequest.lastResult.list;
              currentData = tmpData;

              if (currentData == tmpData) {
              Alert.show('==');
              }
              else {
              Alert.show('!=');
              }

              returns always "==" (as expected). But why isn't the original code working?
              • 4. Re: datagrid data update
                ntsiii Level 3
                If you are using HTTPService, I advise using e4x XML. There is a lot of "blackbox" conversion happening when you use object, that can cause you trouble. Also comparing objects is problematic. I don't advise it. At least read up on it, including the "===" operator, if you insist on doing it.

                You can compare XMLNodes by using toXMLString() on both nodes and using simple "==" on the string representations.

                Tracy
                • 5. Re: datagrid data update
                  ntsiii Level 3
                  Oh, in your case above, comparing objects is actually comparing the references, not the proerty values of the objects. Obviously they are not the same object. Comparing objects: not advised.

                  Tracy
                  • 6. Re: datagrid data update
                    Megha_Shah Level 1
                    Hii,

                    You can compare two object using ObjectUtil.compare(object1,object2).which return 0 if your objects are same or return -1 if they are not same.
                    You cam pass your previous object and current object into compare function and you can check.
                    I have made sample application which compare two objects:
                    <?xml version="1.0" encoding="utf-8"?>
                    <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">

                    <mx:Script>
                    <![CDATA[
                    import mx.controls.Alert;
                    import mx.utils.ObjectUtil;

                    public var object1:Object;
                    public var object2:Object;

                    public function init():void
                    {
                    object1 = new Object();
                    object2 = new Object();


                    object1.name = "ruchi";
                    object1.id = "2";

                    object2.name = "ruchi";
                    object2.id = "2";
                    }

                    public function compare():void
                    {
                    var i:int = ObjectUtil.compare(object1,object2);
                    Alert.show(i+"");
                    }
                    ]]>
                    </mx:Script>
                    <mx:Button x="34" y="10" label="Button" click="compare()"/>
                    </mx:Application>

                    I think it may be help to you.

                    thanks,
                    Megha Shah

                    • 7. Re: datagrid data update
                      v.karailiev Level 1
                      Hi Megha,
                      ObjectUtil.compare(object1,object2) i exactly what I needed. I must only say the method returns 0 when the objects are equal and -1 or 1 if there are differences.
                      Thank you a lot :)
                      • 8. Re: datagrid data update
                        ntsiii Level 3
                        You should still use e4x xml and not object, though.
                        Tracy