在上一篇中实现了增删改查,本篇实现分页和过滤。

本系列包括:

1、前端使用AngularJS的$resource,后端ASP.NET Web API,实现增删改查
2、前端使用AngularJS的$resource,后端ASP.NET Web API,实现分页、过滤

后端添加分页、排序逻辑

首先要在后端API中添加分页的逻辑。对于分页来说,一般需要从前端获取页容量和当前页变量,还可以获取有关排序的变量。大致这样:

public IHttpActionResult Get(int pageSize, int pageNumber, string orderBy = ""){}

在StudentsController这个控制器中,增加一个Get重载方法,用于接受分页变量。

    [RoutePrefix("api/Students")]
public class StudentsController : ApiController
{
private StudentsReop _reop = new StudentsReop(); //GET api/Students
public HttpResponseMessage Get()
{
var students = _reop.Query().ToList();
return Request.CreateResponse(HttpStatusCode.OK, students);
} //GET api/Students/5
public HttpResponseMessage Get(int id)
{
var student = _reop.Get(id);
return Request.CreateResponse(HttpStatusCode.OK, student);
} //GET api/Students/pageSize/pageNumber/orderBy(optional)
[Route("{pageSize:int}/{pageNumber:int}/{orderBy:alpha?}")]
public IHttpActionResult Get(int pageSize, int pageNumber, string orderBy = "")
{
var totalCount = _reop.Query().ToList().Count();//总数量
var totalPages = Math.Ceiling((double)totalCount/pageSize);//总页数和pageSize有关
var tempResult = _reop.Query(); if (QueryHelper.PropertyExists<StudentVm>(orderBy))
{
var orderByExpression = QueryHelper.GetPropertyExpression<StudentVm>(orderBy);
tempResult = tempResult.AsQueryable().OrderBy(orderByExpression);
}
else
{
tempResult = tempResult.OrderBy(c => c.Id);
} var students = tempResult.Skip((pageNumber - ) * pageSize)
.Take(pageSize)
.ToList(); var result = new
{
TotalCount = totalCount,
TotalPages = totalPages,
Students = students
}; return Ok(result); } //POST api/Students
public void Post([FromBody]StudentVm student)
{
_reop.Post(student);
} //PUT api/Students/5
public void Put(int id, [FromBody]StudentVm student)
{
_reop.Put(id, student);
} //DELETE api/Students
public void Delete(int id)
{
_reop.Delete(id);
}
}

以上,Get(int pageSize, int pageNumber, string orderBy = "")方法中,首先获取排序后的临时数据,再使用skip,take方法获取分页数据,最后返回给前端一个json数据,其中,TotalCount表示总数据量,Students表示当前页下的数据,这2个字段供前端读取。另外,QueryHelper封装了可以根据模型字段获取表达式树的方法。

    internal static class QueryHelper
{
public static bool PropertyExists<T>(string propertyName)
{
return typeof(T).GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) != null;
} public static Expression<Func<T, string>> GetPropertyExpression<T>(string propertyName)
{
if (typeof(T).GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) == null)
{
return null;
} var paramterExpression = Expression.Parameter(typeof(T)); return (Expression<Func<T, string>>)Expression.Lambda(Expression.PropertyOrField(paramterExpression, propertyName), paramterExpression);
} public static Expression<Func<T, int>> GetPropertyExpressionInt<T>(string propertyName)
{
if (typeof(T).GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) == null)
{
return null;
} var paramterExpression = Expression.Parameter(typeof(T)); return (Expression<Func<T, int>>)Expression.Lambda(Expression.PropertyOrField(paramterExpression, propertyName), paramterExpression);
}
}

此时,通过浏览器可以获取到分页数据。比如:http://localhost:49621/api/Students/5/2/Name

前端准备

前端需要用到Michael Bromley写的一个分页Directive。 通过如下可安装:

bower install angular-utils-pagination

npm install angular-utils-pagination

还需要安装bootstrap和jquery:

npm install bootstrap
npm install jquery

文件结构变为:

app.js 主module,路由都在这里配置
index.html 主视图,引用所有的css,js文件,提供让其它部分视图呈现的一块区域<div ng-view></div>
.....service/ 自定义服务,$resouce的核心就封装在这里
..........studentService.js
.....controller/
..........studentsCtrl.js 列表
..........studentUpdateCtrl.js 更新
..........studentCreateCtrl.js 添加
.....views/
..........Students.html 列表
..........StudentInfo.html 更新
..........StudentCreate.html 添加
vendor/
......diaPagination/  Michael Bromley写的directive
..........dirPagination.css
..........dirPagination.js
..........dirPagination.tpl.html

index.html

相对于上一篇,添加了如下:

