Skip to content

docs: Update usage section to include passing the query builder #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading