spyrit/doctrine-datagrid-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

spyrit/doctrine-datagrid-bundle

最新稳定版本:4.0.11

Composer 安装命令:

composer require spyrit/doctrine-datagrid-bundle

包简介

Symfony Datagrid Bundle for Doctrine.

README 文档

README

This bundle helps you to create and manage simple to complex datagrids quickly and easily.

Unlike other similar bundle already available on Github and/or Packagist, there is no magic method that will render the datagrid in you view. This technical choice allow you to completely customize your datagrid aspect and render (filter fields, buttons, columns, data displayed in each column, pagination links and informations, etc.).

This make it easy to implement and use in both back-end and front-end applications.

Still skeptical? Let's see how it works!

Installation

Get the code

To install this bundle automatically, use Composer:

composer req spyrit/doctrine-datagrid-bundle

Use it

Create your datagrids:

<?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();
    }
}

Use your datagrids in your controllers:

<?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(),
        ]);
    }
}

Render them in your views:

{% 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 %}

Credit

Our special thanks go to ...

  • Charles SANQUER for its fork of the LightCSV library : Colibri CSV used in the export feature.
  • Subosito for its standalone Inflector class transformed in a service for our needs.

统计信息

  • 总下载量: 20.97k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 7
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-06-30