用ReactJS写的简单组件Pagebar(待完善。。。)

前端之家收集整理的这篇文章主要介绍了用ReactJS写的简单组件Pagebar(待完善。。。)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

代码结构

代码如下

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <Meta charset="UTF-8">
  5. <title>react pagebar</title>
  6. <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
  7. <script type="text/javascript" src="js/react.js"></script>
  8. <script type="text/javascript" src="js/JSXTransformer.js"></script>
  9. </head>
  10. <body>
  11. <div id="pagebar"></div>
  12. <script type="text/jsx">
  13. var PageBar = React.createClass({
  14. handleClick:function(index) {
  15. this.setState({currPage: index});
  16. },handlePrev:function() {
  17. var currPage = this.state.currPage-1;
  18. if (currPage < 1) {
  19. return;
  20. }
  21. this.setState({currPage:currPage});
  22. },handleNext:function(){
  23. var currPage = this.state.currPage+1;
  24. if (currPage > this.props.totalCount) {
  25. return;
  26. }
  27. this.setState({currPage:currPage});
  28. },getInitialState: function(){
  29. return {currPage:1};
  30. },render : function() {
  31. var lis = [];
  32. var liClassName;
  33. for (var i = 0; i < this.props.totalCount; i++) {
  34. liClassName = "page-nav";
  35. if ((i+1) === this.state.currPage) {
  36. liClassName = "page-nav active"
  37. }
  38. lis.push(<li className={liClassName}><a href="javascript:void(0);" onClick={this.handleClick.bind(this,i+1)}>{i+1}</a></li>);
  39. }
  40. return (
  41. <nav>
  42. <ul className="pagination" style={{margin:0}}>
  43. <li className="page-prev">
  44. <a href="javascript:void(0);" aria-label="PrevIoUs" onClick={this.handlePrev}>
  45. <span aria-hidden="true">«</span>
  46. </a>
  47. </li>
  48. {lis}
  49. <li className="page-next">
  50. <a href="javascript:void(0);" aria-label="Next" onClick={this.handleNext}>
  51. <span aria-hidden="true">»</span>
  52. </a>
  53. </li>
  54. </ul>
  55. </nav>
  56. );
  57. }
  58. });
  59.  
  60. React.render(<PageBar totalCount="10" />,document.getElementById("pagebar"));
  61. </script>
  62. </body>
  63. </html>

效果

猜你在找的React相关文章