6 Replies Latest reply on Sep 27, 2008 9:14 AM by 123qweads

    Sort DataGrid Columns built dynamically...

    123qweads
      Hi people...
      usually, I build my DataGrid dynamically... with something like this:

      ...omissis...
      SQLstatement.text = 'SELECT name, address FROM myDBTable';
      SQLstatement.execute();
      resultSQL = SQLstatement.getResult();
      MyArrayCollection.source = resultSQL.data;
      myDataGrid.dataProvider= MyArrayCollection;
      ...omissis...

      and it works perfectly.... the problem is that resultSQL.data, by default, is an array sorted by 'abc' (ehmmm.. in english is alphabetical sort??).
      I could want the same sorting of my query, where NAME came before ADDRESS......

      There is a solution?
      I don't want pre-build a datagrid with all columns..... because this voids my dinamical datagrid functions...

      thanks for every aids....
      bye bye ^__^

        • 1. Re: Sort DataGrid Columns built dynamically...
          ntsiii Level 3
          Sort the ArrayCollection.
          Tracy
          • 2. Re: Sort DataGrid Columns built dynamically...
            123qweads Level 1
            ehmmm.... yes... good idea...

            no... really... it's a joke... true?
            I can't sort AC.. there is not a logic order between fields in DB tables...

            or, maybe, you know how sort AC like a query result??

            byezzz ^__^
            • 3. Re: Sort DataGrid Columns built dynamically...
              ntsiii Level 3
              Oh, I see. You said, "...resultSQL.data, by default, is an array sorted by..." which you did not really mean. The data array will be in whatever order the ORDER BY clause specifies. But you are talking about the fields/columns/properties, not the item order.

              There is no way to be certain of the order when you let Flex build your columns. Typically arrays are ordered in the reverse that they were built, but that is not reliable. The columns are not sorted at all, they are just built from the array of first-level child properties.

              Build the columns yourself, in AS.

              First, what is your resultFormat, and what is the dataType of the items in the ArrayCollection? If your items are Objects, then there is no way to define an order, object properties are not ordered.

              So you would need to come up with some way to guarantee your field order. That will either be Array or XML.

              When you can predict the order of your columns, you can build the columns array dynamically.

              Tracy
              • 4. Re: Sort DataGrid Columns built dynamically...
                123qweads Level 1
                yes ntsiii, you have hit the target...

                quote:

                First, what is your resultFormat, and what is the dataType of the items in the ArrayCollection? If your items are Objects, then there is no way to define an order, object properties are not ordered.

                Yes again...
                My items are objects... so I'm lost


                I've dscovered that SQLStatement class has a public property called itemClass
                With it, I can say to flex how it must fill an object, but in this way I lost reusability...
                at this point is more fast pre-built a specific DataGrid...

                Thanks for your advices ntsiii!!!!!
                • 5. Re: Sort DataGrid Columns built dynamically...
                  ntsiii Level 3
                  I'd advise making your server output XML. XML will retain the order of the child nodes (NOT attributes!)

                  You can then loop over the child nodes of the first "row" to build your columns.

                  Tracy
                  • 6. Re: Sort DataGrid Columns built dynamically...
                    123qweads Level 1
                    thanks for your advice ntsiii : )

                    but now I use a SQLite DB... so I prefer use Flex function...
                    but I've tried a solution (whit google help of course )

                    the idea is this:
                    After that datagrid is completed, I copy dg.colums in a temporaney tempArray....
                    then I swap the columns that are not in the right place.... and at the end, I set dg.columns=tempArray...

                    I tried this method time ago... but it didn't work because I tried to change directly db.columns[x]...

                    Now I'm satisfied!!!

                    bye bye