<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css"/>
<link rel="stylesheet" href="vendor/dirPagination/dirPagination.css"/>

<script src="node_modules/jquery/dist/jquery.min.js"></script>
<script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="vendor/dirPagination/dirPagination.js"></script>

并使用上了bootstrap。

<!DOCTYPE html>
<html lang="en" ng-app="studentManagement">
<head>
<meta charset="UTF-8">
<title>{{title}}</title>
<link rel="stylesheet" href="node_modules/alertify/themes/alertify.core.css"/>
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css"/>
<link rel="stylesheet" href="vendor/dirPagination/dirPagination.css"/>
</head>
<body>
<div>
<p>
<a href="#/">Students</a>
&nbsp;&nbsp;
<a href="#/Create">Create Student</a>
</p>
</div> <div class="container">
<div class="row">
<div class="col-lg-8">
<div ng-view></div>
</div>
</div>
</div> <script src="node_modules/angular/angular.min.js"></script>
<script src="node_modules/angular-route/angular-route.min.js"></script>
<script src="node_modules/angular-resource/angular-resource.min.js"></script>
<script src="node_modules/angular-cookies/angular-cookies.min.js"></script>
<script src="node_modules/alertify/lib/alertify.min.js"></script>
<script src="node_modules/jquery/dist/jquery.min.js"></script>
<script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script> <script src="app.js"></script> <script src="service/studentService.js"></script>
<script src="controller/studentUpdateCtrl.js"></script>
<script src="controller/studentsCtrl.js"></script>
<script src="controller/studentCreateCtrl.js"></script>
<script src="vendor/dirPagination/dirPagination.js"></script> </body>
</html>

app.js

对比上一篇,这里添加了对angularUtils.directives.dirPagination这个module的依赖,去掉了/这个路由中有关resolve获取数据的机制,因为现在要分页了,每次都要获取数据,而不是把所有数据先从服务端获取到放到路由中。

"use strict";

var studentsManagement = angular.module("studentManagement",["ngResource","ngCookies","ngRoute","angularUtils.directives.dirPagination"])
.run(function($rootScope){
$rootScope.title = "Home";
})
.config(["$routeProvider","$locationProvider", function($routeProvider, $locationProvider){ //在全局配置分页模板 需要注入paginationTemplateProvider
//paginationTemplateProvider.setPath('path/to/dirPagination.tpl.html'); //关于url的基本配置
//$locationProvider.html5Mode({
// enabled: true,
// requireBase: false
//}); //配置路由
$routeProvider.when("/", {
templateUrl: "views/Students.html",
controller: "studentsCtrl"
//resolve: {
// students: function($q,studentDataService){
//
// var queryArgs = {
// pageSize: 3,
// pageNumber: 1,
// orderBy: "id"
// };
//
// //$q异步执行方法
// var deferred = $q.defer();
// studentDataService.query(function(data){
// deferred.resolve(data);
// });
//
// return deferred.promise;
// }
//}
}).when("/Student/:id",{
templateUrl: "views/StudentInfo.html",
controller: "studentUpdateCtrl",
resolve: {
student: function($q, studentDataService, $route){
var defered = $q.defer(); //从路由中获取id的值
var id = $route.current.params.id; studentDataService.get({id: id}, function(data){
defered.resolve(data);
}); return defered.promise;
}
}
}).when("/Create",{
templateUrl: "views/CreateStudent.html",
controller: "studentCreateCtrl"
}); }]);

以上,我们还可以通过paginationTemplateProvider.setPath('path/to/dirPagination.tpl.html');在全局中配置分页模板,但这里没有。

studentService.js

用$resouce封装请求数据这块有变化,因为要带上分页、排序参数。

angular.module('studentManagement').factory("studentDataService",["$resource", function($resource){

    var baseUrl = "http://localhost:49621/api/Students";
return $resource("http://localhost:49621/api/Students",{},{
query: {
method: "GET",
url: baseUrl + "/:pageSize/:pageNumber/:orderBy",
params: {pageSize: '@pageSize', pageNumber: '@pageNumber', orderBy: '@orderBy'}
},
//query: {
// method: "GET",
// isArray: true,
//},
create: {method: "POST"},
get: {method: "GET", url: baseUrl + "?id=:id"},
remove: {method: "DELETE", url: baseUrl + "?id=:id"},
update: {method: "PUT", url: baseUrl + "?id=:id"}
})
}]);

studentsControl.js

angular.module('studentManagement').controller("studentsCtrl", ['$scope', '$route', '$rootScope', 'studentDataService', function ($scope, $route, $rootScope, studentDataService) {

    $scope.students = [];
$scope.total = 0; //总数据条数
$scope.currentPage = 1;
$scope.pageSize = 3; //页容量 //初次加载
getPageStudents(1); $scope.pageChangeHandler = function (num) {
getPageStudents(num);
}; //获取分页数据
function getPageStudents(pageNumber){
var queryArgs = {
pageSize: $scope.pageSize,
pageNumber: pageNumber,
orderBy: 'name'
}; studentDataService.query(queryArgs).$promise.then(function(result){
$scope.students = result.students;
$scope.total = result.totalCount;
}, function(result){ //如果失败
console.log('fail'); });
} $rootScope.title = "Students"; //$scope.students = $route.current.locals.students;//students在路由resolve中定义 //删除
$scope.removeStudent = function (id, student) {
studentDataService.remove({id: id}).$promise.then(function () {
//获取student在当前集合中的索引
var index = $scope.students.indexOf(student);
$scope.students.splice(index, 1);
alertify.log(student.Name + ' is removed');
});
}; }]);
//.controller("OtherController", ["$scope", function ($scope) {
// $scope.pageChangeHandler = function (num) {
// console.log('going to page ' + num);
// };
//}]);

以上,初次加载以及点击分页按钮的时候都触发getPageStudents方法,传入页容量和当前页变量。

Students.html

在dir-paginate中多了一个total-items属性,把从controller中获取到的total变量值赋值给它。

<!--显示内容的控制器-->
<div class="my-controller"> <div class="row">
<div class="col-xs-4">
<h3>当前页: {{ currentPage }}</h3>
</div>
<div class="col-xs-4">
<label for="search">搜索:</label>
<input ng-model="q" id="search" class="form-control" placeholder="Filter text">
</div>
<div class="col-xs-4">
<label for="search">每页条数:</label>
<input type="number" min="1" max="100" class="form-control" ng-model="pageSize">
</div>
</div>
<br>
<div class="panel panel-default">
<div class="panel-body"> <table>
<thead>
<tr>
<th>Name</th><th>Age</th><th>Actions</th>
</tr>
</thead>
<tbody>
<!--itemsPerPage是必须的,要放在所有过滤的最后面,表示页容量-->
<!--dir-paginate的pagination-id属性,可选,和dir-pagination-controls中的pagination-id对应-->
<!--dir-paginate的current-page属性,可选,默认会读取$scope中的_currentPage字段-->
<!--dir-paginate的total-items属性,可选,用来读取服务端数据,先把总页数显示出来-->
<tr dir-paginate="student in students | filter:q | itemsPerPage: pageSize" total-items="total" current-page="currentPage">
<td>{{student.name}}</td>
<td>{{student.age}}</td>
<td>
<a href="#/Student/{{student.Id}}">更新</a>
&nbsp;&nbsp;
<a href="javascript:void(0)" ng-click="$parent.removeStudent(student.Id, student)">移除</a>
</td>
</tr>
</tbody>
</table> </div>
</div>
</div> <!--显示分页的控制器-->
<!--<div ng-controller="OtherController" class="other-controller">-->
<div class="other-controller">
<div class="text-center">
<!--其它属性包括:max-size, direction-links,boundary-links,pagination-id,auto-hide-->
<!--max-size:可选,默认为最大是9,最小是5-->
<!--direction-links:可选,默认true,决定是否显示向前向后按钮-->
<!--boundary-links,可选,默认false,决定是否显示首页和最后一页按钮-->
<!--on-page-change,可选,默认为null,声明当点击页数按钮后的回调函数,一旦声明,必须传入newPageNumber形参,必须在$scope中声明回调函数,比如这里的pageCHnageHandler函数-->
<!--pagination-id,可选,与dir-paginate中的pagination-id值对应-->
<!--template-url,可选,默认值是directives/pagination/dirPagination.tpl.html,也可以在全局config中,通过paginationTemplateProvider配置-->
<!--auto-hide,可选,默认是true,决定当没有足够的分页数据时,分页是否显示-->
<!--这里的dir-pagination-controls不能离开dir-paginate而单独存在,否则报错-->
<dir-pagination-controls boundary-links="true" on-page-change="pageChangeHandler(newPageNumber)" template-url="vendor/dirPagination/dirPagination.tpl.html"></dir-pagination-controls>
</div>
</div>

本系列结束☺

参考资料:

有关Web API配合AngularJS分页:

● https://code.msdn.microsoft.com/AngularJS-with-Web-API-43e5de16

有关AngularJS分页的Directive:

● http://www.michaelbromley.co.uk/blog/108/paginate-almost-anything-in-angularjs
● https://github.com/michaelbromley/angularUtils/tree/master/src/directives/pagination#working-with-asynchronous-data

