【问题标题】:how to limit the rows of datagrid in flex3?如何限制flex3中datagrid的行数?
【发布时间】:2011-10-13 06:55:11
【问题描述】:

我有一个由 3 列和多行组成的数据网格,我只想向用户显示前 20 行。有什么方法可以只显示我的数据网格中的前 20 行。单击“下一步”按钮后,应该显示下 20 行,依此类推...

【问题讨论】:

    标签: apache-flex actionscript-3 datagrid flex3


    【解决方案1】:

    如果您有一个列表作为数据提供者(ArrayCollection 左右),您可以通过use filterFunction 过滤您的列表。

    示例代码在这里:

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application initialize="init()" layout="absolute" minHeight="600" minWidth="955"
        xmlns:mx="http://www.adobe.com/2006/mxml">
        <mx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.utils.StringUtil;
    
            private static const DP_LENGTH:int = 100;
            private static const VISIBLE_ROWS_COUNT:int = 20;
    
            [Bindable]
            private var currentPage:int = 0;
    
            [Bindable]
            private var dataProvider:ArrayCollection;
    
            protected function init():void
            {
                var dpArray:Array = [];
                for (var i:int = 0; i < DP_LENGTH; i++)
                {
                    var item:Object = { first: i, second: Math.random(), third: Math.random() };
                    dpArray.push(item);
                }
                dataProvider = new ArrayCollection(dpArray);
                dataProvider.filterFunction = pagingFilterFunction;
                dataProvider.refresh();
            }
    
            protected function nextPage():void
            {
                currentPage++;
                dataProvider.refresh();
            }
    
            protected function prevPage():void
            {
                currentPage--;
                dataProvider.refresh();
            }
    
            private function pagingFilterFunction(item:Object):Boolean
            {
                var start:int = currentPage * VISIBLE_ROWS_COUNT;
                var end:int = start + VISIBLE_ROWS_COUNT - 1;
                var index:int = dataProvider.getItemIndex(item);
                return (index >= start) && (index <= end);
            }
        ]]>
        </mx:Script>
        <mx:VBox horizontalAlign="center" horizontalCenter="0" verticalCenter="0">
            <mx:DataGrid dataProvider="{dataProvider}">
                <mx:columns>
                    <mx:DataGridColumn dataField="first" headerText="First" />
                    <mx:DataGridColumn dataField="second" headerText="Second" />
                    <mx:DataGridColumn dataField="third" headerText="Third" />
                </mx:columns>
            </mx:DataGrid>
            <mx:Label 
                text="{StringUtil.substitute('Page {0} of {1}', currentPage + 1, Math.floor ((DP_LENGTH - 1) / VISIBLE_ROWS_COUNT) + 1)}" />
            <mx:HBox>
                <mx:Button click="prevPage()" enabled="{currentPage > 0}" label="Prev" />
                <mx:Button click="nextPage()" enabled="{DP_LENGTH / VISIBLE_ROWS_COUNT - 1 > currentPage}" label="Next" />
            </mx:HBox>
        </mx:VBox>
    </mx:Application>
    

    【讨论】:

    • 是的,我使用 ArrayCollection 作为数据提供者。你能告诉我如何使用filterFunction吗?谢谢你..
    • 我添加了一个示例代码,演示了filterFunction 用于分页的用法。
    猜你喜欢
    • 2014-06-16
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 2016-12-01
    • 2012-08-23
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多