survos/api-grid-bundle

包含 DatatablesNet,使用 twig 和 stimulus,twig_component 需要 ^2.2

资助包维护!
kbond

安装数: 2,637

依赖项: 1

建议者: 1

安全: 0

星标: 2

关注者: 4

分支: 0

开放问题: 2

类型:symfony-bundle

1.5.340 2024-09-24 13:44 UTC

This package is auto-updated.

Last update: 2024-09-24 13:45:01 UTC


README

使用 DataTables.net JavaScript 库与 Symfony、Twig 和 API Platform。需要 survos/grid-bundle,它加载 datatables 库。

composer req survos/api-grid-bundle

想法

https://domohelp.domo.com/hc/en-us/articles/360043931814-Fun-Sample-DataSets https://www.mytechylife.com/2015/09/29/next-and-previous-row-with-jquery-datatables/ https://github.com/lerocha/chinook-database http://2016.padjo.org/tutorials/sqlite-data-starterpacks/#more-info-simplefolks-for-simple-sql

仅开发者...

composer config repositories.survos_grid_bundle '{"type": "vcs", "url": "git@github.com:survos/SurvosApiGridBundle.git"}'

设置

为了使用 api-grid,我们需要按照以下步骤操作

1) 在您想要 apiGrid 的实体的存储库类中添加 Survos\CoreBundle\Traits\QueryBuilderHelperTrait

2) 您需要设置列变量如下

        {% set columns = [
        'code',
        'description',
        {name: 'code', sortable: true},
        'description',
        {name: 'countrycode', sortable: true,  browsable: true, searchable: true},
        {name: 'privacyPolicy', browsable: true},
        {name: 'projectLocale', browsable: true},
        ] %}

默认情况下,sortable、browsable、searchable 都是 false。

对于您想要排序的列,添加 sortable: true

对于您想要添加搜索面板的列,添加 browsable: true

对于您想要添加搜索的列,添加 searchable: true

3) 对于搜索,需要包含以下内容

use Survos\ApiGrid\Api\Filter\MultiFieldSearchFilter;


#[ApiFilter(MultiFieldSearchFilter::class, properties: ['name', 'code'])]
#[ApiFilter(MultiFieldSearchFilter::class, properties: ['firstName', 'lastName', 'officialName'])]
#[ApiFilter(FacetsFieldSearchFilter::class, properties: ['gender', 'currentParty'])]

这里 name 和 code 是您需要搜索的列

4) 使用以下内容进行 doctrine searchpane 过滤器

首先,确保您已经通过将辅助特性质添加到存储库类中添加了必要的 counts 方法

class OfficialRepository extends ServiceEntityRepository implements QueryBuilderHelperInterface
{
    use QueryBuilderHelperTrait;

然后,将可过滤属性添加到实体类中。您可能想对它们进行索引以加快计数查询

use Survos\ApiGrid\Api\Filter\FacetsFieldSearchFilter;


#[ApiFilter(FacetsFieldSearchFilter::class, properties: ['gender','state'])]

5) 使用以下内容进行 doctrine order 过滤器

use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;


#[ApiFilter(OrderFilter::class, properties: ['id','objectCount','countryCode'])]
symfony new phpunit-bug --webapp && cd phpunit-bug
composer require --dev symfony/phpunit-bridge
vendor/bin/simple-phpunit

API 与 doctrine 的 bug

symfony new api-bug --webapp && cd api-bug
composer config extra.symfony.allow-contrib true
echo "DATABASE_URL=sqlite:///%kernel.project_dir%/var/data.db" > .env.local
composer require api
echo "title,string,80,yes," | sed "s/,/\n/g"  | bin/console -a make:entity Book
bin/console doctrine:schema:update --force --complete

symfony server:start -d
symfony open:local --path=/api

现在通过 post 添加一个 Book

curl -X 'POST' \
  'https://127.0.0.1:8000/api/books' \
  -H 'accept: application/ld+json' \
  -H 'Content-Type: application/ld+json' \
  -d '{
  "title": "Symfony Fast Track"
}'

头脑风暴:jstwig 组件

<twig:jstwig :data="data>
<section class="containers">
        <h3>
            {{ row.s|length }} Results
        </h3>

    {% for row in row %}
            <div>
                {{- 'Overlay ' ~ i -}}
            </div>
    {% endfor %}
</section>
</twig:jstwig>