Search code examples
apache-flexflex4flex-spark

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);
        }
    }

Solution

  • 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.