softivo/data-grid-bundle

一个基于给定数据提供者的Symfony第三方扩展包,允许生成分页、可排序和可筛选的表格和列表

安装次数: 32

依赖者: 0

建议者: 0

安全性: 0

星标: 0

分支: 0

类型:symfony-bundle

v2.0.2 2023-11-18 00:18 UTC

This package is not auto-updated.

Last update: 2024-09-26 16:51:57 UTC


README

关于项目

一个基于给定数据提供者的Symfony第三方扩展包,允许生成分页、可排序和可筛选的表格和列表。

入门指南

要求

此包是为 Symfony框架 设计的。要使用该包,请遵循以下要求

  • php ^8.1
  • Symfony ^5.2 | ^6.0
  • Twig
  • jQuery
  • Webpack Encore & Symfony Stimulus Bridge

安装

  1. 安装包
    composer require softivo/datagridbundle
    
  2. 安装资产。

    添加包并安装依赖

    该包使用 @symfony/stimulus-bridge 来管理资产。通过您首选的包管理器(如npm或yarn),简单地安装所需的资产。以下使用yarn的示例安装

     yarn add link:./vendor/softivo/data-grid-bundle/src/Resources/assets
    

    配置Stimulus控制器

    将之前安装的 @softivo/data-grid-bundle 控制器添加到您的应用程序中。第三方控制器应放在 controllers.json 文件中。默认情况下,此文件位于 assets/controllers.json。示例配置可能如下所示

     {
       "controllers": {
         "@softivo/data-grid-bundle": {
           "main": {
             "fetch": "eager",
             "enabled": true,
             "autoimport": {
               "@softivo/data-grid/dist/style.min.css": true
             }
           }
         }
       }
     }
    

    构建您的资产

    如果您正在使用Webpack Encore构建项目,请在此时刻进行构建。

    // build dev environment 
    encore dev
       
    //build production environment
     encore production --progress
    
  3. 确保新包已添加到您的Symfony应用程序中。如果没有,请自行在 ./config/bunldes.php 文件中添加。
    Softivo\DataGridBundle\DataGridBundle::class => ['all' => true]
    

示例用法

数据转换器

首先,您需要创建一个实现 Softivo\DataGridBundle\Interface\DataTransformerInterface 接口的数据转换器。建议的方法是将转换器创建为symfony服务,以便轻松注入您的依赖。转换器负责将给定的数据转换为视图形式。

class SampleTransformer implements Softivo\DataGridBundle\Interface\DataTransformerInterface
{
    // ...

    /**
     * Use column attributes to define their settings, follow by hints
     * Regarding strings values, in most cases value can be the name of callable method of the class 
     * #[Softivo\DataGridBundle\Column\TextColumn()] 
    */
    public $column;

    /**
     * Use the magic function __invoke() to transform source data, into desire view form
     * @param Type $object
     */
    public function __invoke($object): void
    {
        // ...
        
        $this->column = $object->getSampleValue();
    }

{

创建数据网格

接下来,您应将转换器注入到您的控制器操作中。使用 Softivo\DataGridBundle\Factory\DataGridProviderInterface symfony服务创建尽可能多的网格。

class SampleController extends AbstractController 
{

    use App\SampleTransformer;
    use Softivo\DataGridBundle\Factory\DataGridProviderInterface;
    use \Symfony\Component\HttpFoundation\Request;

    // ...
    
    public function action(SampleProvider $sampleProvider, DataGridProviderInterface $provider, Request $request)
    {
        /**
        * Configure data grid passing unique ID, your data transformer and data source.
        * The source may be QueryBuilder instance, or simply array with data.
        */
        $dataGrid = $provider->createDataGrid('uniqueId', $sampleProvider, $query)
            // ...
            // change available settings for this data grid instance only.
            // Make sure handleRequest is called the last one.
            ->handleRequest($request);

        // Pass DataProvider into your template
        return $this->render('template.html.twig', [
            'dataGrid' => $dataGrid
        ]);  
    }

{

渲染视图

最后一步,使用创建的数据网格,您可以将数据渲染为HTML表格,或根据需要作为列表。渲染HTML表格的基本功能是 data_grid_table

{{ data_grid_table(
dataGrid,
// custom array of html table attributes
) }}

data_grid_list 函数允许为主容器和内部所有项目传递HTML属性。

{{ data_grid_list(
dataGrid,
'path/to/template.html.twig'
// custom array of html container attributes
// custom array of html rows attributes
) }}

您还可以使用twig函数自行渲染每个组件,类似于symfony表单。

{{ data_grid_start(dataGrid) }}
...
{{ data_grid_summary(dataGrid) }}
...
{{ data_grid_pagination(dataGrid) }}
...
{{ data_grid_table(dataGrid) }} OR {{ data_grid_list(dataGrid) }}
...
{{ data_grid_end(dataGrid) }}

文档

更多详细文档可以在 https://softivo.gitlab.io/datagridbundle 找到

许可

在MIT许可下分发。

联系

Softivo kontakt@softivo.pl

GitLab https://gitlab.com/softivo/datagridbundle.git