在業務系統開發中,尤爲是後臺管理系統,列表頁展現的數據來自多個數據源,列表頁須要支持分頁,怎麼解決?數據庫
如上圖,數據源可能來自不一樣 DB 數據庫,可能來自不一樣 API 接口,也可能來自 DB 和 API 的組合。json
我這也沒有太好的解決方案,接到這樣的需求,確定首先和需求方溝通,這樣分頁是否合理。數組
無非就兩種方案:bash
若是以某一數據源進行分頁,其餘字段去其餘數據源獲取,這樣還好處理一些。less
若是以多個數據源融合後再分頁的話,就數據按期同步 或 內存中分頁吧。學習
數據按期同步方案能夠根據實際狀況去設計同步頻率,至於同步到 ES/MySQL/MongoDB 本身決定便可。ui
關於內存中分頁方案,下面分享兩個小方法,供參考。spa
$data = [
0 => ['name' => "姓名1", 'age' => "年齡1"],
1 => ['name' => "姓名2", 'age' => "年齡2"],
2 => ['name' => "姓名3", 'age' => "年齡3"],
3 => ['name' => "姓名4", 'age' => "年齡4"],
4 => ['name' => "姓名5", 'age' => "年齡5"],
5 => ['name' => "姓名6", 'age' => "年齡6"],
6 => ['name' => "姓名7", 'age' => "年齡7"],
7 => ['name' => "姓名8", 'age' => "年齡8"],
8 => ['name' => "姓名9", 'age' => "年齡9"],
9 => ['name' => "姓名10", 'age' => "年齡10"],
];
/**
* 數組分頁
* @param array $arrayData 數組數據
* @param int $page 第幾頁
* @param int $pageSize 每頁展現條數
* @return array
*/
function arrayToPageData($arrayData = [], $page = 1, $pageSize = 10)
{
$arrayData = array_values((array)$arrayData);
$pageData['list'] = array_slice($arrayData, ($page - 1) * $pageSize, $pageSize);
$pageData['pagination']['total'] = count($arrayData);
$pageData['pagination']['currentPage'] = $page;
$pageData['pagination']['prePageCount'] = $pageSize;
return $pageData;
}
echo json_encode(arrayToPageData($data, 2, 3));
複製代碼
輸出:設計
{
"list": [
{
"name": "姓名4",
"age": "年齡4"
},
{
"name": "姓名5",
"age": "年齡5"
},
{
"name": "姓名6",
"age": "年齡6"
}
],
"pagination": {
"total": 10,
"currentPage": 2,
"prePageCount": 3
}
}
複製代碼
package main
import (
"encoding/json"
"fmt"
)
type User []struct {
Name string `json:"name"`
Age string `json:"age"`
}
type Pagination struct {
Total int `json:"total"`
CurrentPage int `json:"currentPage"`
PrePageCount int `json:"prePageCount"`
}
type ListPageData struct {
List User `json:"list"`
Pagination Pagination `json:"pagination"`
}
func main() {
jsonStr := `[{"name": "姓名1","age": "年齡1"},
{"name": "姓名2","age": "年齡2"},
{"name": "姓名3","age": "年齡3"},
{"name": "姓名4","age": "年齡4"},
{"name": "姓名5","age": "年齡5"},
{"name": "姓名6","age": "年齡6"},
{"name": "姓名7","age": "年齡7"},
{"name": "姓名8","age": "年齡8"},
{"name": "姓名9","age": "年齡9"},
{"name": "姓名10","age": "年齡10"}
]`
var user User
err := json.Unmarshal([]byte(jsonStr), &user)
if err != nil {
fmt.Println(err.Error())
}
page := 2
pageSize := 3
pageData := ArraySlice(user, page, pageSize)
listPageData := ListPageData{}
listPageData.List = pageData
listPageData.Pagination.Total = len(user)
listPageData.Pagination.CurrentPage = page
listPageData.Pagination.PrePageCount = pageSize
jsonData, _ := JsonEncode(listPageData)
fmt.Println(jsonData)
}
func JsonEncode(v interface{}) (string, error) {
bytes, err := json.Marshal(v)
if err != nil {
return "", err
}
return string(bytes), nil
}
func ArraySlice(u User, page int, pageSize int) User {
offset := (page - 1) * pageSize
if offset > int(len(u)) {
panic("offset: the offset is less than the length of u")
}
end := offset + pageSize
if end < int(len(u)) {
return u[offset:end]
}
return u[offset:]
}
複製代碼
輸出:code
{
"list": [
{
"name": "姓名4",
"age": "年齡4"
},
{
"name": "姓名5",
"age": "年齡5"
},
{
"name": "姓名6",
"age": "年齡6"
}
],
"pagination": {
"total": 10,
"currentPage": 2,
"prePageCount": 3
}
}
複製代碼
若是你有更好的方案,歡迎留言評論 ~