如何限制flex3中datagrid的行数?

发布于 2024-11-26 17:52:14 字数 105 浏览 1 评论 0原文

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

I have a data grid consisting of 3 columns & many rows, i want to show only the first 20 rows to user. Is there any way i can show only the first 20 rows in my datagrid.After by clicking button 'next', next 20 rows should display and so on...

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

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

发布评论

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

评论(1

面如桃花 2024-12-03 17:52:14

如果您有一个列表作为数据提供程序(ArrayCollection 左右),您可以 使用 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>

If you have a list as a data provider (ArrayCollection or so) you can use filterFunction to filter your list.

The sample code is here:

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