我有一个dojox.grid.DataGrid,我想以编程方式选择一行.我正在使用setSelected()这样做,它第一次工作.但是,对于不同的行再次调用它会使前一行突出显示.此外,如果我尝试重新选择之前选择的行,则不会触发onSelected事件.但是,如果我实际上在网格中单击,它会清除:在未被突出显示和未选中之前在网格中突出显示的行.
代码如下:
if (grid.rowCount > 0 && idx < grid.rowCount)
{
grid.selection.setSelected(idx, true);
grid.render();
}
Run Code Online (Sandbox Code Playgroud)
就好像我启用了多选,但我已将网格声明为selectionMode ="single".
<table dojoType="dojox.grid.DataGrid"
id="hotTablesForAppDg"
autoWidth="true" autoHeight="true" selectionMode="single"
onSelected="autonomics.Clusters.loadTableDetails(this)">
Run Code Online (Sandbox Code Playgroud)
还有什么我需要打电话来清除之前的选择吗?
问题解决了.您需要在当前选定的索引上调用setSelected(...,false):
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();
}
Run Code Online (Sandbox Code Playgroud)