Flex:从绑定到数据网格的集合中删除项目并更新网格选定索引

发布于 2024-10-18 06:48:12 字数 1442 浏览 1 评论 0原文

我有一个绑定了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

波浪屿的海角声 2024-10-25 06:48:12

它不起作用 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.

夜未央樱花落 2024-10-25 06:48:12

在更新 DataGrid 之前,您可能过早地重置了 selectedIndex,但很难准确判断,因为您的代码调用了一堆函数。尝试

添加一个“缓存位置”变量,如下所示:

protected var cachedPosition : int;

然后修改您的deleteCompetenta方法

private function deleteCompetenta():void 
{
    try {
        cachedPosition = dgCompetente.selectedIndex;

        if (position >= 0) {
            colCompetente.removeItemAt(position);
//            don't reset this here
//            dgCompetente.selectedIndex = position;
        }

        clearEdit(fieldsCompetente);
        saveCompetente();
    } catch (error:Error) {
        errorHandler.defaultErrorHandler(error);
    }
}

然后将updateCompleteHander添加到DataGrid:

<mx:DataGrid id="dgCompetente" includeIn="Competente" x="10" y="66" width="547" height="468"
                 change="dgCompetente_changeHandler(event)" dataProvider="{colCompetente}"
                 editable="false" updateComplete="updateCompleteHandler(event)">

并重置updateCompleteHandler中的selectedIndex:

protected function updateCompleteHandler(event:Event):void{
// reset here
 dgCompetente.selectedIndex = cachedPosition;
}

这样可以解决问题吗?

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:

protected var cachedPosition : int;

Then modify your deleteCompetenta method

private function deleteCompetenta():void 
{
    try {
        cachedPosition = dgCompetente.selectedIndex;

        if (position >= 0) {
            colCompetente.removeItemAt(position);
//            don't reset this here
//            dgCompetente.selectedIndex = position;
        }

        clearEdit(fieldsCompetente);
        saveCompetente();
    } catch (error:Error) {
        errorHandler.defaultErrorHandler(error);
    }
}

Then add an updateCompleteHander to the DataGrid:

<mx:DataGrid id="dgCompetente" includeIn="Competente" x="10" y="66" width="547" height="468"
                 change="dgCompetente_changeHandler(event)" dataProvider="{colCompetente}"
                 editable="false" updateComplete="updateCompleteHandler(event)">

And reset the selectedIndex int he updateCompleteHandler:

protected function updateCompleteHandler(event:Event):void{
// reset here
 dgCompetente.selectedIndex = cachedPosition;
}

Would that solve it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文