m2mtech/api-platform-datatables-format

Datatables format extension for API Platform.

v1.1.1 2022-02-11 21:36 UTC

This package is auto-updated.

Last update: 2024-04-25 23:39:46 UTC


README

Author Software License

This bundle provides the Datatables format to the API Platform.

Installation

composer require m2mtech/api-platform-datatables-format

If you are not using Flex enable the bundle:

// config/bundles.php

return [
    // ...
    M2MTech\ApiPlatformDatatablesFormat\M2MTechApiPlatformDatatablesFormatBundle::class => ['all' => true],
];

Usage

Enable the datatables format:

# config/packages/api_platform.yaml
api_platform:
    formats:
        datatables: [ 'application/vnd.datatables+json' ]

Pagination

The package rewrites the query parameters start and length from datatables to page and itemsPerPage or whatever you have set as page_parameter_name and items_per_page_parameter_name for the API Platform.

e.g.:

- /api/offers?draw=1&start=0&length=10
+ /api/offers?draw=1&page=1&itemsPerPage=10

Pagination is enabled by default in the API Platform.

Sorting

The package rewrites the query parameters columns and order from datatables to order or whatever you have set as order_parameter_name for the API Platform.

e.g.:

- /api/offers?draw=2&columns[0][data]=name&columns[1][data]=price&order[0][column]=1&order[0][dir]=desc
+ /api/offers?draw=2&order[email]=desc

You need to enable sorting for the API Platform.

e.g. in your entity definition:

#[ApiFilter(OrderFilter::class, properties: ['name', 'price'])]

Search

The package rewrites the query parameters columns and search from datatables to or for the Filter logic for API Platform.

e.g.:

- /api/offers?draw=3&columns[0][data]=name&columns[1][data]=description&search[value]=shirt 
+ /api/offers?draw=2&or[name]=shirt&or[desciption]=shirt

You need to install Filter logic for API Platform, an equivalent bundle or your own filter for this functionality, e.g.:

composer require metaclass-nl/filter-bundle "dev-master"

You need also to enable the search filter for the API Platform.

e.g. in your entity definition:

#[ApiFilter(SearchFilter::class, properties: ['name' => 'partial', 'description' => 'partial'])]
#[ApiFilter(FilterLogic::class)]

Output

Including the data, the output contains recordsTotal and recordsFiltered (which are always the same) as well as the draw parameter from the query.

Testing

This package has been developed for php 7.4 with compatibility tested for php 7.2 to 8.1.

composer test

Changelog

Please see CHANGELOG for more information about recent changes.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.