前端使用AngularJS的$resource,后端ASP.NET Web API,实现分页、过滤的更多相关文章

  1. 前端使用AngularJS的$resource,后端ASP.NET Web API,实现增删改查

    AngularJS中的$resource服务相比$http服务更适合与RESTful服务进行交互.本篇后端使用ASP.NET Web API, 前端使用$resource,实现增删改查. 本系列包括: ...

  2. AngularJS使用OData请求ASP.NET Web API资源的思路

    本篇整理AngularJS使用OData请求ASP.NET Web API资源的思路. 首先给ASP.NET Web API插上OData的翅膀,通过NuGet安装OData. 然后,给control ...

  3. 前端AngularJS后端ASP.NET Web API上传文件

    本篇体验使用AngularJS向后端ASP.NET API控制器上传文件.    首先服务端: public class FilesController : ApiController { //usi ...

  4. ASP.NET Web API教程 分页查询

    首先增加支持分页的API方法 public IEnumerable<UserInfo> GetUserInfos(int pageindex, int size)         {    ...

  5. ASP.NET Web API 2 external logins with Facebook and Google in AngularJS app

    转载:http://bitoftech.net/2014/08/11/asp-net-web-api-2-external-logins-social-logins-facebook-google-a ...

  6. ASP.NET Web API中把分页信息放Header中返回给前端

    谈到ASP.NET Web API的分页,考虑的因素包括: 1.上一页和下一页的uri2.总数和总页数3.当前页和页容量 接着是服务端的数据以怎样的形式返回? 我们通常这样写: {    totalC ...

  7. ASP.NET Web API接受AngualrJS的QueryString的两种方式

    ASP.NET Web API如何接受来自AngualrJS的QueryString呢?本篇体验两种方式. 第一种方式:http://localhost:49705/api/products?sear ...

  8. 购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(2)--前端,以及前后端Session

    原文:购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(2)--前端,以及前后端Session chsakell分享了前端使用AngularJS,后端使用ASP.NE ...

  9. 前端angularjs+requirejs+dhtmlx 后端asp.net webapi

    享一个前后端分离方案源码-前端angularjs+requirejs+dhtmlx 后端asp.net webapi   一.前言 半年前左右折腾了一个前后端分离的架子,这几天才想起来翻出来分享给大家 ...

随机推荐

  1. 电容充放电时间常数RC计算方法

    进入正题前,我们先来回顾下电容的充放电时间计算公式,假设有电源Vu通过电阻R给电容C充电,V0为电容上的初始电压值,Vu为电容充满电后的电压值,Vt为任意时刻t时电容上的电压值,那么便可以得到如下的计 ...

  2. gentoo系统安装

    1. Gentoo常用镜像   ===>http://www.linuxsir.org/bbs/thread263600.html 2. 安装方式1 http://www.linuxidc.co ...

  3. Java 连接远程Linux 服务器执行 shell 脚本查看 CPU、内存、硬盘信息

    pom.xml jar 包支持 <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch& ...

  4. java 内部类使用 .this 和 .new

    如果需要生成对外部类对象的引用,可以使用外部类的名字后面紧跟圆点和this,这样产生的引用自动地具有正确的类型,这一点在编译器就被知晓并受到检查,因此并没有运行时开销 //: innerclasses ...

  5. Java编程的逻辑 (55) - 容器类总结

    本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...

  6. Ubuntu 下 vi 输入方向键会变成 ABCD 的解决方法

    Ubuntu 下 vi 输入方向键会变成 ABCD,这是 Ubuntu 预装的是 vim tiny 版本,安装 vim full 版本即可解决. 先卸载vim-tiny: $ sudo apt-get ...

  7. zoj 3809 枚举水题 (2014牡丹江网赛 A题)

    题目大意:给出一列取样的几个山的高度点,求山峰有几个? Sample Input 291 3 2 4 6 3 2 3 151 2 3 4 5Sample Output 30 # include < ...

  8. docker:定制node.js的版本

    本想用alpine,但如果想使用node.js 6.3.1版本的软件, 总会搞不定glibc和libstdc++报一个无法识别版本信息的错误. 搞了一天,算了.使用debian:stretch-sli ...

  9. 【PAT】1043 Is It a Binary Search Tree(25 分)

    1043 Is It a Binary Search Tree(25 分) A Binary Search Tree (BST) is recursively defined as a binary ...

  10. 【LOJ】#2278. 「HAOI2017」字符串

    题解 好神仙的题啊 感觉转二维平面能想到,算重复情况的方法真想不到啊 通过扒stdcall代码获得的题解QAQQQQ 我们先把\(p_i\)正串反串建出一个AC自动机来 然后我们把s串放在上面跑匹配, ...