Monday, January 28, 2008

Extending DataGrid and more conveniant way to style a row

The listItems tends to be the most useful way for styling things. and instead of iterating through all items for each row, I instantiate MessagesDataGridRow in getRowAt():

package com.sandman.view
{
import mx.controls.DataGrid;

public class MessagesDataGrid extends DataGrid
{
public function MessagesDataGrid() {
super();
}

public function getRowAt(rowIndex:int):MessagesDataGridRow {

var row:* = super.listItems[rowIndex];
return new MessagesDataGridRow(row);
}

}
}


MessagesDataGridRow:

package com.sandman.view
{
public class MessagesDataGridRow
{
private var row:*;
public function MessagesDataGridRow(itemRow:*) {
row = itemRow;
}

public function setStyle(attribute:String, value:String):void {
var length:int = row.length;
var i:int;
for (i=0; i<length; i++) {
row[i].setStyle(attribute, value);
}
}

}
}


Example usage:

var row:MessagesDataGridRow = yourGrid.getRowAt(3);
row.setStyle('fontWeight', 'bold');
row.setStyle('fontStyle', 'italic');


You'll likely want to modify this to suit your needs.

No comments: