给DataGrid添加一个静态分页组件
之前已经发了好几篇演示flex的DataGrid强大特性的文章,今天发篇教程,给DataGrid添加个静态分页组件。网上关于给DataGrid添加动态分页的教程好多。这里的静态分页是指数据已经全部派发完毕,客服端进行的分页。
这个类是我自己写的,难免会有bug,也有不完善的地方,先放出,以后慢慢改善。
先来看演示:
说明下文档结构:
libs:分页类所在文件夹
images:分页组件所用到的按钮图片
DataGrid7.mxml:主程序入口
主程序代码说明:
(沿用之前所用的DataGrid示例,重复部分不再贴出,详见源代码)
- internal function completeHandler(evt:ResultEvent):void{
- sourceArrCollection = evt.result.rss.channel.item;
- GridPage = new Page(sourceArrCollection,currentPage,perPage);
- GridPage.pageBar.width = 526;
- GridPage.pageBar.height = 32;
- grid_vessel.addChild(GridPage.pageBar);
- }
这是数据加载后执行的回调函数。
GridPage = new Page(sourceArrCollection,currentPage,perPage);实例化分页组件。
接受三个参数:
- sourceArrCollection : 数据源(为ArrayCollection实例)
- currentPage:所要显示的当前页,默认为1
- perPage:每页的记录数,默认为10
grid_vessel.addChild(GridPage.pageBar);
pageBar这个属性值是GridPage生成的分页容器,为HBox实例。
分页类代码:
- package libs
- {
- import flash.events.MouseEvent;
- import mx.collections.ArrayCollection;
- import mx.containers.HBox;
- import mx.controls.Image;
- import mx.controls.LinkButton;
- import mx.controls.NumericStepper;
- import mx.controls.Text;
- public class Page
- {
- //数据源
- [Bindable] public var sourceArrCollection:ArrayCollection;
- //每页条目数
- [Bindable] public var perPage:uint;
- //当前页码
- [Bindable] public var currentPage:uint;
- //总页数
- [Bindable] public var totalPages:uint;
- //总记录数
- [Bindable] public var totalItems:uint;
- //当前页的数据储存器
- [Bindable] public var currentPageCollection:ArrayCollection;
- [Bindable] public var icons:ArrayCollection;
- public var pageBar:HBox;
- public var btns:Object = {};
- public var text:Text;
- private var _dataCollection:ArrayCollection;
- private var _start:uint = 0;
- private var _end:uint = 0;
- private var _btnIcons:Array = ['images/first.gif','images/prev.gif','images/next.gif','images/last.gif'];
- private var _btnDisableIcons:Array = ['images/first_disabled.gif','images/prev_disabled.gif','images/next_disabled.gif','images/last_disabled.gif'];
- public function Page(sourceArrCollection:ArrayCollection = null,currentPage:uint = 1,perPage:uint = 10):void
- {
- this.sourceArrCollection = sourceArrCollection;
- this.currentPage = currentPage;
- this.perPage = perPage;
- this.totalItems = this.sourceArrCollection.length;
- this.totalPages = Math.ceil(this.totalItems / this.perPage);
- icons = new ArrayCollection(this._btnIcons);
- this._create();
- this.getData(currentPage);
- this.btns.next.addEventListener(MouseEvent.CLICK,nextHandler);
- this.btns.prev.addEventListener(MouseEvent.CLICK,prevHandler);
- this.btns.first.addEventListener(MouseEvent.CLICK,firstHandler);
- this.btns.last.addEventListener(MouseEvent.CLICK,lastHandler);
- this.btns.jump.addEventListener(MouseEvent.CLICK,jumpHandler);
- var _that:* = this;
- function nextHandler():void{
- if(_that.btns.next.source != _that._btnDisableIcons[2]) _that.next();
- }
- function prevHandler():void{
- if(_that.btns.prev.source != _that._btnDisableIcons[1]) _that.prev();
- }
- function firstHandler():void{
- if(_that.btns.first.source != _that._btnDisableIcons[0]) _that.goToFirst();
- }
- function lastHandler():void{
- if(_that.btns.last.source != _that._btnDisableIcons[3]) _that.goToLast();
- }
- function jumpHandler():void{
- var _num:uint = _that.btns.PageNum.value;
- _that.getData(_num);
- }
- }
- /**
- * 获取指定页的数据储存器
- * @param pageNum {uint} 指定页
- * @return {ArrayCollection}
- *
- */
- public function getData(pageNum:uint):ArrayCollection{
- if(this.sourceArrCollection != null && this.totalItems > 0){
- this.currentPageCollection = new ArrayCollection();
- this._end = pageNum * this.perPage;
- this._start = this._end - this.perPage;
- if(pageNum == this.totalPages) this._end = this.totalItems;
- this.currentPage = pageNum;
- this._setBtnEnabled();
- this.text.text = "第"+this.currentPage+"页/共"+this.totalPages+"页"+this.totalItems+"条记录";
- for(var i:int=this._start;i<this._end;i++){
- this.currentPageCollection.addItem(this.sourceArrCollection.getItemAt(i));
- }
- return this.currentPageCollection;
- }
- return null;
- }
- /**
- * 下一页
- */
- public function next():void{
- if(this.currentPage < this.totalPages){
- this.currentPage ++;
- this.getData(this.currentPage);
- }
- }
- /**
- * 上一页
- */
- public function prev():void{
- if(this.currentPage >1){
- this.currentPage --;
- this.getData(this.currentPage);
- }
- }
- /**
- * 前往第一页
- */
- public function goToFirst():void{
- this.currentPage = 1;
- this.getData(this.currentPage);
- }
- /**
- * 前往最后一页
- */
- public function goToLast():void{
- this.currentPage = this.totalPages;
- this.getData(this.currentPage);
- }
- /**
- * 获取总页数
- */
- public function getTotalPages():int{
- return this.totalPages;
- }
- /**
- * 获取记录总数
- */
- public function getTotalItems():int{
- return this.totalItems;
- }
- /**
- * 设置分页工具条的enabled
- */
- private function _setBtnEnabled():void{
- if(this.currentPage == 1){
- this.btns.first.source = this._btnDisableIcons[0];
- this.btns.prev.source = this._btnDisableIcons[1];
- }
- else if(this.currentPage == this.totalPages){
- this.btns.next.source = this._btnDisableIcons[2];
- this.btns.last.source = this._btnDisableIcons[3];
- }
- else{
- this.btns.first.source = this._btnIcons[0];
- this.btns.prev.source = this._btnIcons[1];
- this.btns.next.source = this._btnIcons[2];
- this.btns.last.source = this._btnIcons[3];
- }
- }
- /**
- * 创建分页工具条
- */
- private function _create():void{
- this.pageBar = new HBox();
- this.pageBar.styleName = "pageBar";
- this. _createBtns();
- this.pageBar.addChild(this.btns.PageNum);
- this.pageBar.addChild(this.btns.jump);
- this.pageBar.addChild(this.btns.first);
- this.pageBar.addChild(this.btns.prev);
- this.pageBar.addChild(this.btns.next);
- this.pageBar.addChild(this.btns.last);
- this.text = new Text();
- this.pageBar.addChild(this.text);
- }
- private function _createBtn(source:String):Image{
- var btn:Image = new Image();
- btn.source = source;
- btn.width = 16;
- btn.height = 16;
- btn.styleName = "pageBarBtn";
- btn.addEventListener(MouseEvent.MOUSE_OVER,mouseOverHandler);
- function mouseOverHandler(evt:MouseEvent):void{
- //btn.setStyle("borderStyle","solid");
- }
- return btn;
- }
- private function _createBtns():void{
- this.btns.first = this._createBtn(this._btnIcons[0]);
- this.btns.prev = this._createBtn(this._btnIcons[1]);
- this.btns.next = this._createBtn(this._btnIcons[2]);
- this.btns.last = this._createBtn(this._btnIcons[3]);
- var _NumericStepper:NumericStepper = new NumericStepper();
- _NumericStepper.stepSize = 1;
- _NumericStepper.minimum = 1;
- _NumericStepper.maximum = this.totalPages;
- this.btns.PageNum = _NumericStepper;
- var _jump:LinkButton = new LinkButton();
- _jump.label = "跳转";
- this.btns.jump = _jump;
- }
- }
- }




上传到网上后发现组件的图片显示有些问题,本地浏览很正常。下次的demo将把图片直接嵌到flash中
回复