2017-08-16 144 views
0

我正在開發一個spring-boot應用程序。我必須將Hashmap結果集作爲表格打印。爲此我用百里香創造了桌子。該表有時超過10萬條記錄。我希望每10或50條記錄對此表進行分頁。百里香和春 - 桌子的分頁?

我使用thymeleaf代碼片段HTML:

<!DOCTYPE html> 
<html xmlns:th="http://www.thymeleaf.org" 
    xmlns:dt="http://www.thymeleaf.org/dandelion/datatables"> 
<head lang="en"> 
. 
. 
<div id="myDivTable"> 
<table class="table table-bordered" id="bomTable" id="bomTable" 
dt:table="true" dt:displaylength="10"> 
    <span th:each="row, iter : ${result}" pages:paginate="5"> 
     <tr th:classappend="${iter.first} ? header-style"> 
      <span th:each="cell : ${row.value}"> 
      <td th:classappend="${#strings.contains(cell,'difference')}?set-difference-bg-color" > 
      <div th:switch="${cell}"> 
       <div th:case="'Only in WC'" > 
        <span class="set-green-text-bold" th:text="${cell}"> 
        </span> 
       </div> 
       <div th:case="'New in XLSX'" > 
        <span class="set-red-text-bold" th:text="${cell}"> 
        </span> 
       </div> 
       <div th:case="'No'" > 
        <span class="set-red-text-bold" th:text="${cell}"> 
        </span> 
       </div> 
       <div th:case="'Yes'" > 
        <span class="set-green-text-bold" th:text="${cell}"> 
        </span> 
       </div> 
       <div th:case="*" > 
        <div th:if="${#strings.contains(cell,'difference')}"> 
         <span 
th:text="${#strings.substring(cell,0,#strings.indexOf(cell,'difference'))}"> 
         </span>     
        </div> 
        <div th:unless="${#strings.contains(cell,'difference')}"> 
         <span th:text="${cell}"></span> 
        </div> 
       </div> 
      </div> 
      </td> 
      </span> 
     </tr> 
    </span> 
</table> 
</div> 
. 
. 

最近正在打印一個頁面上的所有記錄。我正在檢查120條記錄。我如何在每頁上分割10或50條記錄。我正在使用Thymeleaf。

我試圖使用蒲公英數據表,我在pom.xml中添加了依賴關係,創建了dandelinConfig類等,但它仍然沒有反映在結果中。

回答

0

您可以使用Dandelion Datatables

使用範例如下:

  <dependency> 
       <groupId>com.github.dandelion</groupId> 
       <artifactId>datatables-thymeleaf</artifactId> 
       <version>1.1.0</version> 
      </dependency> 
      <dependency> 
       <groupId>com.github.dandelion</groupId> 
       <artifactId>datatables-spring3</artifactId> 
       <version>1.1.0</version> 
      </dependency> 
      <dependency> 
       <groupId>com.github.dandelion</groupId> 
       <artifactId>dandelion-thymeleaf</artifactId> 
       <version>1.1.1</version> 
      </dependency> 

與配置類:

@Configuration 
public class DandelionConfig { 


    @Bean 
    public DandelionDialect dandelionDialect() { 
     return new DandelionDialect(); 
    } 

    @Bean 
    public DataTablesDialect dataTablesDialect(){ 
     return new DataTablesDialect(); 
    } 

    @Bean 
    public Filter dandelionFilter() { 

     return new DandelionFilter(); 
    } 

    @Bean 
    public ServletRegistrationBean dandelionServletRegistrationBean() { 

     return new ServletRegistrationBean(new DandelionServlet(), "/dandelion-assets/*"); 
    } 


} 

的,你應該資源文件夾下加蒲公英文件夾:/資源/蒲公英/。然後創建/resources/dandelion/sample.json文件象下面這樣:

{ 
    "bundle" : "custom", 
    "assets": [ 
    { 
     "name": "bootstrap4-datatables-css", 
     "type": "css", 
     "locations": { 
     "classpath": "static/css/dataTables.bootstrap4.min.css" 
     } 
    }, 
    { 
     "name": "jquery-datatables-js", 
     "type": "js", 
     "locations": { 
     "classpath": "static/js/jquery.dataTables.min.js" 
     } 
    }, 
    { 
     "name": "bootstrap4-datatables-js", 
     "type": "js", 
     "locations": { 
     "classpath": "static/js/dataTables.bootstrap4.min.js" 
     } 
    }, 

    } 
    ] 
} 

,並創建/resources/dandelion/dandelion.properties文件:

components.standalone=ddl-dt 
bundle.includes=custom 

添加aplication屬性文件components.standalone = ddl-dt

。最後一個html文件示例:

<html xmlns:th="http://www.thymeleaf.org" 
    xmlns:dt="http://www.thymeleaf.org/dandelion/datatables" 
    > 
<table id="paging-simple" dt:table="true" dt:pagingType="simple" class="display"> 
      <thead> 
       <tr> 
       <th>Id</th> 
       <th>Firstname</th> 
       <th>Lastname</th> 
       <th>City</th> 
       <th>Mail</th> 
       </tr> 
      </thead> 
      <tbody> 
       <tr th:each="person : ${persons}"> 
       <td th:text="${person?.id}">1</td> 
       <td th:text="${person?.firstName}">John</td> 
       <td th:text="${person?.lastName}">Doe</td> 
       <td th:text="${person?.address?.town?.name}">Nobody knows!</td> 
       <td th:text="${person?.mail}">[email protected]</td> 
       </tr> 
      </tbody> 
     </table> 

。最後,如果你想添加分頁你的項目,你會做阿賈克斯request.Detail是使用springboot,JAVA,thymeleaf,基金會(JS)和MySQL Dandelion Datatables Ajax

+0

這是行不通的。 – Ruchita

+0

它會工作,但你應該在你的項目中添加蒲公英dataTables配置。 – mrtasln

+0

我已添加: \t \t \t com.github。蒲公英 \t \t \t 數據表,thymeleaf \t \t \t 1.0.1 \t \t在pom.xml中。 – Ruchita

0

IM,idontknow約蒲公英,但隨着春天分頁我能做到這一點

public String listadoProductos(Pageable pageable, Model model) { 
if(pageable.getPageSize() > PAGE_SIZE_INITIAL) { 
    pageable = new PageRequest(0,PAGE_SIZE_INITIAL); 
} 
Page<Productos> productos = productosRepository.findByEnabled(true, pageable);//trae todos los productos habilitados 
model.addAttribute("productos", productos); 
modelPaginacion(model, productos, pageable.getPageNumber()); 

return tiendaFolder+"listaProductos";} 

與thyeleaf和基礎做到這一點:

<div class="row"> <ul class="paginacion text-center"> <li class="previous" th:if="${previo}"> <a th:href="@{/tienda/productos/admin?page={pa}&size={ps}(pa=${paginaActual-1},ps=${size})}"></a> </li> <li class="previa" th:if="${previo}"> <a th:href="@{/tienda/productos/admin?page={pa}&size={ps}(pa=${paginaActual-1},ps=${size})}" th:text="${paginaActual-1}"></a> </li> <li class="actual" th:text="${paginaActual}"> </li> <li class="siguiente" th:if="${siguiente}"> <a th:href="@{/tienda/productos/admin?page={pa}&size={ps}(pa=${paginaActual+1},ps=${size})}" th:text="${paginaActual+1}"></a> </li> <li class="next" th:if="${siguiente}"> <a th:href="@{/tienda/productos/admin?page={pa}&size={ps}(pa=${paginaActual+1},ps=${size})}"></a> </li> </ul> </div>

是隻有塊o頁數