sygmaa/grids

此包已被弃用,不再维护。未建议替代包。

laravel 5 的 Datagrid 小部件。它可以从您的实体中轻松生成数据表。

dev-master 2016-04-15 13:59 UTC

This package is not auto-updated.

Last update: 2021-12-06 13:29:20 UTC


README

Grids 是 laravel 5 的数据网格小部件。它可以从您的实体中轻松生成数据表。生成的 HTML 使用 Twitter Bootstrap,但您可以覆盖模板。

即将推出

  • 更多示例的演示
  • CSV/Excel 导出
  • 扩展插件并添加自定义字段的可能性
  • 字段样式
  • 等等...

安装

首先,在您的 composer.json 中添加以下内容

  "require": {
    "sygmaa/grids": "dev-master"
  }

然后,在您的 config/app.php 中添加新的服务提供者

'Sygmaa\Grids\GridsServiceProvider',

并且对于外观

'Grids' => 'Sygmaa\Grids\GridsFacade',

之后,如果您想覆盖视图或语言,可以运行此命令

php artisan vendor:publish

然后,您必须在模板中添加以下行

    {!! Grids::head() !!}
    {!! Grids::styles() !!}
    {!! Grids::scripts() !!}

简单示例

在您的控制器中

$grid = Grids::make(new Model())
    // Pagination; 15 is the number of results to show by page
    ->paginate(15) 
    // Show a reset button to clear filters (In the filter form)
    ->reset() 
    // You can add a condition...
    ->where('field', '<', 'content') 
    // ...Many conditions
    ->where('field', 'content') 
    // Where In SQL Statement is supported
    ->whereIn('field', ['content1', 'content2']) 
    // Order by SQL Statement is supported
    ->orderBy('field', 'ASC') 
    // Add a new field (name, Label)
    ->addField(Grids::text('id', 'ID')
        // Define the primary key (needed)
        ->setPrimary() 
        // The field will be hidden
        ->setVisible(false) 
    )
    // You can create a custom field with a callback
    ->addField(Grids::custom('custom', 'custom', function($row){ 
      // You can access to the data of the actual row
      return $row->name; 
    })
    ->addField(Grids::text('name', 'Name')
        // An order by on this field will be available
        ->setSortable()
        // We can search keywords in an input
        ->setFilterable()
    )
    // Eloquent OneToMany/OneToOne relation
    // entities.name -> "entities" is your association method in your model ( entities() )
    // "name" is th name of the field in the associated model
    ->addField(Grids::oneRelation('entities.name', 'Label of entity', 'App\Models\EntityName')
         // You can filter the relation field
        ->setFilterable() 
    )
    // Eloquent ManyToMany relation
    // entity.name -> "entity" is your association method in your model ( entity() ), like oneRelation 
    // "name" is th name of the field in the associated model, like oneRelation
    // The result is a list (separated by comas)
    ->addField(Grids::manyRelation('entity.name', 'Label of entity', 'App\Models\EntityName') 
        ->setFilterable()
    )
    // Date field
    ->addField(Grids::date('updated_at', 'Dernière modification', 'd/m/Y H:i:s')
        ->setSortable()
        ->setFilterable() // You will have a bootstrap calendar to select period
    )
    // Add a mass action (For example : to delete entries)
    ->addAction(Grids::massAction("Delete", URL::route('model.delete')))
    // Add a single action at the end of the row : you can for example return a button
    ->addAction(Grids::action("Edit", function($label, $row){
        $url = URL::route('model.edit', ['id' => $row->id]);
        return '<a class="btn btn-primary" href="'. $url .'">'. $label .'</a>';
    })); // Add 

和在模板中

<div class="panel-body">
    <!-- Show the filter form -->
    {!! $grid->renderFilters() !!}
</div>
<div class="panel-body pn">
    <div id="datatable_wrapper" class="dataTables_wrapper form-inline dt-bootstrap no-footer">
        <!-- Show the grid -->
        {!! $grid->renderTable() !!}

        <div class="dt-panelfooter clearfix">
            <div class="dataTables_info" id="datatable_info" role="status" aria-live="polite">
                <!-- Show informations about pagination -->
                {{ $grid->renderPaginationInfos() }}
            </div>
            <div class="dataTables_paginate paging_simple_numbers" id="datatable_paginate">
                <!-- Show the pagination -->
                {!! $grid->renderPagination() !!}
            </div>
        </div>
    </div>
</div>

许可证

Grids 使用 MIT 许可证