PHP code example of thephpguys / spiral-datagrid-bundle

1. Go to this page and download the library: Download thephpguys/spiral-datagrid-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

thephpguys / spiral-datagrid-bundle example snippets


// config/bundles.php

return [
    // ...
    ThePhpGuys\SpiralDataGridBundle\SpiralDataGridBundle::class => ['all' => true],
];

namespace App\Grid;
use App\Entity\Product;
use Spiral\DataGrid\GridSchema;
use Spiral\DataGrid\Specification\Filter;
use Spiral\DataGrid\Specification\Pagination\PagePaginator;
use Spiral\DataGrid\Specification\Sorter\Sorter;
use Spiral\DataGrid\Specification\Value\StringValue;

final class ProductGrid extends GridSchema
{

    public function __construct()
    {
        $this->addFilter('search',  new Filter\Any(
            new Filter\Like('e.title'),
            new Filter\Equals('e.article')
        ));
        $this->addSorter('name',new Sorter('e.name'));
        $this->addSorter('article',new Sorter('e.article'));
        $this->setPaginator(new PagePaginator(10, [10, 20, 50, 100]));
    }

    public function withDefaults():array
    {
        return [
            GridFactory::KEY_SORT => ['name'=>'asc']
        ];
    }

    //If this method exists it will be used as response data transformer
    public function __invoke(Product $product):array
    {
        return [
            'id' => $product->getId(),
            'name' => $product->getName(),
            'article' => $product->getArticle(),
        ];
    }
}

use App\Entity\Product;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Routing\Attribute\Route;
use ThePhpGuys\SpiralDataGridBundle\Attribute\DataGrid

//...
#[Route('/products')]
#[DataGrid(grid: ProductGrid::class)]
public function productsList(EntityManagerInterface $entityManager):QueryBuilder
{
    return $entityManager->createQueryBuilder()->select('*')->from('e',Product::class);
} 
//...