Flex:从绑定到数据网格的集合中删除项目并更新网格选定索引
我有一个绑定了 xmlListCollection 的数据网格:
<mx:DataGrid id="dgCompetente" includeIn="Competente" x="10" y="66" width="547" height="468"
change="dgCompetente_changeHandler(event)" dataProvider="{colCompetente}"
editable="false">
<mx:columns>
<mx:DataGridColumn headerText="ID Competenţă" dataField="idCompetenta"/>
<mx:DataGridColumn headerText="Denumire Competenţă" dataField="denCompetenta"/>
<mx:DataGridColumn headerText="Competenţă Superioară" dataField="idCompSuperioara" labelFunction="labelFunctionCompetentaSuperioara"/>
</mx:columns>
</mx:DataGrid>
以及一个用于删除数据网格中当前选定项目的按钮,该按钮已将此函数分配给单击事件:
<s:Button id="btnDeleteCompetenta" includeIn="Competente" x="813" y="65" label="Stergere" click="deleteCompetenta()"/>
private function deleteCompetenta():void
{
try {
var position:int = dgCompetente.selectedIndex;
if (position >= 0) {
colCompetente.removeItemAt(position);
dgCompetente.selectedIndex = position;
}
clearEdit(fieldsCompetente);
saveCompetente();
} catch (error:Error) {
errorHandler.defaultErrorHandler(error);
}
}
我希望 selectedIndex 保持不变。因此,如果我删除第 2 项,则应选择列表中的下一项。问题是,如果我删除第 2 项,第 3 项将被选中,我不知道为什么。
有人能告诉我我错过了什么吗?
谢谢你!
I have a datagrid with an xmlListCollection bound to it:
<mx:DataGrid id="dgCompetente" includeIn="Competente" x="10" y="66" width="547" height="468"
change="dgCompetente_changeHandler(event)" dataProvider="{colCompetente}"
editable="false">
<mx:columns>
<mx:DataGridColumn headerText="ID Competenţă" dataField="idCompetenta"/>
<mx:DataGridColumn headerText="Denumire Competenţă" dataField="denCompetenta"/>
<mx:DataGridColumn headerText="Competenţă Superioară" dataField="idCompSuperioara" labelFunction="labelFunctionCompetentaSuperioara"/>
</mx:columns>
</mx:DataGrid>
and a button to delete the currently selected item in the datagrid, which has this function assigned to the click event:
<s:Button id="btnDeleteCompetenta" includeIn="Competente" x="813" y="65" label="Stergere" click="deleteCompetenta()"/>
private function deleteCompetenta():void
{
try {
var position:int = dgCompetente.selectedIndex;
if (position >= 0) {
colCompetente.removeItemAt(position);
dgCompetente.selectedIndex = position;
}
clearEdit(fieldsCompetente);
saveCompetente();
} catch (error:Error) {
errorHandler.defaultErrorHandler(error);
}
}
I want the selectedIndex to remain the same. So, if I delete item 2, the next in the list should be selected. The problem is that if I delete item 2, item 3 will be selected and I have no idea why.
Can someone tell me what I'm missing?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它不起作用 selectedIndex 也不会在 updateCompleteHandler 中更新为好的值
但对于 value-1,如果强制 value+1,则取 value+2 并且选择从单击中转义,所以它是完全相同的。
It doesn't work selectedIndex don't update with the good value also in updateCompleteHandler
but with value-1 if force value+1, take value+2 and the selection escape from click, so it's exactly the same.
在更新 DataGrid 之前,您可能过早地重置了 selectedIndex,但很难准确判断,因为您的代码调用了一堆函数。尝试
添加一个“缓存位置”变量,如下所示:
然后修改您的deleteCompetenta方法
然后将updateCompleteHander添加到DataGrid:
并重置updateCompleteHandler中的selectedIndex:
这样可以解决问题吗?
You're probably resetting the selectedIndex too early, before the DataGrid is updated, but it's hard to tell exactly since your code calls a bunch of functions. Try this
Add a "cached position" variable, like this:
Then modify your deleteCompetenta method
Then add an updateCompleteHander to the DataGrid:
And reset the selectedIndex int he updateCompleteHandler:
Would that solve it?