Aggrid Php Example Updated !link! — Fast

AG Grid is a popular JavaScript library used to create interactive, feature-rich data grids. It offers a wide range of features, including support for large datasets, customizable columns, row selection, filtering, sorting, and more. AG Grid is highly customizable and can be easily integrated with various frameworks and libraries, including PHP.

// Define the grid columns $columns = [ ['headerName' => 'Name', 'field' => 'name'], ['headerName' => 'Email', 'field' => 'email'], ['headerName' => 'Department', 'field' => 'department'] ];

// Update the grid data $options['rowData'] = $data; aggrid php example updated

PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]); catch (PDOException $exception) echo json_encode(["error" => "Database connection failed: " . $exception->getMessage()]); exit; // 2. Parse AG Grid Request Payload $input = json_decode(file_get_contents("php://input"), true); $startRow = isset($input['startRow']) ? (int)$input['startRow'] : 0; $endRow = isset($input['endRow']) ? (int)$input['endRow'] : 100; $pageSize = $endRow - $startRow; $sortModel = isset($input['sortModel']) ? $input['sortModel'] : []; $filterModel = isset($input['filterModel']) ? $input['filterModel'] : []; // 3. Build Base Queries $whereClauses = []; $params = []; // Whitelist of columns to prevent SQL injection during dynamic sorting $allowedColumns = ['id', 'name', 'role', 'department', 'salary', 'join_date']; // 4. Handle Server-Side Filtering if (!empty($filterModel)) foreach ($filterModel as $column => $filterData) if (!in_array($column, $allowedColumns)) continue; $filterType = $filterData['filterType'] ?? ''; $type = $filterData['type'] ?? ''; $filterText = $filterData['filter'] ?? ''; if ($filterType === 'text') if ($type === 'contains') $whereClauses[] = "`$column` LIKE :filter_$column"; $params["filter_$column"] = "%" . $filterText . "%"; elseif ($type === 'equals') $whereClauses[] = "`$column` = :filter_$column"; $params["filter_$column"] = $filterText; elseif ($filterType === 'number') if ($type === 'equals') $whereClauses[] = "`$column` = :filter_$column"; $params["filter_$column"] = $filterText; elseif ($type === 'greaterThan') $whereClauses[] = "`$column` > :filter_$column"; $params["filter_$column"] = $filterText; $whereSql = ''; if (!empty($whereClauses)) $whereSql = ' WHERE ' . implode(' AND ', $whereClauses); // 5. Get Total Row Count Matching Filters $countSql = "SELECT COUNT(*) FROM employees" . $whereSql; $countStmt = $pdo->prepare($countSql); $countStmt->execute($params); $totalRows = (int)$countStmt->fetchColumn(); // 6. Handle Server-Side Sorting $sortSql = ''; if (!empty($sortModel)) $sortParts = []; foreach ($sortModel as $sort) $colId = $sort['colId']; $direction = strtoupper($sort['sort']) === 'DESC' ? 'DESC' : 'ASC'; if (in_array($colId, $allowedColumns)) $sortParts[] = "`$colId` $direction"; if (!empty($sortParts)) $sortSql = ' ORDER BY ' . implode(', ', $sortParts); // 7. Handle Pagination Limit Clauses $limitSql = " LIMIT :limit OFFSET :offset"; // 8. Execute Final Data Fetch Query $dataSql = "SELECT * FROM employees" . $whereSql . $sortSql . $limitSql; $dataStmt = $pdo->prepare($dataSql); // Bind bindValue explicitly to enforce integers for LIMIT/OFFSET foreach ($params as $key => $value) $dataStmt->bindValue($key, $value); $dataStmt->bindValue(':limit', $pageSize, PDO::PARAM_INT); $dataStmt->bindValue(':offset', $startRow, PDO::PARAM_INT); $dataStmt->execute(); $rows = $dataStmt->fetchAll(); // 9. Send Standard AG Grid Response echo json_encode([ "rows" => $rows, "totalRows" => $totalRows ]); Use code with caution. 3. The Frontend ( index.html )

To get started, download the AG Grid library from the official website. For this example, we'll use the community edition. AG Grid is a popular JavaScript library used

This script connects to the database using PHP Data Objects (PDO). It enforces strict error handling and returns payload output cleanly formatted as a JSON array. Use code with caution. Key Performance & Optimization Tips

Will you be dealing with that require server-side pagination? // Define the grid columns $columns = [

<?php // Include the AG Grid library require_once 'ag-grid-community.js';

Always use prepared statements to prevent SQL injection.

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