Datagrid接收的json數(shù)據(jù)格式有兩種: 不分頁數(shù)據(jù): [ {key:value,…},//每個(gè)json對(duì)象就是一條記錄 {key:value,…} ]//元素的數(shù)量就是總記錄數(shù)
分頁數(shù)據(jù): { total:100,//總記錄數(shù) rows:[ {},{} ]//當(dāng)前頁的json數(shù)組數(shù)據(jù) }
請(qǐng)求參數(shù)中自動(dòng)提交了兩個(gè)參數(shù)page(當(dāng)前頁碼)和rows(每頁顯示的最大記錄數(shù))。 服務(wù)器返回json數(shù)據(jù)后,客戶端將自動(dòng)顯示數(shù)據(jù)列表。
客戶端分頁的相關(guān)計(jì)算和顯示全部都交給了Datagrid,我們只需要在服務(wù)端拿到page和rows,根據(jù)他們查詢當(dāng)前頁對(duì)應(yīng)的數(shù)據(jù),將數(shù)據(jù)以json格式返回交給Datagrid即可。
PagingAndSortingRepository接口中提供了分頁方法:Page findAll(Pageable pageable)
Pageable接口對(duì)象,Spring Data提供了一個(gè)默認(rèn)的實(shí)現(xiàn)類PageRequest: 構(gòu)造方法中有兩個(gè)參數(shù): 參數(shù)1:page:當(dāng)前頁碼 參數(shù)2:size:每頁最大的記錄數(shù)
public PageRequest(int page, int size) { this(page, size, null); }Page接口對(duì)象,Spring Data提供了一個(gè)默認(rèn)的實(shí)現(xiàn)類PageImpl,提供了大量方法。包含對(duì)應(yīng)total和rows的方法。
//Returns the total amount of elements,獲取總記錄數(shù) public long getTotalElements() { return total; } //Returns the page content as {@link List},返回分頁記錄 public List<T> getContent() { return Collections.unmodifiableList(content); }常用方法:
getNumber():獲取當(dāng)前頁碼 getSize():獲取當(dāng)前頁顯示的最大記錄數(shù) getTotalPages():獲取總的頁數(shù) List getContent();獲取當(dāng)前頁的數(shù)據(jù)列表。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注