Table of Contents
@task.Title
Home Backend Development C#.Net Tutorial Introduction to the implementation method of paging function in ABP introductory series

Introduction to the implementation method of paging function in ABP introductory series

Mar 19, 2017 am 10:34 AM

This section mainly explains how to use ABP for background paging, and also explains the implementation of ABP background paging logic. At the same time, it also demonstrates how to use X.PagedList for front-end paging

After completing the addition, deletion, modification and check of the task list, let's talk about the essential paging function.

First of all, I am very glad that ABP has helped us encapsulate the paging implementation, which is really considerate.

Come on, let’s take a look at how to use ABP for paging in this section.

1. Paging request DTO definition

Data TransferObject(Data Transfer Objects ) is used for data transmission at the application layer and presentation layer.

The presentation layer passes in the data transfer object (DTO) to call an application service method, and then the application service executes some specific business logic through the domain object and returns the DTO to the presentation layer. In this way, the presentation layer and domain layer are completely separated. In a well-layered application, the presentation layer does not directly use domain objects (warehouses, entities).

When we have a paging request in the presentation layer, we generally need to specify two parameters, one is used to mark the page, and the other is the number of lines per page (usually configured with configuration file).

Because paging is a very commonly used function, when we have a paging request in the presentation layer, it is best to have a corresponding DTO to encapsulate these two parameters.

There is no such public class encapsulated for us in the ABP source code, but in the ABPZero project, the corresponding paging Dto is defined.

The corresponding class diagram is as follows:

Introduction to the implementation method of paging function in ABP introductory series

Paging sorting and filtering the corresponding Dto class diagram

It can be seen that it mainly includes the following four common DTO definition:

  1. PagedInputDto: paging request Dto

  2. PagedAndSortedInputDto: paging sorting Dto

  3. PagedSortedAndFilteredInputDto : Paging sorting and filtering Dto

  4. PagedAndFilteredInputDto: Paging filtering Dto

is not very practical. Our paging is usually mixed with filtering and sorting. .

The following main attributes are mainly defined:

  • MaxResultCount: The number of rows per page, which can be read from the defined configuration .

  • SkipCount: The number of jumps. The general calculation formula is SkipCount=Page*MaxResultCount (number of pages*number of rows).

  • Filter: Filter string

  • Sorting: Sorting method

The specific implementation will not be described in detail. I believe that if you look at the class diagram carefully, you can implement it yourself. AbpZero defines these public Dtos in the Dto folder of the application service layer. The specific paths are as follows.

Introduction to the implementation method of paging function in ABP introductory series

2. How to use paging DTO

Take our task list as an example, let’s modify it and create GetTaskInputDto, let inherit from PagedSortedAndFilteredInputDto, so that GetTaskInputDto has the properties needed for paging sorting and filtering.


public class GetTasksInput : PagedSortedAndFilteredInputDto
{
 public TaskState? State { get; set; }
 public int? AssignedPersonId { get; set; }
}
Copy after login


3. Return the paging result DTO

Abp The generic PagedResultDto has been defined for us to wrap the returned paging results. It mainly includes two attributes, int TotalCount saves the total number, and IReadOnlyList Items saves the returned paging result set.

4. Application service layer paging logic implementation

1. Define interface

PagedResultDto<TaskDto> GetPagedTasks(GetTasksInput input);
Copy after login
# in ITaskAppService


##2. Implement the interface in TaskAppService:


public PagedResultDto<TaskDto> GetPagedTasks(GetTasksInput input)
{
 //初步过滤
 var query = _taskRepository.GetAll().Include(t => t.AssignedPerson)
  .WhereIf(input.State.HasValue, t => t.State == input.State.Value)
  .WhereIf(!input.Filter.IsNullOrEmpty(), t => t.Title.Contains(input.Filter))
  .WhereIf(input.AssignedPersonId.HasValue, t => t.AssignedPersonId == input.AssignedPersonId.Value);
 //排序
 query = !string.IsNullOrEmpty(input.Sorting) ? query.OrderBy(input.Sorting) : query.OrderByDescending(t => t.CreationTime);
 //获取总数
 var tasksCount = query.Count();
 //默认的分页方式
 //var taskList = query.Skip(input.SkipCount).Take(input.MaxResultCount).ToList();
 //ABP提供了扩展方法PageBy分页方式
 var taskList = query.PageBy(input).ToList();
 return new PagedResultDto<TaskDto>(tasksCount,taskList.MapTo<List<TaskDto>>());
}
Copy after login


The implementation of paging is very simple. First filter, sort, then perform paging, and finally use PagedResultDto to encapsulate the paging results.

If you are careful, you may find two methods

WhereIf and PageBy that have never been used in Linq. Yes, these are extension methods provided by ABP. Thank you. If you are interested, you can take a look at the specific implementation of the source code QueryableExtensions. In fact, the implementation is very simple, but we may not be able to imagine it when we usually use linq.

I would like to ask a few questions here:

How many queries have been performed in this code?

代码中使用的是什么分页技术?(真分页?假分页?)

5. 使用X.PagedList进行前端分页

