spyrit/doctrine-datagrid-bundle

Symfony Datagrid Bundle for Doctrine.

4.0.11 2023-11-22 09:48 UTC

README

这个包可以帮助您快速、轻松地创建和管理简单到复杂的表格。

与其他在GitHub和/或Packagist上可用的类似包不同,此包没有魔法方法可以直接在视图中渲染表格。这种技术选择允许您完全自定义表格的外观和渲染(筛选字段、按钮、列、每列显示的数据、分页链接和信息等)。

这使得它在后端和前端应用程序中都易于实现和使用。

还有疑虑?让我们看看它是如何工作的!

安装

获取代码

要自动安装此包,请使用Composer

composer req spyrit/doctrine-datagrid-bundle

使用它

创建您的表格

<?php

namespace App\Datagrid;

use App\Entity\Video;
use Spyrit\Bundle\DoctrineDatagridBundle\Datagrid\DoctrineDatagrid;
use Spyrit\Bundle\DoctrineDatagridBundle\Datagrid\DoctrineDatagridFactory;
use Symfony\Component\Form\Extension\Core\Type\TextType;

class VideoDatagrid
{
    private DoctrineDatagrid $datagrid;

    public function __construct(DoctrineDatagridFactory $factory)
    {
        $this->datagrid = $factory
            ->create('video', ['multi_sort' => false])
            ->select(['video'])
            ->id('video.id')
            ->query(function ($qb) {
                return $qb->from(Video::class, 'video');
            })
            ->filter('title', TextType::class, [
                'attr' => [
                    'placeholder' => 'Title',
                ],
                'translation_domain' => false,
                'required' => false,
            ], function ($value, $qb) {
                return $qb->andWhere('video.title LIKE :title')
                    ->setParameter('title', '%'.$value.'%');
            })
            ->setDefaultSort(['video.id' => 'desc'])
            ->setDefaultMaxPerPage(30);
    }

    public function execute(): DoctrineDatagrid
    {
        return $this->datagrid->execute();
    }
}

在控制器中使用您的表格

<?php

namespace App\Controller\Admin;

use App\Datagrid\VideoDatagrid;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
* @Route("/video")
*/
class VideoController extends Controller
{
    /**
     * @Route("/list", name="video_index", methods={"GET","POST"})
     */
    public function index(VideoDatagrid $datagrid): Response
    {
        return $this->render('video/index.html.twig', [
            'datagrid' => $datagrid->execute(),
        ]);
    }
}

在视图中渲染它们

{% extends 'layout.html.twig' %}
{% import '@DoctrineDatagrid/Datagrid/macros.html.twig' as macros %}

{% set form = datagrid.filterFormView %}
{% set route = app.request.attributes.get('_route') %}

{% block body %}
    <table class="table">
        <thead>
            <tr id="filters">
                {{ form_start(form) }}
                <td>
                    {{ form_widget(form.title) }}
                </td>
                <td class="text-right">
                    <button class="btn btn-primary">{{ 'Filter' }}</button>
                    <a href="{{ datagrid.resetPath(route) }}" class="btn btn-secondary">{{ 'Reset' }}</a>
                </td>
                {{ form_end(form) }}
            </tr>
            <tr>
                <th>{{ 'Title' }}</th>
                <th>{{ 'Description' }}</th>
            </tr>
        </thead>
        <tbody>
            {% for video in datagrid.results %}
                <tr>
                    <td>{{ video.title }}</td>
                    <td>{{ video.description }}</td>
                </tr>
            {% else %}
                <tr>
                    <td colspan="2">{{ 'No video' }}</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
    <div class="row">
        <div class="col-md-4"></div>
        <div class="col-md-4">
            {% include '@DoctrineDatagrid/Datagrid/pagination_info.html.twig' %}
        </div>
        <div class="col-md-4">
            {% include '@DoctrineDatagrid/Datagrid/pagination.html.twig' %}
        </div>
    </div>
{% endblock %}

致谢

我们特别感谢...

  • Charles SANQUER,其从LightCSV库的分支Colibri CSV用于导出功能。
  • Subosito,其独立的Inflector 被转换为一个服务,以满足我们的需求。