开发者

Adding a sortcomparefunction to Dynamic Data Grid in flex

I am trying to create a dynamic datagrid in Flex 3, I have a list of columns a list of objects which correspond to datapoints for those columns which I fetch from a url. While the grid works perfectly fine the problem is that sorting on the columns is done in lexical order.

I am aware that this can be fixed by adding a sortcomparefunction to a column, which is not easy for this case. I have tried doing

            var dgc:DataGridColumn = new DataGridColumn(dtf);
            f1[dtf] = function(obj1:Object, obj2:Object):int {
               return Comparators.sortNumeric(obj1[dtf],obj2[dtf]);
            };
            dgc.sortCompareFunction = f1[dtf];`

But the problem is that the function object that I am creating here is being overwritten in every iteration (as I am adding columns) and eventually all开发者_开发技巧 the columns will have sorting done only on the last column added.

Suggestions please.


Lets assume you have dynamicColumns array of some objects that you want create datagrid columns from.

var columns:Array = new Array();    
for (var i:int = 0; i < dynamicColumns.length; i++) {
    var column:DataGridColumn = new DataGridColumn(dynamicColumns[i].label);

    var dataGridColumnSortingHelper:DataGridColumnSortingHelper = new DataGridColumnSortingHelper(column);
    column.sortCompareFunction = dataGridColumnSortingHelper.columnSortCompareFunction;
    columns.push(column);
}
yourDataGrid.columns = columns;

Where DataGridColumnSortingHelper is like this:

public class DataGridColumnSortingHelper
{
    private var column:DataGridColumn;

    // Constructor
    public function DataGridColumnSortingHelper(column:DataGridColumn)
    {
        this.column = column;
    }

    // Public Methods
    public function columnSortCompareFunction(item1:Object, item2:Object):int
    {
        // define your custom function here and use column field to know what column you sorting by...

        return 0;
    }
}

Thanks Tom for comment.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