Flex dataGrid default column sort

The below example  gives you a hook to sort a  particular column in a datagrid without user input. One can use the below function where ever a datagrid has to have a default sort order on its column(s).  Note : This works with flex 2 datagrid and not with Advanced datagrid in Moxie.

The dataGridDefaultSort functions requires two input items. The dgName which as the variable suggest is the name of the DataGrid and the dgColumn, DataGrid Column, to be sorted by the numbers. column numbering begins with zero.

These input items are used with the DataGridEvent below and is dispatched (excuted). The DataGridEvent has a total of 9 values for successful operation. They are listed below in expanded detail.

1. type : String — The event type; indicates the action that caused the event. This is represented by the dgName input variable. The DataGridEvent.HEADER_RELEASE constant defines the value of the type property of the event object for a headerRelease event, which indicates that the user pressed and released the mouse on a column header
2. bubbles : Boolean (default = false) — Specifies whether the event can bubble up the display list hierarchy.
3. cancelable : Boolean (default = false) — Specifies whether the behavior associated with the event can be prevented.
4. columnIndex : int (default = -1) — The zero-based index of the column where the event occurred. This is represented by the dgColumn input variable.
5. dataField : String (default = null) — The name of the field or property in the data associated with the column.
6. rowIndex : int (default = -1) — The zero-based index of the item in the in the data provider.
7. reason : String (default = null) — The reason for an itemEditEnd event.
8. itemRenderer : IListItemRenderer (default = null) — The item renderer that is being edited or the header renderer that was clicked.
9. localX : Number — Column x-position for replaying columnStretch events.

Call this function anywhere in the program and  use as per above attributes.

1
2
3
private function dataGridDefaultSort (dgName:Object, dgColumn:int): void {
dgName.dispatchEvent(new DataGridEvent(DataGridEvent.HEADER_RELEASE,   false,true,dgColumn,null,0,null,null,0));
}

Related posts:

  1. Copy functionality in DataGrid
  2. Search dataGrid and Simple dataGrid Itemrenderer example
  3. Printing dataGrid in Flex
  4. Filter datagrid using a hslider
  5. Dynamically add remove columns in a datagrid

Tags: , , , ,

Leave a comment