Monday, June 15, 2009

Selecting All Rows in an Advanced Data Grid

Well, I just spent waaay to long writing a little bit of AS to select/deselect all the cells in a datagrid/AdvancedDataGrid. Maybe I'm slow, and try stuff without really doing enough research, but either way, I had this super simple idea.

Why use an itemRenderer to get a check box in an AdvancedDataGrid, when it has a selectMode of 'multipleRows'? So, I wanted to select and Deselect all of the rows in a single click. I know, kinda crazy right?

Anyways, I did some Googling, and looked at FlexCF.com and found nothing that really helped with what I wanted. There was a tute on the Flex3 LiveDocs that related to getting the data from selected Cells, but not really what I wanted. Anyways, this is what I came up with:

The AdvancedDataGrid(ADG from now on) uses an array to store the data, and as a reference to what rows/cells are selected. So to get it to select them all? Well just create the correct array of course!

Code:

public function selecterOfAll():void
{
var selectedRows:Array = new Array(); //Define the array that will hold the values for selection
if (selectAllOutput.label == "Select All") //Test what the button says - default 'Select all'
{
for( var rows:int = 0; rows <>
{
//Loop over the rows that are in the Datagrid. the variable holds the ArrayCollection for the datagrid. should also be able to do ADG.rowCount
selectedRows[rows]=rows
//Make the selectedRows Array look like what the ADG expects
}
//Make the assignment happen - the 'magic' really
this.masterList.selectedIndices = selectedRows;
//Change the label to clear the selection
selectAllOutput.label = "Deselect All";
}
else //the label must say 'Deselect all'
{
//Update the label so that it will enter the loop above again
this.selectAllOutput.label="Select All";
//This is the best way I could find to clear the array
selectedRows = new Array();
//Update the ADG to have nothing selected
this.masterList.selectedIndices = selectedRows;
}
}

So, that is all well and good, but what if someone wanted to clear something that they had already selected? Well I need another button for that to call a similar bit of code. And of course, some people may want to go back to what they had already selected. Anyways, I am going to be working with the folks at FlexCF to get this up as a demo on their site, with perhaps some more of these goodies in there. Hope that this helps anyone, and of course I welcome feedback on my blogging style/content, I am still just getting the hang of this :).

No comments:

Post a Comment