·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]as3数组元素如何实现分页功能代码示例(2)

时间:2012-09-14 14:04bbs.9ria.com
实现分页的类 packagepages { publicclassPageManager { privatevaritems:Array; //当前索引 privatevar _index:int = 0 ; publicfunctionPageManager() { this.items = new Array; _index = 0 ; } /** *当前页索引

 实现分页的类

  1. package pages  
  2. {  
  3.           
  4.  
  5.         public class PageManager  
  6.         {  
  7.                 private var items:Array;  
  8.  
  9.                 //当前索引  
  10.                 private var _index:int=0;  
  11.  
  12.                 public function PageManager()  
  13.                 {  
  14.                         this.items=new Array;  
  15.                         _index=0;  
  16.                 }  
  17.  
  18.                 /**  
  19.                  *当前页索引  
  20.                  * @return  
  21.                  *  
  22.                  */  
  23.                 public function get index():int  
  24.                 {  
  25.                         return current.index;  
  26.                 }  
  27.  
  28.                 /**  
  29.                  *对数组执行分页  
  30.                  * @param items  
  31.                  * @param pagePerSize 每页大小  
  32.                  * @return  分页后的第一页  
  33.                  *  
  34.                  */  
  35.                 public function page(items:Array, pagePerSize:int=6):Page  
  36.                 {  
  37.                         var page:Page=null;  
  38.                         if (items != null && items.length > 0 && pagePerSize > 0)  
  39.                         {  
  40.                                 if (pagePerSize < items.length)  
  41.                                 {  
  42.                                         //最后一页的数目  
  43.                                         var left:int=items.length % pagePerSize;  
  44.                                         var pageCount:int=(items.length - left) / pagePerSize;  
  45.                                         for (var i:int=0; i < pageCount; i++)  
  46.                                         {  
  47.                                                 page=new Page();  
  48.                                                 //每页的容量  
  49.                                                 page.capacity=pagePerSize;  
  50.                                                 for (var j:int=0; j < pagePerSize; j++)  
  51.                                                 {  
  52.                                                         page.add(items[i * pagePerSize + j]);  
  53.                                                 }  
  54.                                                 //页索引(从0开始)  
  55.                                                 page.index=i;  
  56.                                                 this.items.push(page);  
  57.                                         }  
  58.                                         //最后一页  
  59.                                         if (left > 0)  
  60.                                         {  
  61.                                                 page=new Page();  
  62.                                                 page.capacity=pagePerSize;  
  63.                                                 page.index=pageCount;  
  64.                                                 for (i=items.length - left; i < items.length; i++)  
  65.                                                 {  
  66.                                                         page.add(items[i]);  
  67.                                                 }  
  68.                                                 this.items.push(page);  
  69.                                         }  
  70.                                         if (this.items.length > 0)  
  71.                                         {  
  72.                                                 page=this.items[0] as Page;  
  73.                                         }  
  74.                                 }  
  75.                                 else  
  76.                                 {  
  77.                                         //所有元素当做一页  
  78.                                         page=new Page();  
  79.                                         page.capacity=items.length;  
  80.                                         page.index=0;  
  81.                                         for (var t:int=0; t < items.length; t++)  
  82.                                         {  
  83.                                                 page.add(items[t]);  
  84.                                         }  
  85.                                         this.items.push(page);  
  86.                                 }  
  87.                         }  
  88.                         return page;  
  89.                 }  
  90.  
  91.                 public function next():Page  
  92.                 {  
  93.                         var page:Page=null;  
  94.                         if (hasNext())  
  95.                         {  
  96.                                 _index++;  
  97.                                 page=items[_index] as Page;  
  98.                         }  
  99.                         return page;  
  100.                 }  
  101.  
  102.                 public function previous():Page  
  103.                 {  
  104.                         var page:Page=null;  
  105.                         if (hasPrevious())  
  106.                         {  
  107.                                 _index--;  
  108.                                 page=items[_index] as Page;  
  109.                         }  
  110.                         return page;  
  111.                 }  
  112.  
  113.                 public function hasNext():Boolean  
  114.                 {  
  115.  
  116.                         var idx:int=_index;  
  117.                         return (idx + 1) in items;  
  118.  
  119.                 }  
  120.  
  121.                 public function hasPrevious():Boolean  
  122.                 {  
  123.                         var idx:int=_index;  
  124.                         return (idx - 1) in items;  
  125.                 }  
  126.  
  127.                 /**  
  128.                  * 页数  
  129.                  * @return  
  130.                  *  
  131.                  */  
  132.                 public function get count():int  
  133.                 {  
  134.                         return items.length;  
  135.                 }  
  136.  
  137.                 /**  
  138.                  *获取指定页  
  139.                  * @param index  
  140.                  * @return  
  141.                  *  
  142.                  */  
  143.                 public function getPageAt(index:int):Page  
  144.                 {  
  145.                         var obj:Page=null;  
  146.                         if (index in items)  
  147.                         {  
  148.                                 obj=items[index] as Page;  
  149.                         }  
  150.                         return obj;  
  151.                 }  
  152.  
  153.                 /**  
  154.                  *当前页  
  155.                  * @return  
  156.                  *  
  157.                  */  
  158.                 public function get current():Page  
  159.                 {  
  160.                         var page:Page=null;  
  161.                         if (_index in items)  
  162.                         {  
  163.                                 page=items[_index] as Page;  
  164.                         }  
  165.                         return page;  
  166.                 }  
  167.  
  168.                 /**  
  169.                  *字符串描述  
  170.                  * @return  
  171.                  *  
  172.                  */  
  173.                 public function toString():String  
  174.                 {  
  175.                         return items.toString();  
  176.                 }  
  177.         }  

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器