跳到正文

远程分页接入 ​

FaTable 把页码、每页条数和搜索词传给 Fast.Axios。接口返回本页的 rows 和筛选后的总条数 totalRows,表格据此显示分页。

运行最小示例 ​

准备 .NET 10 SDK 和符合组件库环境要求的 Node.js。在任意空目录打开两个终端,分别创建后端和前端。想先看操作效果,可以打开站内 FaTable 分页演示。

启动后端并确认响应 ​

在第一个终端创建 ASP.NET Core 空项目。

bash
dotnet new web -n PaginationApi -f net10.0
cd PaginationApi

将 Program.cs 替换为下面的代码。示例从内存筛选数据,用与 Fast.NET 相同的响应结构返回分页结果。

csharp
var app = WebApplication.CreateBuilder(args).Build();
UserRow[] users = [new(1, "Alice"), new(2, "Bob"), new(3, "Alicia")];

app.MapPost("/api/users/search", (UserSearch input) =>
{
    var pageIndex = Math.Max(1, input.PageIndex);
    var pageSize = Math.Clamp(input.PageSize, 1, 100);
    var keyword = input.SearchValue?.Trim() ?? string.Empty;
    var matches = users.Where(user => user.Name.Contains(keyword, StringComparison.OrdinalIgnoreCase)).ToArray();
    var offset = (int)Math.Min((long)(pageIndex - 1) * pageSize, matches.Length);
    var rows = matches.Skip(offset).Take(pageSize).ToArray();
    return Results.Ok(new { code = 200, success = true, message = "", data = new { rows, totalRows = matches.Length, pageIndex, pageSize } });
});

app.Run();

record UserSearch(int PageIndex = 1, int PageSize = 20, string? SearchValue = null);
record UserRow(int Id, string Name);

在 PaginationApi 目录启动后端,并保持终端运行。

bash
dotnet run --no-launch-profile -- --urls http://127.0.0.1:5080

向 http://127.0.0.1:5080/api/users/search 发送 POST,设置 Content-Type: application/json,请求体如下。

json
{ "pageIndex": 2, "pageSize": 1, "searchValue": "Ali" }

预期得到 HTTP 200。第二页只有 Alicia,totalRows 是两页共有的匹配记录总数。

json
{
	"code": 200,
	"success": true,
	"message": "",
	"data": {
		"rows": [
			{
				"id": 3,
				"name": "Alicia"
			}
		],
		"totalRows": 2,
		"pageIndex": 2,
		"pageSize": 1
	}
}

初始化请求容器 ​

回到最初的空目录,在第二个终端创建 Vue + TypeScript 项目并安装公开依赖。

bash
npm create vite@latest pagination-web -- --template vue-ts --no-interactive
cd pagination-web
npm install fast-element-plus element-plus @element-plus/icons-vue @fast-element-plus/icons-vue @fast-china/axios axios

将 src/main.ts 替换为下方代码,在应用入口初始化 FastAxios。

ts
import { createApp } from "vue";
import FastElementPlus from "fast-element-plus";
import { createFastAxios } from "@fast-china/axios";
import App from "./App.vue";
import "element-plus/dist/index.css";
import "fast-element-plus/index.css";

createFastAxios({ baseUrl: "/api", requestCipher: false });

createApp(App).use(FastElementPlus).mount("#app");

将 vite.config.ts 替换为下方配置。Vite 会把同源的 /api 请求转发到后端,并保留原路径。

ts
import vue from "@vitejs/plugin-vue";
import { defineConfig } from "vite";

export default defineConfig({
	plugins: [vue()],
	server: {
		host: "127.0.0.1",
		port: 5174,
		strictPort: true,
		proxy: {
			"/api": "http://127.0.0.1:5080",
		},
	},
});

在页面绑定 FaTable ​

将 src/App.vue 替换为下方代码。Fast.Axios 校验响应后返回 data,FaTable 直接从分页结果里读取 rows 和 totalRows。

vue
<template>
	<main>
		<h1>ASP.NET Core · Fast.Axios · FaTable</h1>
		<FaTable :request-api="requestUsers" :page-sizes="[1, 10, 20]" hide-search-time>
			<FaTableColumn prop="name" label="Name" />
		</FaTable>
	</main>
</template>

<script setup lang="ts">
import { FaTable, FaTableColumn } from "fast-element-plus";
import { axiosUtil } from "@fast-china/axios";
import type { DefaultRow, PagedInput, PagedResult } from "fast-element-plus";

interface UserRow extends DefaultRow {
	id: number;
	name: string;
}

function requestUsers(input?: PagedInput) {
	return axiosUtil.request<PagedResult<UserRow>>({
		url: "/users/search",
		method: "post",
		requestType: "query",
		data: { pageIndex: input?.pageIndex, pageSize: input?.pageSize, searchValue: input?.searchValue },
	});
}
</script>

在 pagination-web 目录启动前端。

bash
npm run dev

打开 http://127.0.0.1:5174/。把每页条数设为 1,在关键字框输入 Ali 并按 Enter。第一页显示 Alice,总条数为 2,点击下一页后显示 Alicia。

排查常见问题 ​

FaTable 没数据 ​

表格没有数据时,先在浏览器的「网络」面板检查 POST 响应和请求里的 searchValue。清空关键字再刷新,确认不是筛选条件排除了所有数据。响应为 HTTP 200 时,再检查 code、success 和 data.rows。本例中 requestApi 应返回解包后的分页对象。使用普通 JSON 或改过响应选项时,先核对调用方拿到的响应形状。

总数或页码不对 ​

确认翻页后的 POST 带着新 pageIndex 和当前 pageSize。本例页码从 1 开始。后端先用同一个条件筛选,再计算 totalRows 和截取 rows。例如搜索 Ali、每页 1 条时,两页的 totalRows 都应为 2。

请求打到前端端口 ​

开发者工具里看到 5174/api/users/search 是正常的,Vite 会把它转发到 5080。如果返回页面 HTML、404 或代理连接错误,先按上文直接向后端发送 POST 并确认返回 HTTP 200,再核对 Vite 代理目标、/api 前缀和请求方法是否一致。