这篇文章主要为大家详细介绍了AngularJS实现分页显示数据库信息效果的相关资料,感兴趣的小伙伴们可以参考一下
接着第一篇《》AngularJS内建服务$location及其功能详解》,进行学习
Section 2:实现分页显示效果
那么再隐身一下,通过location的setter方法设置当前的url信息。在这里为了能够让演示看到更好的效果,在这个比较完整的实例中,我引入了angularJS的多路由技术、嵌套的控制器之间传递数据、scope的继承、 http通信、内链接传递变量等。
首先建立一个首页模板
<title></title> .turnPageButtonArea { background-color: #f07a13; width: 500px; height: 30px; line-height: 30px; text-align: center; margin: 10px auto; padding: 20px; } button { margin-right: 20px; width: 100px; height: 30px; font-size: 15px; } .pageNum { width: 50px; height: 23px; text-align: center; } body { font-family: 微软雅黑; } h1 { text-align: center; } .totalPages { color: #ffffff } <h1>AngularJS TurnPage By $location Service</h1><div></div><div class="turnPageButtonArea"> <button>Previous</button><button>Next</button><button>Go</button><span class="totalPages">共 {{allPage}} 页</span></div>
通过一些简单的CSS样式将页面的布局修饰了一下。
然后在首页的元素里设置了一些ngApp和controller。
在属性为ngView的div元素中,将嵌入其他HTML的模板。
紧接着下方,我摆放了三个按钮,其中前两个是上一页和下一页的翻页按钮;一个输入框可供用户输入他想跳转到的页码,旁边的按钮作为页码的提交按钮,点击后页面立即翻页。
这三个按钮里面都有一个ngClick属性,表示当用户点击按钮后,便会执行对应的函数。
在讲解angularJS的js代码前,先截图看看文件的目录结构。
上面的index.html是之前两个例子的,可以不去理会。
为了简单期间,我把script脚本都放在了turnPage.html文件的下方了。下面就是全这个文件的完整代码:
turnPage.html
<title></title> .turnPageButtonArea { background-color: #f07a13; width: 500px; height: 30px; line-height: 30px; text-align: center; margin: 10px auto; padding: 20px; } button { margin-right: 20px; width: 100px; height: 30px; font-size: 15px; } .pageNum { width: 50px; height: 23px; text-align: center; } body { font-family: 微软雅黑; } h1 { text-align: center; } .totalPages { color: #ffffff } <h1>AngularJS TurnPage By $location Service</h1><div></div><div class="turnPageButtonArea"> <button>Previous</button><button>Next</button><button>Go</button><span class="totalPages">共 {{allPage}} 页</span></div>
view/student.html
<table cellspacing="0"> <tr> <td class="title">ID</td><td>{{student.id}}</td></tr><tr> <td class="title">Name</td><td>{{student.name}}</td></tr><tr> <td class="title">Sex</td><td>{{student.sex}}</td></tr><tr> <td class="title">Age</td><td>{{student.age}}</td></tr><tr> <td class="title">Courses</td><td> <ul> <li>{{course}}</li></ul></td></tr></table> table { border: solid 1px #000000; margin: 0px auto; } td { padding: 10px 10px 10px 20px; margin: 0px; border: solid 1px #000000; } tr { margin: 0px; padding: 0px; } .title { background-color: #207ef0; text-align: center; color: #ffffff; } ul { list-style: none; margin: 0px; padding: 0px; } li { float: left; margin: 10px; }
data/students.json
[ { "id": 1, "name": "Frank Li", "sex": "male", "age": "30", "courses": [ "HTML", "CSS", "Javascript", "Java", "PHP", "MySQL", "Ubuntu", "MongoDB", "NodeJS", "AngularJS", "Photoshop", "LESS", "Bootstrap" ] }, { "id": 2, "<b style="color:transparent">来源gao@!dai!ma.com搞$$代^@码网</b>name": "Cherry", "sex": "female", "age": "27", "courses": [ "Anderson's Fairy Tales", "Pride and Prejudice", "Vanity Fair", "Oliver Twist" ] }, { "id": 3, "name": "John Liu", "sex": "male", "age": "29", "courses": [ "iology and medical science", "pplied biology", "medicine", "cell biology" ] }, { "id": 4, "name": "Lucy Mu", "sex": "female", "age": "22", "courses": [ "Introduction to ART ", "sketch", "Composition", "sculpture" ] } ]
这是刚开始的样子,地址栏中默认的path是/1,所以直接显示了第一个学生的信息。页码总数也能获得。
点击了Previous按钮后的效果。不能再往前翻页了
处于第4页时,点击Next按钮时的效果。不能再往后翻页了。
在页码范围内翻页是没有问题的!
鉴于篇幅,我就不演示输入的页码超出范围的情况了。上面的截图是输入正确范围的页码,点击Go按钮的效果。
以上就是AngularJS实现分页显示数据库信息的详细内容,更多请关注gaodaima搞代码网其它相关文章!