setSelected() in dojo DataGrid leaves previous selection active even for grid with selectionMode="single"

Kevin Beck picture Kevin Beck · Jun 29, 2012 · Viewed 11.9k times · Source

I have a dojox.grid.DataGrid where I want to select a row programmatically. I'm using setSelected() to do so and it works the first time. However, calling it a second time for a different row leaves the previous row highlighted. Also, if I try to reselect a row that was previously selected, the onSelected event does not fire. But if I actually click in the grid, it clears things up: rows that were highlighted in the grid before get unhighlighted and unselected.

The code looks like so:

if (grid.rowCount > 0 && idx < grid.rowCount)
{
    grid.selection.setSelected(idx, true);
    grid.render();
}

It is as if I had multi-select enabled, but I have declared the grid as selectionMode="single".

<table dojoType="dojox.grid.DataGrid"
    id="hotTablesForAppDg"
    autoWidth="true" autoHeight="true" selectionMode="single"
    onSelected="autonomics.Clusters.loadTableDetails(this)">

Is there something else I need to call to clear the previous selection?

Answer

Kevin Beck picture Kevin Beck · Jun 29, 2012

Problem solved. You need to call setSelected(..., false) on the currently selected index:

if (grid.rowCount > 0 && idx < grid.rowCount)
{
    if (grid.selection.selectedIndex >= 0)
    {
        // If there is a currently selected row, deselect it now
        grid.selection.setSelected(grid.selection.selectedIndex, false);
    }
    grid.selection.setSelected(idx, true);
    grid.render();
}