3

How can you setup pagination on Datatables with Elasticsearch? I've already populated the Datatables with Elasticsearch result but it's not rendering pagination properly.

enter image description here

clicking on the numbered links will yield same data

Here's my JS code:

var searchResultTable = $('#search-results').DataTable({
    "processing": true,
    "serverSide": true,
    "ajax": "elasticsearch/",
    "dom": "<'row'<'col-sm-6'l>>" + "<'row'<'col-sm-12'tr>>" + "<'row'<'col-sm-5'i><'col-sm-7'p>>",
});

Here's my views.py (An excerpt; I'm using Django):

def elasticsearch_result_json(request):
    search_query = request.GET.get('search[value]', None)
    results = search_service.get_remittances_json(search_query)

    return JsonResponse(
        {
        'data': results
        }
    )

The pagination is now working correctly but the data the table is showing is the same in all pages. enter image description here Here's my updated views.py:

def elasticsearch_result_json(request):
    search_query = request.GET.get('search[value]', None)
    results = search_service.get_remittances_json(search_query)
    total_records = search_service.get_total_count()

    return JsonResponse({
        'draw': request.GET.get('draw', 0),
        'recordsTotal': total_records,
        'recordsFiltered': len(results),
        'data': results,
    })
5
  • 2
    In the request, you will see the parameters relating to paging, sorting etc. You need to use these in your query to handle paginination.. See datatables.net/manual/server-side Commented Sep 22, 2016 at 9:34
  • @markpsmith see edit. Thanks! Commented Sep 26, 2016 at 10:55
  • Paging is not working because you need to use the start and 'length' parameters in your query. Commented Sep 26, 2016 at 11:07
  • @markpsmith I'm having trouble doing what you said. I added "displayStart": 10 and "pageLength": 10 but the problem persists. Commented Sep 27, 2016 at 9:41
  • No, you need to get the datatables start and length parameters - in the same way as you do for search and draw - and use them in your SQL query to page the data. Without this, you're just returning the same data on every time. Commented Sep 27, 2016 at 14:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.