m2mtech / api-platform-datatables-format
Datatables format extension for API Platform.
Installs: 83
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 0
Open Issues: 1
Type:symfony-bundle
Requires
- php: >=7.2.5
- api-platform/core: ^2.5
- symfony/config: ^5.4|^6.0
- symfony/dependency-injection: ^5.4|^6.0
- symfony/http-kernel: ^5.4|^6.0
Requires (Dev)
- phpstan/phpstan: ^1.4
- phpunit/phpunit: ^8.5.22|^9.5
- roave/security-advisories: dev-latest
- symfony/phpunit-bridge: ^5.4|^6.0
- symfony/stopwatch: ^5.4|^6.0
- symplify/easy-coding-standard: ^10.0
Suggests
- metaclass-nl/filter-bundle: dev-master
README
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.