1.21 gigawatts
1.21 gigawatts

Reputation: 17762

Ensure Spark DataGrid item is visible

Is there a way to make sure the selected item is visible in the Spark DataGrid?

.

Context
I have a data grid bound to an array collection. I receive remotely a service that gives me a ID (string) of an object that is in the collection. Using just the string I loop through the collection to find the item that matches the string. I find the object by it's ID. Now I have the object I want to select in the datagrid. I can set the

dataGrid.selectedItem = object; 

Now I need to make sure it's visible. I do not have the row or column index.

.

Update
Using the answer below I've complimented it with this function:

    /**
     * Ensures the item is visible (for spark data grid)
     **/
    public function ensureItemIsVisibleInSparkDataGrid(datagrid:spark.components.DataGrid, item:Object):void {
        var list:IList = datagrid.dataProvider;
        var length:int = list.length;
        var itemFound:Boolean;
        var object:Object;
        var index:int;

        for (var i:int;i<length;i++) {
            object = list.getItemAt(i);

            if (object==item) {
                itemFound = true;
                index = i;
                break;
            }
        }

        if (itemFound) {
            datagrid.ensureCellIsVisible(index);
        }
    }

Upvotes: 0

Views: 1429

Answers (1)

NoobsArePeople2
NoobsArePeople2

Reputation: 1996

Yes, it's called ensureCellIsVisible(). You need to know that row and column of the item in question. To get this to work you'd need to listen for the selectionChange event then calculate the row and column of the currently selected item.

Upvotes: 1

Related Questions