Skip to content

Commit

Permalink
docs: update usage section to include passing the query builder (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kreyu authored May 11, 2024
1 parent 8ed310f commit 04b404a
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions docs/src/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,19 @@ From here, you can add [columns](components/columns.md), [filters](components/fi
In most cases, the data tables are created in the controller, using the `createDataTable()` method from the `DataTableFactoryAwareTrait`.

```php
use App\Repository\ProductRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Kreyu\Bundle\DataTableBundle\DataTableFactoryAwareTrait; // [!code ++]

class ProductController extends AbstractController
{
use DataTableFactoryAwareTrait; // [!code ++]

public function index()
public function index(ProductRepository $productRepository)
{
$dataTable = $this->createDataTable(ProductDataTableType::class); // [!code ++]
$query = $productRepository->createQueryBuilder('product'); // [!code ++]

$dataTable = $this->createDataTable(ProductDataTableType::class, $query); // [!code ++]
}
}
```
Expand All @@ -52,11 +55,15 @@ This method accepts _three_ arguments:
- data — in most cases, an instance of Doctrine ORM query builder;
- options — defined by the data table type, used to configure the data table;

In above example, we're passing an instance of Doctrine ORM query builder as data, not results.
This allows the bundle to paginate the results, apply filtration, and more.

## Handling the request

In order to be able to paginate, sort, filter, personalize or export the data table, call the `handleRequest()` method of the data table:

```php
use App\Repository\ProductRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Kreyu\Bundle\DataTableBundle\DataTableFactoryAwareTrait;
Expand All @@ -65,9 +72,11 @@ class ProductController extends AbstractController
{
use DataTableFactoryAwareTrait;

public function index(Request $request)
public function index(Request $request, ProductRepository $productRepository)
{
$dataTable = $this->createDataTable(ProductDataTableType::class);
$query = $productRepository->createQueryBuilder('product');

$dataTable = $this->createDataTable(ProductDataTableType::class, $query);
$dataTable->handleRequest($request); // [!code ++]
}
}
Expand All @@ -78,6 +87,7 @@ class ProductController extends AbstractController
In order to render the data table, create the data table view and pass it to the template:

```php
use App\Repository\ProductRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Kreyu\Bundle\DataTableBundle\DataTableFactoryAwareTrait;
Expand All @@ -86,9 +96,11 @@ class ProductController extends AbstractController
{
use DataTableFactoryAwareTrait;

public function index(Request $request)
public function index(Request $request, ProductRepository $productRepository)
{
$dataTable = $this->createDataTable(ProductDataTableType::class);
$query = $productRepository->createQueryBuilder('product');

$dataTable = $this->createDataTable(ProductDataTableType::class, $query);
$dataTable->handleRequest($request);

return $this->render('product/index.html.twig', [
Expand Down

0 comments on commit 04b404a

Please sign in to comment.