How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Create Ajax-Based Pagination in DataTables
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Create Ajax-Based Pagination in DataTables
Web Development

How to Create Ajax-Based Pagination in DataTables

how7o
By how7o
Last updated: May 22, 2026
6 Min Read
DataTables server-side Ajax pagination with Laravel
SHARE

To create Ajax-based pagination in DataTables — where the table loads one page at a time from the server instead of pulling every row — enable serverSide: true alongside your ajax URL. DataTables then sends start, length, search, order, and draw parameters on every interaction, and your endpoint returns just the slice for the current view.

Contents
  • Why the original config doesn’t paginate on the server
  • The fix — serverSide: true
  • The response shape
  • Laravel controller example
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 with DataTables 2.0. Originally published 2022-11-06, rewritten and updated 2026-05-17.

Why the original config doesn’t paginate on the server

$('#example').DataTable({
    ajax: 'ajax-url',
    columns: [
        { data: 'id' },
        { data: 'date' },
        // ...
    ],
});

This config tells DataTables to fetch the entire dataset once, then paginate, sort, and search it client-side. There’s no page parameter in the request because DataTables isn’t asking the server to paginate — it has every row already.

The fix — serverSide: true

$('#example').DataTable({
    serverSide: true,
    processing: true,
    ajax: '/api/example',
    columns: [
        { data: 'id' },
        { data: 'date' },
        { data: 'period' },
        { data: 'component' },
        { data: 'category' },
        { data: 'created_by' },
        { data: 'action' },
    ],
});

With serverSide: true, every page change, sort click, or search keystroke triggers a new Ajax request with these parameters (see the server-side docs):

  • draw — request counter (must be echoed back)
  • start — row offset (e.g. 0, 10, 20…)
  • length — page size (e.g. 10, 25, 50)
  • search[value] — current search box value
  • order[0][column] + order[0][dir] — sort column index + direction
  • columns[i][data], columns[i][searchable], columns[i][orderable] — per-column metadata
DataTables server-side ajax — request parameters and response shape

The response shape

{
  "draw": 1,
  "recordsTotal": 1000,
  "recordsFiltered": 1000,
  "data": [
    { "id": 1, "date": "2026-05-17", "period": "Q2", "component": "...",
      "category": "...", "created_by": "...", "action": "Edit" },
    ...
  ]
}
  • draw — the same value the request sent. Required.
  • recordsTotal — total row count before search (used for the “of N total” label).
  • recordsFiltered — row count after the search filter. Same as recordsTotal when no search is active.
  • data — array of rows for the current page (length matches the length param, except on the last page).

Laravel controller example

public function index(Request $request)
{
    $query = Report::query();

    if ($search = $request->input('search.value')) {
        $query->where(function ($q) use ($search) {
            $q->where('component', 'like', "%$search%")
              ->orWhere('category', 'like', "%$search%");
        });
    }

    $columns = ['id', 'date', 'period', 'component', 'category', 'created_by'];
    $orderCol = $columns[$request->input('order.0.column', 0)];
    $orderDir = $request->input('order.0.dir', 'asc');

    $total    = Report::count();
    $filtered = $query->count();

    $rows = $query
        ->orderBy($orderCol, $orderDir)
        ->skip((int) $request->input('start', 0))
        ->take((int) $request->input('length', 10))
        ->get();

    return response()->json([
        'draw'            => (int) $request->input('draw'),
        'recordsTotal'    => $total,
        'recordsFiltered' => $filtered,
        'data'            => $rows,
    ]);
}

Whitelist the orderable columns by index (don’t pass user input straight into orderBy()) and use parameterised where for the search — both like bindings are escaped by Eloquent. For larger datasets, swap skip()/take() for keyset pagination on an indexed column.

Frequently asked questions

What’s the difference between ajax and serverSide: true?

With just ajax, DataTables fetches every row in one request and then handles pagination, sorting, and search on the client. With serverSide: true, DataTables sends the current page, page size, sort, and search to your endpoint on every interaction, and your endpoint returns just the slice for that view. Use server-side when the dataset is too large to ship all at once (rule of thumb: more than a few thousand rows).

What is the draw parameter for, and why does the response have to include it?

draw is a monotonically increasing counter that DataTables sends with each request. Your server must echo it back unchanged in the response. It guarantees responses are rendered in request order even if the network reorders them — an older response with a smaller draw is discarded. Without echoing it, DataTables ignores the response.

How do I read DataTables’ request parameters in Laravel without using a package?

Pull them straight from the request: $request->input('start'), $request->input('length'), $request->input('search.value'), $request->input('order.0.column'), $request->input('order.0.dir'), and $request->input('draw'). Apply skip() + take() with Eloquent, use where(...like) for search, then return JSON in the DataTables shape. The dedicated yajra/laravel-datatables package wraps all of this if you want.

What if my data isn’t paginated at the source — can I still use server-side?

Yes but it defeats most of the benefit. If you load every row into memory and slice in PHP, the server still pays the full query cost on every request. The win only comes when you push start/length down to the database with LIMIT/OFFSET (or keyset pagination), and use a single COUNT(*) query for recordsTotal.

Related guides

  • How to Change the Default Sort Order in DataTables
  • How to Add an HTML Column in Laravel DataTables
  • How to Search in Custom or Composite Columns in Laravel DataTables

References

DataTables server-side processing: datatables.net/manual/server-side. serverSide option: datatables.net/reference/option/serverSide. Laravel DataTables package: yajrabox.com/docs/laravel-datatables.

TAGGED:ajaxdatatablesjQueryLaravel

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Laravel login and registration system with scaffolding packages How to Create a Login and Registration System in Laravel
Next Article Delete all lines in Vim with :%d How to Delete All Lines in a File with Vi or Vim
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026
Tailscale mesh — peer-to-peer connections between devices, coordination server
How to Install Tailscale on Ubuntu (Zero-Config Mesh VPN for Self-Hosters)
May 24, 2026

You Might Also Like

WooCommerce auto add to cart on visit — template_redirect hook and cart dedup
Web Development

How to Automatically Add a Product to Cart on Visit in WooCommerce

8 Min Read
Scroll to an element on a web page with jQuery
Web Development

How to Scroll to an Element Using jQuery

4 Min Read
Open a file selection dialog from a button click
Web Development

How to Open a File Dialog When Clicking a Button

4 Min Read
PHP string to float conversion with cast, regex cleanup, NumberFormatter
Web Development

How to Convert a String to Float in PHP

7 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?