访问 DataGrid 中的 ItemRenderer

发布于 2024-07-13 12:01:44 字数 654 浏览 4 评论 0原文

我有一个数据网格,在列中有一个复选框项目渲染器,以允许行选择:

主应用程序:


<mx:DataGrid id="dg">
    <mx:columns>
        <mx:DataGridColumn id="ir" itemRenderer="renderers.RowCheckbox" /> 
        <mx:DataGridColumn dataField="Name" headerText="Name" /> 
    </mx:columns>
</mx:DataGrid>

项目渲染器:


<-- RowCheckbox -->
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" horizontalAlign="center"> 
    <mx:CheckBox id="chk"/>
</mx:HBox>

如何获取项目渲染器的句柄/复选框以便我可以确定检查哪些行?

I have a data grid that has a checkbox item renderer in a cloumn to allow row selections:

Main application:


<mx:DataGrid id="dg">
    <mx:columns>
        <mx:DataGridColumn id="ir" itemRenderer="renderers.RowCheckbox" /> 
        <mx:DataGridColumn dataField="Name" headerText="Name" /> 
    </mx:columns>
</mx:DataGrid>

Item renderer:


<-- RowCheckbox -->
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" horizontalAlign="center"> 
    <mx:CheckBox id="chk"/>
</mx:HBox>

How can I get a handle to the item renderer / checkbox so that I may determine which rows are checked?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

柠檬色的秋千 2024-07-20 12:01:44

只是一句建议:我们在应用程序中遇到了类似的问题,我们通过向数据网格的数据提供程序中的实体添加“选定”属性来解决它。 然后,复选框的选定属性将绑定到我们实体的选定属性。 为了知道选择了哪些,我们只是循环遍历数据提供者中的实体而不是项目渲染器。 经过很多不同的方法之后,这确实是最好的选择。

如果我没记错的话,问题是项目渲染器没有正确记住所选状态,并且当您上下滚动时数据网格完全混乱。 滚动后选择了错误的行。

另一种选择是在项目渲染器中调度一个事件,该事件一直冒泡到托管数据网格的控件。 然后,您可以侦听这些事件并更新模型以反映更改。

Just a word of advice: We had a similar problem in our application and we solved it by adding a "selected" property to the entities in the dataprovider of the datagrid. The selected property of the checkBox was then bound to the selected property of our entity. To know which ones were selected, we just looped over the entities in the dataprovider instead of the item renderers. After a lot of different approaches, this really was the best option.

If I remember correctly, the problem was that the itemrenderers did not remember the selected state correctly and the datagrid was completely messed up when you scrolled up and down. The wrong rows were selected after scrolling.

Another option would be to dispatch an event in the item renderer which bubbles up all the way to the control hosting the datagrid. You could then listen for these events and update your model to reflect the changes.

黒涩兲箜 2024-07-20 12:01:44

我在 DataGrid 和多个项目渲染器以及滚动时重用项目渲染器方面遇到了类似的问题。 为了访问 DataGrid 项目渲染器,我扩展了 DataGrid。 我的第一个想法是使用indexToIndex(),然后使用indexToItemRenderer()。 不幸的是,这些方法没有达到我的预期,所以我添加了indicesToItemRenderer()方法:

package com.whatever.controls
{

import mx.controls.DataGrid;
import mx.controls.listClasses.IListItemRenderer;

public class CustomDataGrid extends DataGrid
{

    public function CustomDataGrid()
    {
        super();
    }

    public function indicesToItemRenderer(rowIndex:int, colIndex:int):IListItemRenderer
    {
        var firstItemIndex:int = verticalScrollPosition - offscreenExtraRowsTop;
        if (rowIndex < firstItemIndex ||
            rowIndex >= firstItemIndex + listItems.length
            )
        {
            return null;
        }

        return listItems[rowIndex - firstItemIndex][colIndex];
    }

}

要解决滚动时重用项目渲染器的问题,请参阅本文:

http: //www.adobe.com/devnet/flex/articles/itemrenderers_pt1.html

它归结为覆盖数据设置器并将属性存储在数据中。 例如,我有一列使用 CheckBox itemRenderer,另一列使用 ComboBox。 对于这两者,我都会侦听更改事件,并在属性更改时将 selected、selectedIndex 等存储在数据中,并覆盖数据设置器以设置这些属性:

        override public function set data(value:Object):void
        {
            if (value != null)
            {
                super.data = value;

                if (data.hasOwnProperty('selected') && data.selected)
                {
                    selected = data.selected;
                }
                else
                {
                    selected = false;
                }
            }
        }

I ran into similar issues with the DataGrid and multiple item renderers and the reuse of item renderers when scrolling. In order to access DataGrid item renderers I extended the DataGrid. My first thought was to use the indicesToIndex() followed by indexToItemRenderer(). Unfortunately these methods didn't do what I expected so I added the indicesToItemRenderer() method:

package com.whatever.controls
{

import mx.controls.DataGrid;
import mx.controls.listClasses.IListItemRenderer;

public class CustomDataGrid extends DataGrid
{

    public function CustomDataGrid()
    {
        super();
    }

    public function indicesToItemRenderer(rowIndex:int, colIndex:int):IListItemRenderer
    {
        var firstItemIndex:int = verticalScrollPosition - offscreenExtraRowsTop;
        if (rowIndex < firstItemIndex ||
            rowIndex >= firstItemIndex + listItems.length
            )
        {
            return null;
        }

        return listItems[rowIndex - firstItemIndex][colIndex];
    }

}

To resolve the reused item renderers when scrolling issue, refer to this article:

http://www.adobe.com/devnet/flex/articles/itemrenderers_pt1.html

It boils down to overriding the data setter and storing properties in data. For example, I had one column using a CheckBox itemRenderer and another column using ComboBox. For both I listen for the change event and store selected, selectedIndex, etc in data whenever properties change and override the data setter to set those properties:

        override public function set data(value:Object):void
        {
            if (value != null)
            {
                super.data = value;

                if (data.hasOwnProperty('selected') && data.selected)
                {
                    selected = data.selected;
                }
                else
                {
                    selected = false;
                }
            }
        }
小情绪 2024-07-20 12:01:44

您可以使用 ListBase 的所有子类公开的 indexToItemRenderer() 方法。

例如:

private function someFunction(index:int):void
{
    var rowCheckbox:RowCheckbox = dg.indexToItemRenderer(index) as RowCheckbox;
    trace(rowCheckbox.chk.selected.toString());
}

... 其中 index 表示要测试其“chk”属性的 DataGrid 项的索引。

You can use the indexToItemRenderer() method exposed by all subclasses of ListBase.

For example:

private function someFunction(index:int):void
{
    var rowCheckbox:RowCheckbox = dg.indexToItemRenderer(index) as RowCheckbox;
    trace(rowCheckbox.chk.selected.toString());
}

... where index represents the index of the DataGrid item whose "chk" property you want test.

最佳男配角 2024-07-20 12:01:44

在 ItemRenderer 中,尝试将 Checkbox 组件放入 VBox 中。解决了滚动问题。

In the ItemRenderer, try putting Checkbox Component in a VBox..resolves the scrolling issue.

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