Paging does not work when processing on the server side

Paging does not work when processing on the server side

iraldoadiraldoad Posts: 4Questions: 2Answers: 0

My datatable works fine, except for paging, it is disabled.

        $('#datatable').dataTable({
            'paging': true,
            'ordering': true,
            'info': true,
            'order': [[2, 'desc']],
            "columnDefs": [
                {"orderable": false, "targets": 6}
            ],
            "processing": true,
            "serverSide": true,
            "ajax": 'list-load.php',
            "deferRender": true
        });
        public function loadListAction(Request $request)
        {
            $strSearch = $request->query->get('search');
            $start = (int)$request->query->get('0');
            $length = (int)$request->query->get('length');
    
            $rep = $this->getDoctrine()->getRepository(Reserva::class);
    
            // Filtered results
            $bookings = $rep->findForDatatable($strSearch, $start, $length)->getQuery()->getResult();
    
            // All records
            $total = $rep->countAll();
    
            $data = [];
            
            foreach ($bookings as $b) {
    
                $data[] = [
                    $b->getAd(),
                    $b->getCl(),
                    $b->getStart(),
                    $b->getEnd(),
                    $b->getEstado(),
                    $b->getGain(),
                    'html',
                ];
            }
    
            // Array for response
            $res = [
                'recordsTotal' => $total,
                'recordsFiltered' => count($bookings),
                'data' => $data,
            ];
    
            return new JsonResponse($res);
        }

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @iraldoad ,

    It looks like you're always getting page 0 with this line:

    $start = (int)$request->query->get('0');
    

    You should check the request and see what page the client is asking for,

    Cheers,

    Colin

  • iraldoadiraldoad Posts: 4Questions: 2Answers: 0

    Hi @colin now I wrote it this way. The error remains.

        $start = (int)$request->query->get('start');
    

    This is the response from the controller:

  • kthorngrenkthorngren Posts: 21,309Questions: 26Answers: 4,947

    You need to review this doc to understand the parameters sent to the server and the expected response:
    https://datatables.net/manual/server-side

    Your response is incomplete and the recordsFiltered looks to be incorrect. The draw parameter is used as a sequence number. You need to return the same draw value that was sent in the request. If this parameter is not correct then Datatables won't show the expected data. Your response doesn't appear to have this parameter but it is needed.

    Less important is recordsFiltered is the number of records filtered when searching, not the number of rows returned. The value you are returning is incorrect but shouldn't stop the page from displaying.

    Kevin

This discussion has been closed.