How to indicate hasNext from server side processing

How to indicate hasNext from server side processing

marianolcmarianolc Posts: 3Questions: 1Answers: 0

Hi! I was able to show my database data without problems. The only issue I'm having is that the datatable has a page control that always shows 1,2,3,4,5...

Right now I have only 15 records so it should show only page 1 and 2.

From server side I'm using a JPA repository so I can know if I have more rows:

@Service("DocumentService")
public class DocumentServiceImpl implements DocumentService {

    @Autowired
    private DocumentRepository documentRepository;

    @Override
    public List<Document> getAllDocuments(Pageable pageable) {
        Page<Document> page = documentRepository.findAll(pageable);
        // page.hasNext() WHAT TO DO WITH THIS VALUE?
        return page.getContent();
    }
}

Right now the @RestController is doing this:

    @RequestMapping(path = "/listdocuments", method = RequestMethod.GET)
    public List<Documents> getAllDocuments(HttpServletRequest request) {
        return documentService.getAllDocuments(RequestPageable.buildPageableByRequest(request));
    }

What should I do to inform the datable the pageCount/hasNext values to the controller?

Thanks!

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,237Questions: 1Answers: 10,418 Site admin
    Answer ✓

    DataTables' server-side processing parameters are documented here, including the information it expects to get in return.

    You should only really need to use server-side processing if you are working with tens of thousands of records.

    Allan

  • marianolcmarianolc Posts: 3Questions: 1Answers: 0

    Sorry.. i'm getting 404 page not found. Right now I have a few records because I'm starting with the app.. but It will grew a lot.

  • marianolcmarianolc Posts: 3Questions: 1Answers: 0

    Hi! thanks for the link. I was able to show the correct information like this:

    Edited the javascript, added this:

    "sAjaxDataProp": "aoData",
            "fnServerData": function (sSource, aoData, fnCallback) {
                $.ajax({
                    "dataType": 'json',
                    "type": "GET",
                    "url": sSource,
                    "data": aoData,
                    "success":fnCallback
                });
            },
    

    And then changed the response to this(added recordsTotal and recordsFiltered in the response json)

        @RequestMapping(path = "/listdocuments", method = RequestMethod.GET)
        public @ResponseBody
        String getAllDocuments(HttpServletRequest request) {   
            Gson gson = new Gson();
            JsonObject jsonResponse = new JsonObject();
            DataResponse data = documentService.getAllDocuments(RequestPageable.buildPageableByRequest(request));
            jsonResponse.add("aoData", gson.toJsonTree(data.getData()));
            jsonResponse.addProperty("recordsTotal", data.getRecordsTotal());
            jsonResponse.addProperty("recordsFiltered", data.getRecordsTotal());
            return jsonResponse.toString();
        }
    
This discussion has been closed.