定制 vysokeskoly/data-grid-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

vysokeskoly/data-grid-bundle

Composer 安装命令:

composer require vysokeskoly/data-grid-bundle

包简介

Symfony DataGridBundle

README 文档

README

Latest Stable Version License PHP - Checks Coverage Status

This Symfony Bundle is a simple datagrid bundle. It aims to be easy to use and extensible.

Warning version 3

Version 3 is for symfony ~3.3 and ~4.0 and PHP 7, and only twig ~1.8

Version 3 is here. You can switch to branch 2.x (or tags 2.x) if you want to stay on legacy version.

There is no BC break in the usage between version 2 and version 3, but the version 3 is not compatible with symfony < 3.3.

Warning version 2

Version 2 is here. You can switch to branch 1.x (or tags 1.x) if you want to stay on legacy version. There are BC Breaks between version 1 and version 2.

Actual state

see CHANGELOG.md

Features

  • Display a Data Grid from a Doctrine 2 Query Builder
  • Automatic filter
  • Sorting on columns
  • Easy to configure
  • Easy to extend
  • Documented (in this readme for basics and in Resources/doc for advanced topics)
  • Paginator can be used as a standalone component
  • Change of DataGrid behaviour with events
  • Change of DataGrid presentation with twig embeds

System Requirement

  • jQuery has to be present on your pages
  • version 1.8+ of twig is mandatory (use of twig embeds)

Documentation

The documentation is in this README and in Resources/doc

Installation

You need to add the following lines in your deps :

Add KitpagesChainBundle in your composer.json

{
    "require": {
        "vysokeskoly/data-grid-bundle": "^5.0"
    }
}

Now tell composer to download the bundle by running the step:

$ php composer.phar update vysokeskoly/data-grid-bundle

AppKernel.php

$bundles = [
    ...
    new Kitpages\DataGridBundle\KitpagesDataGridBundle(),
];

Configuration in config.yml

These values are default values. You can skip the configuration if it is ok for you.

kitpages_data_grid:
    grid:
        default_twig: @KitpagesDataGrid/Grid/grid.html.twig
    paginator:
        default_twig: @KitpagesDataGrid/Paginator/paginator.html.twig
        item_count_in_page: 50
        visible_page_count_in_paginator: 5

Note you can use the followin configuration in order to user Bootstrap 3 :

kitpages_data_grid:
    grid:
        default_twig: @KitpagesDataGrid/Grid/bootstrap3-grid.html.twig
    paginator:
        default_twig: @KitpagesDataGrid/Paginator/bootstrap3-paginator.html.twig

Simple Usage example

In the controller

use Kitpages\DataGridBundle\Grid\GridConfig;
use Kitpages\DataGridBundle\Grid\Field;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ContactController
{
    public function productListAction(Request $request): Response
    {
        // create query builder
        $repository = $this->getDoctrine()->getRepository('AcmeStoreBundle:Product');
        $queryBuilder = $repository->createQueryBuilder('item')
            ->where('item.price > :price')
            ->setParameter('price', '19.90')
        ;

        $gridConfig = new GridConfig($queryBuilder, 'item.id');
        $gridConfig
            ->addField('item.id')
            ->addField('item.slug', ['filterable' => true])
            ->addField('item.updatedAt', [
                'sortable' => true,
                'formatValueCallback' => fn ($value) => $value->format('Y/m/d')
            ])
        ;

        $gridManager = $this->get('kitpages_data_grid.grid_manager');
        $grid = $gridManager->getGrid($gridConfig, $request);

        return $this->render('AppSiteBundle:Default:productList.html.twig', [
            'grid' => $grid
        ]);
    }
}

Twig associated

In your twig you just have to put this code to display the grid you configured.

{% embed kitpages_data_grid.grid.default_twig with {'grid': grid} %}
{% endembed %}

More advanced usage

In the controller

same controller than before

Twig associated

If you want to add a column on the right of the table, you can put this code in your twig.

{% embed kitpages_data_grid.grid.default_twig with {'grid': grid} %}

    {% block kit_grid_thead_column %}
        <th>Action</th>
    {% endblock %}

    {% block kit_grid_tbody_column %}
        <td><a href="{{ path ("my_route", {"id": item['item.id']}) }}">Edit</a></td>
    {% endblock %}

{% endembed %}

More advanced usage

In the controller

