10 Replies Latest reply on Aug 8, 2008 8:48 AM by 123qweads

    Problems about ArrayCollection and DataGrid

    123qweads Level 1
      Hi folks!
      I've an ugly problem..
      in the my application, I've a datagrid called DG and its dataprovider is binded too an ArrayCollection called AC.
      AC contains a query result...

      ok.. so far so good...

      the problem come when I try to select an object in AC by its name...
      AC made like this:
      0 objecthjh324jghf7
      1 objectd5674567
      2 objectàààgòhrTT
      3 object4534ç45jg

      so I can select an object by its index (0..1..2..3.. ecc)
      but not by its name (objectxxxxxx)

      I could want that the name of objects are the column name (as ID, NAME, NATION, TEL. ecc)

      is this possible?
      now, If I want ID object in AC ( i.e the ID column in my datagrid DG) , I must check with a loop from 0 to col_number in order to find AC(loop_value).ID

      EDIT
      example:
      if my first column in DG is 'id', I could want use a thing like "myDataGrid.columns('id')", now I must use this "myDataGrid.columns(0)"
      EDIT
      help me please!!!! :D :D

      P.S: sorry for my terrible english... ^__^
        • 1. Re: Problems about ArrayCollection and DataGrid
          Gregory Lafrance Level 6
          We should be able to help with this, but it is probably best to post simplified yet "complete" code reproducing your issue, including small sample data set.
          • 2. Problems about ArrayCollection and DataGrid
            123qweads Level 1
            Hi
            ok... I'll post the hot code (see above)

            in this way, all work fine, but I can't point an object by its name because my_AC is like in this http://labusa.altervista.org/immagininews/Immagine1.png...
            (if doesn't work do cut&paste)
            there is a solution??

            thank you :D
            • 3. Re: Problems about ArrayCollection and DataGrid
              Level 7

              "123qweads" <webforumsuser@macromedia.com> wrote in message
              news:g7ef87$49h$1@forums.macromedia.com...
              > Hi
              > ok... I'll post the hot code (see above)
              >
              > in this way, all work fine, but I can't point an object by its name
              > because
              > my_AC is like in this
              > http://labusa.altervista.org/immagininews/Immagine1.png...
              > there is a solution??
              >
              > thank you :D
              >
              >
              > THIS IS MY DATAGRID
              > <mx:DataGrid dataProvider="{_myAC}" id="_myDG" editable="false" x="10"
              > y="40" />
              >
              >
              > THIS IS MY ARRAYCOLLECTION
              > <mx:ArrayCollection id="_myAC" />
              >
              >
              > I LOAD A QUERY RESULT IN MY _myAC
              > ArrayToArrayCollection(myQuery, _myAC);
              >
              > Explain: myQuery is an Array type: resultSQL.data
              > (see http://livedocs.adobe.com/flex/3/langref/index.html)
              > ArrayToArrayCollection(...) is this simple function
              >
              > public function ArrayToArrayCollection(myArray:Array,
              > myAC:ArrayCollection):void{
              > myAC.removeAll();
              > for (var i:int=0; i< myArray.length; i++) {
              > myAC.addItem(myArray );
              > }
              > }

              This isn't related to your question, but you could replace the whole
              ArrayToArrayCollection stuff with this:

              _myAC.source=myQuery;

              What does myQuery contain? You show the top level of it, but there's no way
              to see what is _in_ your objects.


              • 4. Problems about ArrayCollection and DataGrid
                123qweads Level 1
                quote:


                This isn't related to your question, but you could replace the whole
                ArrayToArrayCollection stuff with this:

                _myAC.source=myQuery;


                Thanks Amy Blankenship!! :D
                Good advice... I don't know flex very well...

                myQuery is a normal Array, returned by resultSQL.data...
                so it contains an SQL table like this:

                Cod._____________LVL400
                Description_______This is a good object..
                Price____________400,00$
                Name___________coffee machine

                ecc ecc...
                so .. in each object that you can see in my previous image, are insert a set of data like that.
                Now I can approach the objects using a string instead of an index?
                • 5. Re: Problems about ArrayCollection and DataGrid
                  Level 7

                  "123qweads" <webforumsuser@macromedia.com> wrote in message
                  news:g7et5k$io6$1@forums.macromedia.com...
                  >
                  quote:


                  > This isn't related to your question, but you could replace the whole
                  > ArrayToArrayCollection stuff with this:
                  >
                  > _myAC.source=myQuery;
                  >

                  > Thanks Amy Blankenship!! :D
                  > Good advice... I don't know flex very well...
                  >
                  > myQuery is a normal Array, returned by resultSQL.data...
                  > so it contains an SQL table like this:
                  >
                  > Cod. LVL400
                  > Description This is a good object..
                  > Price 400,00$
                  > Name coffee machine
                  >
                  > ecc ecc...
                  > so .. in each object that you can see in my previous image, are insert a
                  > set
                  > of data like that.
                  > Now I can approach the objects using a string instead of an index?
                  >

                  Each object will be at an index of the ArrayCollection. They wouldn't be
                  named. But you could use something like

                  var myObj:Object = _myAC.getItemAt(0) as Object;
                  var thingie:String = myObj['property']

                  But that's not really what you need for a DataGrid. Could you try
                  articulate more clearly exactly what it is you are trying to do and what you
                  have tried?


                  • 6. Re: Problems about ArrayCollection and DataGrid
                    123qweads Level 1
                    ok...
                    maybe I've done some mistakes during my past posts.
                    I'll try to explain better my problem...

                    my problem is to give a name at myDG.columns
                    (before I've speak about DATAGRID... sorry! I'm guilty... but the problem is the same)

                    myDG.colums is an ARRAY and I can pick an object by index for exemple myDG.colums(1).dataFiled or myDG.colums(2).visible ecc...
                    If I can give a name for each array elements, I could pick an object by its name for example myDG.colums(ID).dataFiled or myDG.colums(PRICE).visible ecc...

                    Today I've discovery how give a name to object in ARRAYCOLLECTION... in this way
                    var o:Object = new Object();
                    o[namecol1] = myObject1;
                    o[namecol2] = myObject2;
                    myAC.addItem(o);

                    works fine, and in myAC I find namecol1, namecol2, namecol3 instead (0),(1),(2) but when I convert ArrayCollection in Array in the line

                    <mx:DataGrid columns="{myAC.toArray()}" ..../>

                    I lost all object name... and rest only the object information (i.e the column information like dataField, visible, headerText , etc)

                    WHY DO I WANT DO IT??
                    Because I want write a function that hide some columns...
                    now my function is O(n*m) where n < m but if I can resolve this problem my function become O(n)
                    and it is a good thing


                    • 7. Re: Problems about ArrayCollection and DataGrid
                      Level 7

                      "123qweads" <webforumsuser@macromedia.com> wrote in message
                      news:g7f9ff$2vu$1@forums.macromedia.com...
                      > ok...
                      > maybe I've done some mistakes during my past posts.
                      > I'll try to explain better my problem...
                      >
                      > my problem is to give a name at myDG.columns
                      > (before I've speak about DATAGRID... sorry! I'm guilty... but the problem
                      > is
                      > the same)
                      >
                      > myDG.colums is an ARRAY and I can pick an object by index for exemple
                      > myDG.colums(1).dataFiled or myDG.colums(2).visible ecc...
                      > If I can give a name for each array elements, I could pick an object by
                      > its
                      > name for example myDG.colums(ID).dataFiled or myDG.colums(PRICE).visible
                      > ecc...
                      >
                      > Today I've discovery how give a name to object in ARRAYCOLLECTION... in
                      > this
                      > way
                      > var o:Object = new Object();
                      > o[namecol1] = myObject1;
                      > o[namecol2] = myObject2;
                      > myAC.addItem(o);
                      >
                      > works fine, and in myAC I find namecol1, namecol2, namecol3 instead
                      > (0),(1),(2) but when I convert ArrayCollection in Array in the line
                      >
                      > <mx:DataGrid columns="{myAC.toArray()}" ..../>
                      >
                      > I lost all object name... and rest only the object information (i.e the
                      > column
                      > information like dataField, visible, headerText , etc)
                      >
                      > WHY DO I WANT DO IT??
                      > Because I want write a function that hide some columns...
                      > now my function is O(n*m) where n < m but if I can resolve this
                      > problem
                      > my function become O(n)
                      > and it is a good thing
                      >
                      >
                      >
                      >
                      > BAD!!!! (now)
                      > public function HideColums(col:Array, myDataGrid:DataGrid):void{
                      > for (var x:int = 0; x < col.length; x++)
                      > for (var i:int = 0; i < myDataGrid.columnCount; i++)
                      > if (myDataGrid.columns .dataField ==
                      > col[x]) myDataGrid.columns
                      .visible = false;
                      > }
                      >
                      >
                      > GOOD(after your explanation)
                      > public function HideColums(col:Array, myDataGrid:DataGrid):void{
                      > for (var x:int = 0; x < col.length; x++)
                      > myDataGrid.columns[col[c].toString()].visible = false;
                      > }
                      I'm confused now. Is this working for you (are you posting it as a
                      solution) or did you need further help?


                      • 8. Re: Problems about ArrayCollection and DataGrid
                        123qweads Level 1
                        the BAD case works, but is BAD...
                        the GOOD case doesn't work... it is an exemple of my dream

                        I'm here in order to know if it is a possible way, or not...

                        Thank for your patience Amy!!
                        • 9. Re: Problems about ArrayCollection and DataGrid
                          Level 7

                          "Amy Blankenship" <amySpamFilter@magnolia_pleaseNOspam_multimedia.com> wrote
                          in message news:g7fail$46o$1@forums.macromedia.com...
                          >
                          > "123qweads" <webforumsuser@macromedia.com> wrote in message
                          > news:g7f9ff$2vu$1@forums.macromedia.com...
                          >> ok...
                          >> maybe I've done some mistakes during my past posts.
                          >> I'll try to explain better my problem...
                          >>
                          >> my problem is to give a name at myDG.columns
                          >> (before I've speak about DATAGRID... sorry! I'm guilty... but the problem
                          >> is
                          >> the same)
                          >>
                          >> myDG.colums is an ARRAY and I can pick an object by index for exemple
                          >> myDG.colums(1).dataFiled or myDG.colums(2).visible ecc...
                          >> If I can give a name for each array elements, I could pick an object by
                          >> its
                          >> name for example myDG.colums(ID).dataFiled or myDG.colums(PRICE).visible
                          >> ecc...
                          >>
                          >> Today I've discovery how give a name to object in ARRAYCOLLECTION... in
                          >> this
                          >> way
                          >> var o:Object = new Object();
                          >> o[namecol1] = myObject1;
                          >> o[namecol2] = myObject2;
                          >> myAC.addItem(o);
                          >>
                          >> works fine, and in myAC I find namecol1, namecol2, namecol3 instead
                          >> (0),(1),(2) but when I convert ArrayCollection in Array in the line
                          >>
                          >> <mx:DataGrid columns="{myAC.toArray()}" ..../>
                          >>
                          >> I lost all object name... and rest only the object information (i.e the
                          >> column
                          >> information like dataField, visible, headerText , etc)
                          >>
                          >> WHY DO I WANT DO IT??
                          >> Because I want write a function that hide some columns...
                          >> now my function is O(n*m) where n < m but if I can resolve this
                          >> problem
                          >> my function become O(n)
                          >> and it is a good thing
                          >>
                          >>
                          >>
                          >>
                          >> BAD!!!! (now)
                          >> public function HideColums(col:Array, myDataGrid:DataGrid):void{
                          >> for (var x:int = 0; x < col.length; x++)
                          >> for (var i:int = 0; i < myDataGrid.columnCount; i++)
                          >> if (myDataGrid.columns .dataField ==
                          >> col[x]) myDataGrid.columns
                          .visible = false;
                          >> }
                          >>
                          >>
                          >> GOOD(after your explanation)
                          >> public function HideColums(col:Array, myDataGrid:DataGrid):void{
                          >> for (var x:int = 0; x < col.length; x++)
                          >> myDataGrid.columns[col[c].toString()].visible = false;
                          >> }

                          try (somewhere in your result handler that loads your objects):

                          var sampleObj:Object = myQuery[0];
                          columnNames = [];//should be globally defined
                          for (prop:String in sampleObj){
                          columnNames.push(prop);
                          }

                          now:

                          public function hideColumns(columnsToHide:Array, myDataGrid:DataGrid):void{
                          for (var x = 0; x< columnsToHide.length; x++){
                          myDataGrid.columns[columnNames.indexOf(columnsToHide[x])].visible=false;
                          }
                          }

                          Note this is not tested, but it is the first thing I would try




                          • 10. Re: Problems about ArrayCollection and DataGrid
                            123qweads Level 1
                            great idea!!
                            thank you very very much!!

                            I'll try to extend datagrid component to handler columnNames ,
                            like some guys have extend datagrid component to add color-row propriety...

                            Thank you again ^__^