Solved
Search and Pagination Component
Hi,
I am a beginner in AEM,I want to implement the following function as in screenshot. This site has a paging function. How can I implement it.

Best regards!
Hi,
I am a beginner in AEM,I want to implement the following function as in screenshot. This site has a paging function. How can I implement it.

Best regards!
Hi @krist_wang ,
From your Ajax call, call the servlet,
bin/public/aem-demo/blog?page=2
Where the page params will be the page number.
Try with the following Java code and change them according to your logic.
@Component(service = Servlet.class,
property = {
Constants.SERVICE_DESCRIPTION + "= Servlet",
ServletResolverConstants.SLING_SERVLET_METHODS + "=" + HttpConstants.METHOD_GET,
ServletResolverConstants.SLING_SERVLET_PATHS + "=bin/public/aem-demo/blog"
})
public class PaginationServlet extends SlingAllMethodsServlet {
@Reference
private QueryBuilder queryBuilder;
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
String pageNumber = request.getParameter("page") != null ?request.getParameter("page") : "1";
Map<String, String> searchPredicates = searchPredicates("10", pageNumber);
Query query = queryBuilder.createQuery(PredicateGroup.create(searchPredicates), request.getResourceResolver().adaptTo(Session.class));
SearchResult searchResult = query.getResult();
// Convert search results to a list of pages
List<MyModel> items = new ArrayList<>();
for (Hit hit : searchResult.getHits()) {
try {
Page page = hit.getResource().adaptTo(Page.class);
// Put necessary Items into your model
MyModel model = new MyModel(page.getTitle(), page.getPath());
items.add(model);
} catch (RepositoryException e) {
log.error("Error Happened {}", e, e.getMessage());
}
}
response.setHeader("content-type", "application/json;charset=UTF-8");
response.setStatus(response.SC_OK);
response.getWriter().write(new ObjectMapper().writeValueAsString(items));
}
private Map<String, String> searchPredicates(String itemPerPage, String pageNum) {
Map<String, String> predicateMap = new HashMap<>();
predicateMap.put("path", "/content/aem-demo/master");
predicateMap.put("property", "cq:Page");
predicateMap.put("p.limit", itemPerPage);
predicateMap.put("p.offset", pageNum);
return predicateMap;
}
}
Hope this helps you.
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.