flex 中使用分页,要数据库支持

类别:编程语言 点击:0 评论:0 推荐:

使用方法

<PageSelector id="pageSelector"

                             numPagesToDisplay="5"    <!--当前界面一次显示几页-->

                             pageSelected="kmfspx(kmfstaxis)"  <!---->

                             borderStyle="none" 

                            showZero="0"    <!--是否显示0  及如果有100页 第一页显示为 001 -->

                            MaxRows="1212"   <!--拥有的记录数-->

                           SimplePageRows="20"  <!--多少条记录是一页-->

/>

查询时要用到的变量有 

as 文件中        pageSelector.Startrow  当前选择页对应的数据库开始记录 

                        pageSelector.Endrow   当前选择页对应的数据库结束记录 

 

PageSelector.mxml 文件内容

<?xml version="1.0" encoding="utf-8"?>
<!--
 
-->
<mx:HBox xmlns:mx="http://www.macromedia.com/2003/mxml" visible="{totalPages > 0}" horizontalGap="-1" >

    <mx:Script>
        <![CDATA[

        private var __currentPage : Number = 1;
        private var __totalPages : Number = 0;
        private var repeaterStartIndex : Number = 0;
        private var pageStringLength : Number;
        private var currentButton : mx.controls.Link;
      
        public var __showZero:Number=0;
  
  private var __MaxRows: Number =0;//得到的总行数
  private var __SimplePageRows: Number;//单页要有的行数

 

  //通过页数计算开始记录数
  function get Startrow():Number
  {
   return (((currentPage-1)*SimplePageRows)+1);
  }
  
  //返回当前页的结束记录数
  function get Endrow():Number
  {
   var re:Number=0;
   if (currentPage != totalPages)
   {
    re = currentPage*SimplePageRows;
   }
   else if (currentPage == totalPages)
   {
    re =MaxRows;
   }
   
   return re;
  }


        [ChangeEvent("MaxRowsChanged")]
        function get showZero() : Number
        {
            return __showZero;
        }
          
       function set showZero(tp : Number) : Void
        {
         __showZero=tp;
         mathcountpage();
  }


  
        [ChangeEvent("MaxRowsChanged")]
        function get MaxRows() : Number
        {
            return __MaxRows;
        }
          
       function set MaxRows(tp : Number) : Void
        {
         __MaxRows=tp;
         mathcountpage();
  }

        [ChangeEvent("SimplePageRowsChanged")]
        function get SimplePageRows() : Number
        {
            return __SimplePageRows;
        }
          
       function set SimplePageRows(tp : Number) : Void
        {
         __SimplePageRows=tp;
         mathcountpage();
  }
  
  function mathcountpage()
  {
   var a:Number = __MaxRows ;
   var b:Number = __SimplePageRows;
         totalPages =(a -(a%b))/b +(((a%b)>0)?1:0);   
  }
  
        [ChangeEvent("pageArrayChanged")]
        private var pageArray : Array;

        public var numPagesToDisplay : Number;

        [ChangeEvent("pageSelected")]
        function get currentPage() : Number
        {
            return __currentPage;
        }

        function set currentPage(cp : Number) : Void
        {
            __currentPage = cp;
            dispatchEvent({type: 'pageSelected'});
        }

        [ChangeEvent("totalPagesChanged")]
        function get totalPages() : Number
        {
            return __totalPages;
        }

        function set totalPages(tp : Number) : Void
        {
            __totalPages = tp;
            if (pageArray == null) pageArray = new Array();
            if (tp > pageArray.length)
            {
                for (var i = pageArray.length+1; i <= tp; ++i)
                {
                    pageArray.push(i);
                }
            }
            else if (tp < pageArray.length)
            {
                pageArray.splice(tp);
            }

            pageStringLength = String(tp).length;

            dispatchEvent({type: 'totalPagesChanged'});
            dispatchEvent({type: 'pageArrayChanged'});
        }

        private function moveToBeginning()
        {
            repeaterStartIndex = 0;
            updateCurrentButton(undefined);
        }

        private function moveToEnd()
        {
            var mod = totalPages % numPagesToDisplay;
            if (mod == 0) mod = numPagesToDisplay;
            repeaterStartIndex = totalPages - mod;
            updateCurrentButton(undefined);
        }

        private function previousPageGroup()
        {
            if (repeaterStartIndex - numPagesToDisplay >= 0)
            {
                updateCurrentButton(undefined);
                repeaterStartIndex -= numPagesToDisplay;
            }
        }

        private function nextPageGroup()
        {
            if (repeaterStartIndex + numPagesToDisplay < totalPages)
            {
                updateCurrentButton(undefined);
                repeaterStartIndex += numPagesToDisplay;
            }
        }

        private function selectPage(page) : Void
        {
            currentPage = page.getRepeaterItem();
            updateCurrentButton(page);
        }

        function updateCurrentPage(newCurrentPage : Number) : Void
        {
            var butIndex = newCurrentPage % numPagesToDisplay;
            repeaterStartIndex = newCurrentPage - butIndex;
            selectPage(pageButton[butIndex]);
        }

        private function updateCurrentButton(cb : mx.controls.Link) : Void
        {
            if (currentButton != undefined)
            {
                currentButton.setStyle("color", undefined);
            }
            if (cb != undefined)
            {
                cb.setStyle("color", 0xAA0000);
            }
            currentButton = cb;
        }

        private function formatPageNumber(num : Number) : String
        {
            var str : String = String(num);
            var diff : Number = pageStringLength - str.length;     
            if (showZero==1)   
            switch(diff)
            {
                case 0 : return str;
                case 1 : return "0" + str;
                case 2 : return "00" + str;
                case 3 : return "000" + str;
                case 4 : return "0000" + str;
                case 5 : return "00000" + str;
                default : return str; //don't worry about it
            }
            else
            {
             return str;
            }
 
 
        }

        ]]>
    </mx:Script>

    <mx:Metadata>
        [Event("pageSelected")]
    </mx:Metadata>

    <mx:Link id="beginningButton" icon="@Embed('left2.jpg')"  height="22" click="moveToBeginning()" enabled="{repeaterStartIndex > 0}"/>

    <mx:Link id="prevPageButton" icon="@Embed('left.jpg')"  height="22" click="previousPageGroup()" enabled="{repeaterStartIndex > 0}"/>
    <mx:Repeater id="pageRepeater" dataProvider="{pageArray}" recycleChildren="true"
        startingIndex="{repeaterStartIndex}" count="{numPagesToDisplay}">

        <mx:Link id="pageButton" label="{formatPageNumber(pageRepeater.currentItem)}" click="selectPage(event.target)" />

    </mx:Repeater>
    <mx:Link id="nextPageButton" icon="@Embed('right.jpg')" height="22" click="nextPageGroup()" enabled="{repeaterStartIndex + numPagesToDisplay &lt; totalPages}"/>
    <mx:Link id="endButton" icon="@Embed('right2.jpg')"  height="22"  click="moveToEnd()" enabled="{repeaterStartIndex + numPagesToDisplay &lt; totalPages}"/>

</mx:HBox>

本文地址:http://com.8s8s.com/it/it24865.htm