Operation using a grid

In previous chapters, we have created the Sylius resource and basic operations. Now we need to create the index operation using a Grid. To achieve that, we reuse the query we already have in the Application folder to create a grid provider.

src
├── BookStore
│   ├── Application
│   │   └── Query
│   │       ├── FindBooksQuery.php
│   │       └── FindBooksQueryHandler.php
│   ├── Domain
│   └── Infrastructure
│       └── Sylius
│           └── Grid
│               ├── Filter
│               │   ├── AuthorFilter.php
│               │   └── AuthorFilterType.php
│               ├── BookGrid.php
│               └── BookGridProvider.php
└── Shared
    └── Infrastructure
        └── Sylius
            └── Grid
                └── GridPageResolver.php

Book list

List of book resources

In the Application folder, we already have this FindBooksQuery:

And its query handler:

The idea is to reuse this query to list book from your storage for your "index" operation.

Create a page helper

To resolve current page and items per page in the grid provider, we can use this helper:

Create the BookGridProvider

First, we need to create the BookGridProvider.

Create the BookGrid

Now, we need to create the BookGrid.

Add the grid on the Book Resource

Now that we have a grid, let's add it to the "index" operation on our BookResource.

Create the Author filter type

Let's imagine we want to be able to filter books by their author within our grid. First, we need to create a Symfony form type for our custom author filter.

Create the Author filter

Use the Author Filter on the Grid provider

Last updated