Php Example — Ag-grid

// Build base query $sql = "SELECT * FROM users WHERE 1=1"; $params = [];

// ... same SQL building logic as above ...

echo json_encode([ 'rows' => $rows, 'lastRow' => $totalRows ]); ?>

// Apply filters if (!empty($filterModel)) { foreach ($filterModel as $field => $filter) { $filterType = $filter['filterType'] ?? 'text'; $type = $filter['type'] ?? 'equals'; $filterValue = $filter['filter'] ?? ''; ag-grid php example

case 'date': // Add date filtering logic as needed break; } } }

$limit = $endRow - $startRow;

$startRow = $input['startRow'] ?? 0; $endRow = $input['endRow'] ?? 100; $sortModel = $input['sortModel'] ?? []; $filterModel = $input['filterModel'] ?? []; // Build base query $sql = "SELECT *

// Grid configuration const gridOptions = { columnDefs: columnDefs, rowModelType: 'serverSide', serverSideStoreType: 'partial', cacheBlockSize: 100, pagination: true, paginationPageSize: 100, enableSorting: true, enableFilter: true, animateRows: true, onGridReady: function(params) { params.api.setServerSideDatasource(dataSource); } };

// Apply sorting if (!empty($sortModel)) { $orderBy = []; foreach ($sortModel as $sort) { $colId = $sort['colId']; $sortDir = $sort['sort']; $orderBy[] = "$colId $sortDir"; } $sql .= " ORDER BY " . implode(', ', $orderBy); } else { $sql .= " ORDER BY id ASC"; }

INSERT INTO users (name, email, age, country, salary) VALUES ('John Doe', 'john@example.com', 28, 'USA', 55000), ('Jane Smith', 'jane@example.com', 32, 'Canada', 65000), ('Carlos Mendez', 'carlos@example.com', 45, 'Mexico', 48000); <?php header('Content-Type: application/json'); header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: GET, POST'); $host = 'localhost'; $dbname = 'ag_grid_demo'; $username = 'root'; $password = ''; 'text'; $type = $filter['type']

try { $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { die(json_encode(['error' => 'Database connection failed'])); } ?> <?php require_once 'config.php'; // Get request parameters from AG Grid $startRow = isset($_GET['startRow']) ? (int)$_GET['startRow'] : 0; $endRow = isset($_GET['endRow']) ? (int)$_GET['endRow'] : 100; $sortModel = isset($_GET['sortModel']) ? json_decode($_GET['sortModel'], true) : []; $filterModel = isset($_GET['filterModel']) ? json_decode($_GET['filterModel'], true) : [];

/ag-grid-php-example ├── index.html (AG Grid frontend) ├── data.php (Server-side data handler) └── config.php (Database configuration) 1. Database Setup (MySQL) CREATE DATABASE ag_grid_demo; USE ag_grid_demo; CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), email VARCHAR(100), age INT, country VARCHAR(50), salary DECIMAL(10,2), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );

// Execute main query $stmt = $pdo->prepare($sql); foreach ($params as $key => $value) { $stmt->bindValue($key, $value); } $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

const queryString = new URLSearchParams(requestData).toString();