Posted By

brajendra on 09/13/11


Tagged


Versions (?)

mxtest


 / Published in: MXML
 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" horizontalGap="0">
  3. <mx:Script>
  4. <![CDATA[
  5. import mx.effects.Move;
  6. import events.*;
  7. import mx.controls.Alert;
  8.  
  9. private var _selectedBtn:Button = null;
  10. private var _selectedIndex:int = 0;
  11. private var _itemsPerPage:int = 0;
  12. private var _totalItems:int = 0;
  13. private var _pages:int = 0;
  14. private var _visiblePages:int = 0;
  15. private var _dataProvider:Array = [];
  16.  
  17. private var pageMove:Move = new Move();
  18.  
  19. public function get selectedIndex():int
  20. {
  21. return _selectedIndex;
  22. }
  23. public function set selectedIndex(value:int):void
  24. {
  25. pageMove.target = pageHolder;
  26. var index:int = 0;
  27. var btn:Button = null;
  28. value = Math.max(0, value);
  29. if (this.initialized)
  30. {
  31. if (pageHolder.numChildren > 0)
  32. {
  33. btn = pageHolder.getChildAt(value) as Button;
  34. if (_selectedBtn != null)
  35. {
  36. _selectedBtn.selected = false;
  37. }
  38. _selectedBtn = btn;
  39. _selectedBtn.selected = true;
  40. }
  41. index = Math.round(visiblePages / 2);
  42. if (value < (index - 1))
  43. {
  44. movePage(0);
  45. }
  46. else if (value > _dataProvider.length - index)
  47. {
  48. movePage((-(_dataProvider.length - visiblePages)) * 45);
  49. }
  50. else
  51. {
  52. movePage(-Math.round((value + 1) * 45) + index * 45);
  53. }
  54. prevBtn.enabled = firstBtn.enabled = value == 0 || pageHolder.numChildren == 0 ? (false) : (true);
  55. nextBtn.enabled = lastBtn.enabled = value == (_dataProvider.length - 1) ? (false) : (true);
  56. }
  57. _selectedIndex = value;
  58.  
  59. var event:PaginatorEvent = new PaginatorEvent(PaginatorEvent.PAGE_CHANGE);
  60. event.page = _selectedIndex;
  61. dispatchEvent(event);
  62.  
  63. }
  64. public function get selectedBtn():Button
  65. {
  66. return _selectedBtn;
  67. }
  68. public function set selectedBtn(value:Button):void
  69. {
  70. _selectedBtn = value;
  71. }
  72. public function get itemsPerPage():int
  73. {
  74. return _itemsPerPage;
  75. }
  76. public function set itemsPerPage(value:int):void
  77. {
  78. _itemsPerPage = value;
  79. if(_totalItems > 0 && _itemsPerPage > 0)
  80. update();
  81. }
  82. public function get pages():int
  83. {
  84. return _pages;
  85. }
  86. public function set pages(value:int):void
  87. {
  88. _pages = value;
  89. }
  90. public function get visiblePages():int
  91. {
  92. return _visiblePages;
  93. }
  94. public function set visiblePages(value:int):void
  95. {
  96. _visiblePages = value;
  97.  
  98. }
  99. public function get totalItems():int
  100. {
  101. return _totalItems;
  102. }
  103. public function set totalItems(value:int):void
  104. {
  105. _totalItems = value;
  106. if(_itemsPerPage > 0)
  107. update();
  108. }
  109. public function get dataProvider():Array
  110. {
  111. return _dataProvider;
  112. }
  113. public function set dataProvider(value:Array):void
  114. {
  115. _dataProvider = value;
  116. }
  117.  
  118. private function update():void
  119. {
  120. var totalPages:int = 0;
  121. pages = Math.ceil(totalItems / itemsPerPage);
  122. if (_dataProvider.length < _pages)
  123. {
  124. totalPages = _dataProvider.length;
  125. while (totalPages < _pages)
  126. {
  127. _dataProvider.push(totalPages);
  128. var btn:Button = new Button();
  129. btn.width = 45;
  130. btn.label = String((totalPages + 1)) as String;
  131. btn.data = totalPages;
  132. btn.addEventListener(MouseEvent.CLICK, pageClickHandler);
  133. btn.setStyle("paddingLeft", 0);
  134. btn.setStyle("paddingRight", 0);
  135. btn.setStyle("cornerRadius", 10);
  136. pageHolder.addChildAt(btn, totalPages);
  137. totalPages++;
  138. }
  139. pageCan.width = 45 * _visiblePages;
  140. }
  141. else if (_dataProvider.length > _pages)
  142. {
  143. totalPages = _dataProvider.length - 1;
  144. while (totalPages >= _pages)
  145. {
  146. _dataProvider.splice(totalPages);
  147. btn = Button(pageHolder.removeChildAt(totalPages)) as Button;
  148. btn.removeEventListener(MouseEvent.CLICK, pageClickHandler);
  149. btn.width = 0;
  150. totalPages = totalPages - 1;
  151. }
  152. }
  153. selectedIndex = Math.min(selectedIndex, (_dataProvider.length - 1));
  154.  
  155. var event:PaginatorEvent = new PaginatorEvent(PaginatorEvent.PAGE_CHANGE);
  156. event.page = _selectedIndex;
  157. dispatchEvent(event);
  158. }
  159.  
  160. private function pageClickHandler(vEvent:MouseEvent):void
  161. {
  162. var btn:Button = vEvent.currentTarget as Button;
  163. selectedIndex = btn.data as int;
  164. }
  165. private function movePage(newPos:int) : void
  166. {
  167. var currentPos:Number = -(pageHolder.width - visiblePages * 45);
  168. if (newPos < currentPos)
  169. {
  170. newPos = currentPos;
  171. }
  172. newPos = Math.min(0, newPos);
  173. pageMove.end();
  174. pageMove.xTo = newPos;
  175. pageMove.play();
  176. }
  177. private function onNPClick(vEvent:MouseEvent):void
  178. {
  179. switch(vEvent.target)
  180. {
  181. case prevBtn:
  182. {
  183. if (selectedIndex > 0)
  184. {
  185. var prevIndex:int = selectedIndex - 1;
  186. selectedIndex = prevIndex;
  187. }
  188. break;
  189. }
  190. case nextBtn:
  191. {
  192. if (selectedIndex < (_dataProvider.length - 1))
  193. {
  194. var nextIndex:int = selectedIndex + 1;
  195. selectedIndex = nextIndex;
  196. }
  197. break;
  198. }
  199. case firstBtn:
  200. {
  201. selectedIndex = 0;
  202. break;
  203. }
  204. case lastBtn:
  205. {
  206. selectedIndex = _dataProvider.length - 1;
  207. break;
  208. }
  209. default:
  210. {
  211. break;
  212. }
  213. }
  214. }
  215. ]]>
  216. </mx:Script>
  217.  
  218. <mx:Button id="firstBtn" label="&lt;&lt;" click="onNPClick(event)"/>
  219. <mx:Button id="prevBtn" label="&lt;" click="onNPClick(event)"/>
  220.  
  221. <mx:Canvas width="100%" horizontalScrollPolicy="off" id="pageCan">
  222. <mx:HBox horizontalGap="0" horizontalScrollPolicy="off" id="pageHolder"/>
  223. </mx:Canvas>
  224.  
  225. <mx:Button id="nextBtn" label="&gt;" click="onNPClick(event)"/>
  226. <mx:Button id="lastBtn" label="&gt;&gt;" click="onNPClick(event)"/>
  227.  
  228. </mx:HBox>

Report this snippet  

You need to login to post a comment.