m2mtech/api-platform-datatables-format

API Platform 的 Datatables 格式扩展。

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

This package is auto-updated.

Last update: 2024-08-26 00:43:12 UTC


README

Author Software License

此包为 API Platform 提供了 Datatables 格式

安装

composer require m2mtech/api-platform-datatables-format

如果您未使用 Flex,请启用此包

// config/bundles.php

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

使用方法

启用 datatables 格式

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

分页

该包将查询参数 startlengthdatatables 重写为 pageitemsPerPage 或您为 API Platform 设置的 page_parameter_nameitems_per_page_parameter_name

例如:

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

API Platform 中默认启用了分页。

排序

该包将查询参数 columnsorderdatatables 重写为 order 或您为 API Platform 设置的 order_parameter_name

例如:

- /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

您需要为 API Platform 启用排序。

例如,在实体定义中

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

搜索

该包将查询参数 columnssearchdatatables 重写为 or 以供 API Platform 的 Filter 逻辑 使用。

例如:

- /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

您需要安装 API Platform 的 Filter 逻辑 或类似的包或您自己的过滤器以实现此功能,例如。

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

您还需要为 API Platform 启用搜索过滤器。

例如,在实体定义中

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

输出

输出包括数据以及查询中的 recordsTotalrecordsFiltered(总是相同的)以及 draw 参数。

测试

此包是为 php 7.4 开发的,并已测试与 php 7.2 到 8.1 的兼容性。

composer test

变更日志

有关最近更改的更多信息,请参阅 变更日志

贡献

有关详细信息,请参阅 贡献指南

安全漏洞

有关如何报告安全漏洞,请参阅我们的 安全策略

鸣谢

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件