Aggrid Php Example Updated [2027]
Replaced jQuery $.ajax with modern JavaScript fetch and async/await .
INSERT INTO employees (employee_name, job_title, department, salary) VALUES ('Alice Johnson', 'Software Engineer', 'Engineering', 95000), ('Bob Smith', 'Project Manager', 'Operations', 85000), ('Charlie Davis', 'UX Designer', 'Product', 78000), ('Diana Evans', 'Data Analyst', 'Marketing', 72000);
With a powerful backend endpoint ready, the next step is to connect it to AG Grid on the frontend. This involves a standard AG Grid setup in your Blade template, but with a crucial piece of configuration: a custom datasource . The datasource is the bridge that tells AG Grid how to fetch data from your PHP API.
If you are interested in exploring specific features like , cell editing , or server-side filtering , I can help you implement those as well. Let me know which functionality you'd like to explore next! aggrid php example updated
The true power of AG Grid emerges when you fully utilize its server-side row model, which allows your PHP backend to handle complex data operations that would be impossible to perform on the frontend alone.
// 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)
CREATE DATABASE IF NOT EXISTS company_db; USE company_db; CREATE TABLE IF NOT EXISTS employees ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, role VARCHAR(100) NOT NULL, department VARCHAR(100) NOT NULL, salary INT NOT NULL, join_date DATE NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; INSERT INTO employees (name, role, department, salary, join_date) VALUES ('Alice Smith', 'Senior Developer', 'Engineering', 95000, '2021-03-15'), ('Bob Jones', 'UI Designer', 'Design', 72000, '2022-06-01'), ('Charlie Brown', 'Product Manager', 'Product', 105000, '2020-11-12'), ('Diana Prince', 'QA Engineer', 'Engineering', 68000, '2023-01-10'), ('Evan Wright', 'Data Analyst', 'Analytics', 80000, '2019-08-24'), ('Fiona Gallagher', 'HR Specialist', 'People', 62000, '2021-05-19'), ('George Clark', 'DevOps Engineer', 'Engineering', 98000, '2020-02-05'); Use code with caution. 2. The PHP Backend ( data.php ) Replaced jQuery $
First, ensure your Laravel project is set up and you have your database credentials configured in your .env file. Then, install the package via Composer:
public function __invoke(AgGridGetRowsRequest $request): AgGridQueryBuilder
Do you plan to add capabilities?
use Clickbar\AgGrid\AgGridSetValuesRequest; use Clickbar\AgGrid\AgGridQueryBuilder; use App\Models\Product;
Using ag-theme-alpine (or ag-theme-quartz in the newest versions) for a modern look. 5. Next Steps: Server-Side Operations
Optionally, you can publish the package's configuration file for further customization, though the default settings are often sufficient to get started: The datasource is the bridge that tells AG
