前端
把Pagination的css封装到了另一个文件里
import Pagination from '@/components/Pagination'
封装的Pagination
<template>
<div :class="{'hidden':hidden}" class="pagination-container">
<el-pagination
:background="background"
:current-page.sync="currentPage"
:page-size.sync="pageSize"
:layout="layout"
:page-sizes="pageSizes"
:total="total"
v-bind="$attrs"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
</div>
</template>
<script>
import { scrollTo } from '@/utils/scroll-to'
export default {
name: 'Pagination',
props: {
total: {
required: true,
type: Number
},
page: {
type: Number,
default: 1
},
limit: {
type: Number,
default: 20
},
pageSizes: {
type: Array,
default() {
return [10, 20, 30, 50]
}
},
layout: {
type: String,
default: 'total, sizes, prev, pager, next, jumper'
},
background: {
type: Boolean,
default: true
},
autoScroll: {
type: Boolean,
default: true
},
hidden: {
type: Boolean,
default: false
}
},
computed: {
currentPage: {
get() {
return this.page
},
set(val) {
this.$emit('update:page', val)
}
},
pageSize: {
get() {
return this.limit
},
set(val) {
this.$emit('update:limit', val)
}
}
},
methods: {
handleSizeChange(val) {
this.$emit('pagination', { page: this.currentPage, limit: val })
if (this.autoScroll) {
scrollTo(0, 800)
}
},
handleCurrentChange(val) {
this.$emit('pagination', { page: val, limit: this.pageSize })
if (this.autoScroll) {
scrollTo(0, 800)
}
}
}
}
</script>
<style scoped>
.pagination-container {
background: #fff;
padding: 32px 16px;
}
.pagination-container.hidden {
display: none;
}
</style>
在table外进行分页功能
<pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getProjectList" />
v-show:通过改变元素的 css 属性(display)来决定元素是显示还是隐藏,设定当数据总数>0时即显示
通过limit和page来进行分页,在信息获取时要带着这两个数据,如果页面浏览时设计信息下拉选项查询,应注意分页后只会显示当前几条数据
data(){
return{
listQuery: {
projectArea: '',
customerName: '',
projectName: '',
page: 1,//页码默认值为1
limit: 10,//一页限制的数据条数
},
}
}
数据总数先提前在数据库获取,将total值确定,以便确认页数等
// 获取总数
getTotalCount() {
var self = this
request({
url: self.url + '/api/Apply/GetTotalCount',
method: 'get',
headers: {
'Authorization': 'Bearer ' + getToken()
},
params: self.listQuery
}).then(response => {
self.total = response
})
},
后端
获取总数Controller,接口
// 获取项目信息总数
[HttpGet]
[Route("GetTotalCount")]
public ActionResult GetTotalCount(string area, string custom, string projectName, string roles, string userId)
{
int result = _applyService.GetTotalCount(area, custom, projectName, roles, userId);
JsonResult jr = new JsonResult(result);
return jr;
}
获取总数Service,进行非空判定
/// <summary>
/// 获取项目列表总数
/// </summary>
/// <returns></returns>
public int GetTotalCount(string area, string custom, string projectName, string roles, string userId)
{
if (!string.IsNullOrEmpty(roles) && true == roles.Contains("manager"))
{
return _projectRepository.QueryTotalCount(null, area, custom, projectName, userId, null);
}
else if (!string.IsNullOrEmpty(roles) && true == roles.Contains("user"))
{
return _projectRepository.QueryTotalCount(null, area, custom, projectName, null, userId);
}
else if (!string.IsNullOrEmpty(roles) && true == roles.Contains("project"))
{
return _projectRepository.QueryTotalCountProjecter(null, area, custom, projectName, null, userId);
}
else
{
return _projectRepository.QueryTotalCount(null, area, custom, projectName, null, null);
}
}
查询总数时不需要传入limit和page,但是在获取列表时需要传入
// 获取项目信息
[HttpGet]
[Route("GetProjectInfo")]
public ActionResult GetProjectInfo(string area, string custom, string projectName, string roles, string userId,int page,int limit)
{
//string roles = HttpContext.Session.GetString("ROLES");
//string userId = HttpContext.Session.GetString("USERID");
List<ProjectInfo> result = _applyService.GetProjectInfo(area, custom, projectName, roles, userId, page, limit);
JsonResult jr = new JsonResult(result);
return jr;
}
XML文件,读取数据库总数
<Statement Id="QueryTotalCount">
SELECT count(*)
from dbo.Vproject vp
<Where>
<IsNotEmpty Prepend="And" Property="id">
vp.Id = @id
</IsNotEmpty>
<IsNotEmpty Prepend="And" Property="area">
vp.ProjectArea = @area
</IsNotEmpty>
<IsNotEmpty Prepend="And" Property="custom">
vp.CustomerName = @custom
</IsNotEmpty>
<IsNotEmpty Prepend="And" Property="projectName">
vp.ProjectName = @projectName
</IsNotEmpty>
<IsNotEmpty Prepend="And" Property="manager">
vp.Manager = @manager
</IsNotEmpty>
<IsNotEmpty Prepend="And" Property="viewer">
vp.Viewer = @viewer
</IsNotEmpty>
</Where>
</Statement>
最终实现的页面效果