Aggrid Php Example Updated Jun 2026

// Close the database connection $conn->close();

Create a PHP backend that will interact with the AG Grid application. Our backend will consist of two files: grid.php and data.php .

// Create the grid $grid = new ag_grid($options);

<!DOCTYPE html> <html> <head> <!-- Include AG Grid Community CSS and JS via CDN --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ag-grid-community/styles/ag-grid.css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ag-grid-community/styles/ag-theme-alpine.css"> <script src="https://cdn.jsdelivr.net/npm/ag-grid-community/dist/ag-grid-community.min.js"></script> </head> <body> <div id="myGrid" style="height: 500px; width: 100%;" class="ag-theme-alpine"></div>

// Fetch initial data fetchGridData(); );

$query = User::query(); return AgGridQueryBuilder::forRequest($request, $query); aggrid php example updated

<?php

For large datasets, don't load everything at once. Use the SSRM to fetch data in blocks as the user scrolls.

// Define the grid options $options = [ 'columnDefs' => $columns, 'rowData' => [] ];

// backend.php header('Content-Type: application/json'); // Database Connection $host = 'localhost'; $db = 'your_database'; $user = 'username'; $pass = 'password'; $pdo = new PDO("mysql:host=$host;dbname=$db", $user, $pass); // Get Request from AG Grid $request = json_decode(file_get_contents('php://input'), true); // 1. Pagination & Limits $start = $request['startRow'] ?? 0; $end = $request['endRow'] ?? 20; $limit = $end - $start; // 2. Sorting $sortSql = ""; if (isset($request['sortModel']) && !empty($request['sortModel'])) $sortModel = $request['sortModel'][0]; $sortSql = " ORDER BY " . $sortModel['colId'] . " " . $sortModel['sort']; // 3. Filtering $whereSql = " WHERE 1=1 "; if (isset($request['filterModel']) && !empty($request['filterModel'])) foreach ($request['filterModel'] as $colId => $filter) if ($filter['filterType'] === 'text') $whereSql .= " AND $colId LIKE '%" . $filter['filter'] . "%'"; // Add more filter types (number, date) as needed // 4. Fetch Data $sql = "SELECT * FROM users $whereSql $sortSql LIMIT $start, $limit"; $stmt = $pdo->prepare($sql); $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); // 5. Total Rows (required for pagination) $totalRowsStmt = $pdo->query("SELECT COUNT(*) FROM users $whereSql"); $totalRows = $totalRowsStmt->fetchColumn(); // 6. Return Response echo json_encode([ 'rows' => $rows, 'totalRows' => (int)$totalRows ]); Use code with caution. 5. Key Updates and Best Practices (2026)

This guide focuses on an updated implementation for , utilizing modern PHP best practices and AG Grid's latest Server-Side Row Model (SSRM) features. 1. The Strategy: Server-Side Row Model (SSRM) // Close the database connection $conn-&gt;close(); Create a

To add filtering and sorting, update the grid.php file to include the following code.

Backend (api/users.php)

In an updated stack, you move away from rendering HTML tables on the server. Instead, PHP acts as the —using a framework like Laravel or a simple Slim app—to serve JSON. AG Grid sits on the frontend, consuming that JSON. This separation allows for "Server-Side Row Model" features, where the grid only loads the data visible to the user, making it capable of handling millions of rows without crashing the browser. Data Fetching and CRUD An effective implementation involves a few key steps:

With this simple setup, the ProductGridController is now capable of receiving a request from AG Grid and returning the exact slice of filtered, sorted, and paginated data your page needs. This approach is not only efficient but also remarkably clean, keeping your business logic and API structure maintainable.

Using AG Grid Server-Side row model with Angular, Laravel & MySQL Use the SSRM to fetch data in blocks as the user scrolls

Build a high-performance AG Grid using PHP backend to handle large datasets with server-side pagination, sorting, and filtering.

php artisan vendor:publish --tag="ag-grid-laravel-config"

) .then(response => response.json()) .then(data => gridApi.setGridOption( Use code with caution. Copied to clipboard 3. Backend Data Source (PHP) script serves as the API. Ensure you set the correct Content-Type header so the browser interprets it as JSON. query( "SELECT id, name, email FROM users" ); $results = $stmt->fetchAll(PDO::FETCH_ASSOC); // Output JSON for AG Grid json_encode($results); (PDOException $e) json_encode([ => $e->getMessage()]);

// Eager-load the 'category' relationship for each product $query = Product::with('category')->orderBy('id'); return AgGridQueryBuilder::forRequest($request, $query);