use Kitpages\DataGridBundle\Grid\GridConfig;
use Kitpages\DataGridBundle\Grid\Field;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class AdminController extends Controller
{
    public function listAction(Request $request, $state): Response
    {
        // create query builder
        $em = $this->get('doctrine')->getEntityManager();
        $queryBuilder = $em->createQueryBuilder()
            ->select('m, e, c')
            ->from('KitappMissionBundle:Mission', 'm')
            ->leftJoin('m.employee', 'e')
            ->leftJoin('m.client', 'c')
            ->where('m.state = :state')
            ->add('orderBy', 'm.updatedAt DESC')
            ->setParameter('state', $state)
        ;

        $gridConfig = new GridConfig($queryBuilder, 'm.id');
        $gridConfig
            ->addField('m.title', ['label' => 'title', 'filterable' => true])
            ->addField('m.country', ['filterable' => true])
            ->addField('c.corporation', ['filterable' => true])
            ->addField('e.lastname', ['filterable' => true])
            ->addField('e.email', ['filterable' => true])
        ;

        $gridManager = $this->get('kitpages_data_grid.grid_manager');
        $grid = $gridManager->getGrid($gridConfig, $request);

        return $this->render('KitappMissionBundle:Admin:list.html.twig', [
            'grid' => $grid
        ]);
    }
}

Twig associated

same Twig than before

Field "as"

For request like

$queryBuilder->select("item, item.id * 3 as foo");

You can display the foo field with

$gridConfig->addField("item.id");
$gridConfig->addField("foo");

Events

You can modify the way this bundle works by listening events and modify some objects injected in the $event.

see the event documentation in Resources/doc/30-Events.md

Tags

Tag system is used to get some fields by tags. When you create a field, you can define some tags associated to this field. After that, in the grid config, you can find the fields that match this tag.

// add tag as the third parameter of the field 
$gridConfig->addField("item.id", [], ['foo', 'bar']);
$gridConfig->addField("foo", [], ['myTag', 'foo']);

// get fieldList matching 'bar' tag. There is only one result.
$fieldList = $gridConfig->getFieldListByTag('bar');
$fieldList[0] // -> this is the first Field (which name is 'item.id')

Custom class for the $grid object

By default, the following line

$grid = $gridManager->getGrid($gridConfig, $request);

returns an object of the type Grid

You can use your own subclass of Grid. By default the GridManager creates the instance $grid of Grid but you can create the instance by yourself.

class CustomGrid extends Grid
{
	public $myParameter;
}
$myCustomGrid = new CustomGrid();
$grid = $gridManager->getGrid($gridConfig, $request,$myCustomGrid);

// now the $grid object is an instance of CustomGrid (it 
// is exactly the same object than $myCustomGrid, not cloned)

Reference guide

Add a field in the gridConfig

when you add a field, you can set these parameters :

$gridConfig->addField('slug', [
    'label' => 'Mon slug',
    'sortable' => false,
    'visible' => true,
    'filterable' => true,
    'translatable' => true,
    'formatValueCallback' => fn ($value) => strtoupper($value),
    'autoEscape' => true,
    'category' => null, // only used by you for checking this value in your events if you want to...
    'nullIfNotExists' => false, // for leftJoin, if value is not defined, this can return null instead of an exception
]);

TIP: Use FieldOption enum

$gridConfig->addField('slug', [
    FieldOption::Label->value => 'Mon slug',
    FieldOption::Sortable->value => false,
    FieldOption::Visible->value => true,
    FieldOption::Filterable->value => true,
    FieldOption::Translatable->value => true,
    FieldOption::FormatValueCallback->value => fn ($value) => strtoupper($value),
    FieldOption::AutoEscape->value => true,
    FieldOption::Category->value => null, // only used by you for checking this value in your events if you want to...
    FieldOption::NullIfNotExists->value => false, // for leftJoin, if value is not defined, this can return null instead of an exception
]);

What can you personalize in your twig template

With the embed system of twig 1.8 and more, you can override some parts of the default rendering (see example in the "More advanced usage" paragraph).

You can consult the base twig template here to see what you can personalize. https://github.com/kitpages/KitpagesDataGridBundle/blob/master/Resources/views/Grid/grid.html.twig

vysokeskoly/data-grid-bundle 适用场景与选型建议

vysokeskoly/data-grid-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.63k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 03 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「datagrid」 「paginator」 「filter」 「sort」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 vysokeskoly/data-grid-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 vysokeskoly/data-grid-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 vysokeskoly/data-grid-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 27
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-03-25