Asp.Net Mvc中进行前端分页有一系列开源实现,在我的Demo中使用的是开源的X.PagedList分页。想了解具体源码,请参考X.PagedList GitHub。

1、请自行在Web项目中安装X.PagedList.Mvc Nuget程序包

Introduction to the implementation method of paging function in ABP introductory series

X.PagedList.Mvc Nuget程序包

2、在Controller中使用X.PagedList提供的方法构造分页结果供前端使用

因为我们在应用服务层已经手动实现了分页逻辑,所以按照X.PagedList官网示例,我们需要自行构造StaticPagedList作为返回结果。


 public ActionResult PagedList(int? page)
 {
  //每页行数
  var pageSize = 5;
  var pageNumber = page ?? 1;//第几页
  var filter = new GetTasksInput
  {
   SkipCount = (pageNumber - 1) * pageSize,//忽略个数
   MaxResultCount = pageSize
  };
  var result = _taskAppService.GetPagedTasks(filter);
  //已经在应用服务层手动完成了分页逻辑,所以需手动构造分页结果
  var onePageOfTasks = new StaticPagedList<TaskDto>(result.Items, pageNumber, pageSize, result.TotalCount);
  //将分页结果放入ViewBag供View使用
  ViewBag.OnePageOfTasks = onePageOfTasks;
  return View();
}
Copy after login

从代码中可以看到我们构造完X.PagedList提供的分页结果后放入了ViewBag中,供视图使用。

3、View中添加分页控件

PagedList视图的代码如下:

@using X.PagedList.Mvc;
@using Abp.Web.Mvc.Extensions
@using X.PagedList; 
<link href="~/Content/PagedList.css" rel="external nofollow" rel="stylesheet" />
<ul class="list-group">
 @foreach (var task in ViewBag.OnePageOfTasks)
 {
  <li class="list-group-item">
   <p class="btn-group pull-right">
    <button type="button" class="btn btn-info">Edit</button>
    <button type="button" class="btn btn-success">Delete</button>
   </p>
   <p class="media">
    <a class="media-left" href="#" rel="external nofollow" >
     @*<i class="fa @Model.GetTaskLable(task) fa-3x"></i>*@
    </a>
    <p class="media-body">
     <h4 id="task-Title">@task.Title</h4>
     <span class="text-muted">@task.CreationTime.ToString("yyyy-MM-dd HH:mm:ss")</span>
    </p>
   </p>
  </li>
 }
</ul>
@Html.PagedListPager((IPagedList)ViewBag.OnePageOfTasks, page => Url.Action("PagedList", new { page }))
Copy after login

其中最后一句代码是用来生成分页控件。

最终效果如图:

Introduction to the implementation method of paging function in ABP introductory series总结

总结

本节主要讲解了如何使用ABP进行后台分页,并顺带讲解了ABP后台分页逻辑的实现方式。同时也演示了如何使用X.PagedList进行前端分页。

The above is the detailed content of Introduction to the implementation method of paging function in ABP introductory series. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Clair Obscur: Expedition 33 - How To Get Perfect Chroma Catalysts
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
The Continued Relevance of C# .NET: A Look at Current Usage The Continued Relevance of C# .NET: A Look at Current Usage Apr 16, 2025 am 12:07 AM

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

C# as a Versatile .NET Language: Applications and Examples C# as a Versatile .NET Language: Applications and Examples Apr 26, 2025 am 12:26 AM

C# is widely used in enterprise-level applications, game development, mobile applications and web development. 1) In enterprise-level applications, C# is often used for ASP.NETCore to develop WebAPI. 2) In game development, C# is combined with the Unity engine to realize role control and other functions. 3) C# supports polymorphism and asynchronous programming to improve code flexibility and application performance.

Deploying C# .NET Applications to Azure/AWS: A Step-by-Step Guide Deploying C# .NET Applications to Azure/AWS: A Step-by-Step Guide Apr 23, 2025 am 12:06 AM

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

C# and the .NET Runtime: How They Work Together C# and the .NET Runtime: How They Work Together Apr 19, 2025 am 12:04 AM

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

C# .NET: Building Applications with the .NET Ecosystem C# .NET: Building Applications with the .NET Ecosystem Apr 27, 2025 am 12:12 AM

How to build applications using .NET? Building applications using .NET can be achieved through the following steps: 1) Understand the basics of .NET, including C# language and cross-platform development support; 2) Learn core concepts such as components and working principles of the .NET ecosystem; 3) Master basic and advanced usage, from simple console applications to complex WebAPIs and database operations; 4) Be familiar with common errors and debugging techniques, such as configuration and database connection issues; 5) Application performance optimization and best practices, such as asynchronous programming and caching.

.NET Framework vs. C#: Decoding the Terminology .NET Framework vs. C#: Decoding the Terminology Apr 21, 2025 am 12:05 AM

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

C# .NET Development: A Beginner's Guide to Getting Started C# .NET Development: A Beginner's Guide to Getting Started Apr 18, 2025 am 12:17 AM

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

C# and .NET: Understanding the Relationship Between the Two C# and .NET: Understanding the Relationship Between the Two Apr 17, 2025 am 12:07 AM

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

See all articles