2
votes

I want to save remotely (on a database) the state (visible columns, columns width and order) of a Flex3 DataGrid.

For width and visibility I can simply save them by accessing each column attribute.. ugly but possible.. But for the order? Do I have to create the dataGrid dynamically??

Any idea is appreciated thanks

2
are you talking about the order of multiple datagrids in respect of how they are laid out? or the sort order of the columns? - Shua
the order of the columns, meaning the headers (not the sorting of each column) - luca

2 Answers

1
votes

In my case I have saved the order by header name (I'm making an assumption that your DataGrid always has the same columns and header names).

        for (var n:Number = 0; n< datagrid.columns.length; n++)
        {
            var thiscol:DataGridColumn = DataGridColumn(datagrid.columns[n]);
            colArray.addItem(thiscol.headerText);
        }

Then I can restore the column order by retrieving the ordered list of column headers, and swapping the position of columns in the datagrid as required.

        for (var n:Number = 0; n < colArray.length; n++)
        {
            moveColumnTo(String(colArray.getItemAt(n)), n);
        }

I have defined a function moveColumnTo() to perform the switch.

        private function moveColumnTo(columnName:String, columnIndex:Number):void
        {
            // Find current column position
            var i:Number = -1;
            for (var n:Number = 0; n < datagrid.columns.length; n++)
            {
                if (DataGridColumn(datagrid.columns[n]).headerText == columnName)
                {
                    i = n;
                    break;
                }
            }

            if (i == -1 || i == columnIndex) return; // Don't shift column

            this.mx_internal::shiftColumns(i, columnIndex); // Shift column to required position
        }
0
votes

Why can't you just save the order as you're looping through each column?

for(var i:int = 0; i < dataGrid.columns.size; i++)
{
    var column:DataGridColumn = dataGrid.columns[i];
    arr.push({column.visible, column.width, i});
}