PageJson分页数据对象实体

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 分页数据对象实体
 * @version: 1.0
 */
public class PageJson {

    /**
     * 当前页号
     */
    private Integer page;
    /**
     * 总页数
     */
    private Integer total;
    /**
     * 总记录数
     */
    private Long records;

    /**
     * 数据列表
     */
    private List<Map<String, Object[]>> rows = null;

    /**
     * @description 设置分页数据
     * @param pageInfo 分页对象
     * @return void
     */
    private void setPageInfo(PageInfo<?> pageInfo) {
        this.page = pageInfo.getPageNum();
        this.total = pageInfo.getPages();
        this.records = pageInfo.getTotal();
    }

    public PageJson() {
    }

    public PageJson(List<JSONObject> list, String[] propertyNames) {
        try {
            this.rows = buildRows(list, propertyNames);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public PageJson(List<JSONObject> list, String[] propertyNames, String orderName) {
        try {
            this.rows = buildRowsWithOrder(list, propertyNames, orderName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public PageJson(PageInfo<JSONObject> pageInfo) {
        setPageInfo(pageInfo);
    }

    /**
     * @description 构建分页列表对象
     * @param pageInfo 分页对象
     * @param propertyNames 列表展示属性
     * @return
     */
    public PageJson(PageInfo<JSONObject> pageInfo, String[] propertyNames) {
        setPageInfo(pageInfo);
        try {
            this.rows = buildRows(pageInfo.getList(), propertyNames);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @description 构建带自增列的分页对象
     * @param pageInfo 分页对象
     * @param propertyNames 列表展示属性
     * @param orderName 自增列的属性名
     * @return
     */
    public PageJson(PageInfo<JSONObject> pageInfo, String[] propertyNames, String orderName) {
        setPageInfo(pageInfo);
        try {
            this.rows = buildRowsWithOrder(pageInfo.getList(), propertyNames, orderName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @description: 根据属性顺序构建返回的列表数据
     * @param: [pageList, propertyNames]
     * @return java.util.List<java.util.Map < java.lang.String, java.lang.Object [ ]>>
     */
    public List<Map<String, Object[]>> buildRows(List<JSONObject> pageList, String[] propertyNames) throws InvocationTargetException, IllegalAccessException, IntrospectionException {
        int rowCount = pageList.size();
        List<Map<String, Object[]>> rows = new ArrayList<Map<String, Object[]>>(rowCount);
        if (rowCount > 0) {
            int columnLen = propertyNames.length;
            Map<String, Object[]> rowMap = null;
            Object[] colums = null;
            JSONObject bean = null;
            Object value = null;
            int row = 0, col = 0;
            for (row = 0; row < rowCount; row++) {
                bean = pageList.get(row);
                colums = new Object[columnLen];
                for (col = 0; col < columnLen; col++) {
                    value = bean.getString(propertyNames[col]);
                    colums[col] = value ==  null ? "" : String.valueOf(value);
                }
                rowMap = new HashMap<String, Object[]>(1);
                rowMap.put("cell", colums);
                rows.add(rowMap);
            }
        }
        return rows;
    }

    /**
     * @description 根据属性顺序构建返回的列表数据
     * @param pageList 列表数据
     * @param propertyNames 列表展示属性
     * @param orderProperty 排序属性名
     * @return java.util.List<java.util.Map < java.lang.String, java.lang.Object [ ]>>
     */
    public List<Map<String, Object[]>> buildRowsWithOrder(List<JSONObject> pageList, String[] propertyNames, String orderProperty) throws InvocationTargetException, IllegalAccessException, IntrospectionException {
        int rowCount = pageList.size();
        List<Map<String, Object[]>> rows = new ArrayList<Map<String, Object[]>>(rowCount);
        if (rowCount > 0) {
            int columnLen = propertyNames.length;
            Map<String, Object[]> rowMap = null;
            Object[] colums = null;
            JSONObject bean = null;
            Object value = null;
            int row = 0, col = 0;
            for (row = 0; row < rowCount; row++) {
                bean = pageList.get(row);
                colums = new Object[columnLen];
                for (col = 0; col < columnLen; col++) {
                    if (propertyNames[col].equals(orderProperty)) { //自增序号列
                        value = row + 1;
                    } else {
                        value = bean.getString(propertyNames[col]);
                    }
                    colums[col] = value ==  null ? "" : String.valueOf(value);
                }
                rowMap = new HashMap<String, Object[]>(1);
                rowMap.put("cell", colums);
                rows.add(rowMap);
            }
        }
        return rows;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public Long getRecords() {
        return records;
    }

    public void setRecords(Long records) {
        this.records = records;
    }

    public List<Map<String, Object[]>> getRows() {
        return rows;
    }

    public void setRows(List<Map<String, Object[]>> rows) {
        this.rows = rows;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要生成一个包含多条数据JSON 数据,例如: ```json { "data": [ {"id": 1, "name": "Alice", "age": 20}, {"id": 2, "name": "Bob", "age": 22}, {"id": 3, "name": "Charlie", "age": 25}, {"id": 4, "name": "David", "age": 30}, {"id": 5, "name": "Emily", "age": 28}, {"id": 6, "name": "Frank", "age": 35}, {"id": 7, "name": "Grace", "age": 18}, {"id": 8, "name": "Henry", "age": 27}, {"id": 9, "name": "Ivy", "age": 23}, {"id": 10, "name": "Jack", "age": 32}, {"id": 11, "name": "Kate", "age": 29}, {"id": 12, "name": "Leo", "age": 26}, {"id": 13, "name": "Maggie", "age": 21}, {"id": 14, "name": "Nancy", "age": 24}, {"id": 15, "name": "Oliver", "age": 33}, {"id": 16, "name": "Penny", "age": 31}, {"id": 17, "name": "Quinn", "age": 19}, {"id": 18, "name": "Ray", "age": 36}, {"id": 19, "name": "Sandy", "age": 37}, {"id": 20, "name": "Tom", "age": 34} ] } ``` 接着,我们可以使用 JavaScript 编写一个函数来实现分页效果: ```javascript function paginate(data, page, pageSize) { const start = (page - 1) * pageSize; const end = start + pageSize; const paginatedData = data.slice(start, end); return paginatedData; } ``` 其中,`data` 表示要分页JSON 数据,`page` 表示当前页数,`pageSize` 表示每页显示的数据量。函数会根据当前页数和每页显示的数据量来计算出应该显示哪些数据,并返回分页后的数据。 例如,我们可以使用以下代码来测试分页效果: ```javascript const jsonData = { "data": [ {"id": 1, "name": "Alice", "age": 20}, {"id": 2, "name": "Bob", "age": 22}, {"id": 3, "name": "Charlie", "age": 25}, {"id": 4, "name": "David", "age": 30}, {"id": 5, "name": "Emily", "age": 28}, {"id": 6, "name": "Frank", "age": 35}, {"id": 7, "name": "Grace", "age": 18}, {"id": 8, "name": "Henry", "age": 27}, {"id": 9, "name": "Ivy", "age": 23}, {"id": 10, "name": "Jack", "age": 32}, {"id": 11, "name": "Kate", "age": 29}, {"id": 12, "name": "Leo", "age": 26}, {"id": 13, "name": "Maggie", "age": 21}, {"id": 14, "name": "Nancy", "age": 24}, {"id": 15, "name": "Oliver", "age": 33}, {"id": 16, "name": "Penny", "age": 31}, {"id": 17, "name": "Quinn", "age": 19}, {"id": 18, "name": "Ray", "age": 36}, {"id": 19, "name": "Sandy", "age": 37}, {"id": 20, "name": "Tom", "age": 34} ] }; const page = 2; const pageSize = 5; const paginatedData = paginate(jsonData.data, page, pageSize); console.log(paginatedData); ``` 输出结果如下: ```javascript [ {"id": 6, "name": "Frank", "age": 35}, {"id": 7, "name": "Grace", "age": 18}, {"id": 8, "name": "Henry", "age": 27}, {"id": 9, "name": "Ivy", "age": 23}, {"id": 10, "name": "Jack", "age": 32} ] ``` 可以看出,函数成功将第二页的数据(ID 为 6 到 10 的数据)返回了出来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值