Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

DataGrid

Felice Ostuni edited this page Nov 18, 2016 · 14 revisions

DataGrid extends DataSet in order to make data-grid output with few lines of fluent code.
It builds a bootstrap striped table, with pagination at bottom and order-by links on table header. It also supports blade syntax inline, filters, closures... lot of funny things

in a controller:

   $grid = \DataGrid::source(Article::with('author'));  //same source types of DataSet
   
   $grid->add('title','Title', true); //field name, label, sortable
   $grid->add('author.firstname','Body'); //relation.fieldname 
   $grid->add('{{ substr($body,0,20) }}...','Body'); //blade syntax with main field
   $grid->add('{{ $author->firstname }}','Author'); //blade syntax with related field
   $grid->edit('/dataedit/uri', 'Edit','modify|delete'); //shortcut to link DataEdit
   $grid->orderBy('article_id','desc'); //default orderby
   $grid->paginate(10); //pagination

   view('articles', compact('grid'))

in a view you can just write:

  #articles.blade.php
  {!! $grid !!}

datagrid support "filters" and closures, so to format cell values you can use:

...
$grid->add('body', 'Body')->filter('strip_tags|substr[0,20]');  //filters
$grid->add('body|strip_tags|substr[0,20]','Body');  //inline filters
$grid->add('ucfirst(substr( {{ $body }}, 0,100))', 'Body');  //inline blade

//and closure on value
$grid->add('body', 'Body')->cell( function ($value) {
            return substr(strip_tags($value), 0, 20);
 });

//you can also access to the entire row dataset
$grid->add('revision', 'Article status')->cell( function ($value, $row) {
	if ($row->is_visible) {
		return '<u>'.link_to($row->full_url, "published", ['target'=>'_blank']).'</u>';
	}
	if ($row->pending_approval) {
		return 'pending approval';
	}
	..
});

...

styling a datagrid using style and attributes:

   ...
   $grid->add('title','Title', true)->style("width:100px"); 
   $grid->add('body','Body')->attributes(array("class"=>"custom_column"));
   ...
    //row and cell manipulation via closure
    $grid->row(function ($row) {
       if ($row->cell('public')->value < 1) {
           $row->cell('title')->style("color:Gray");
           $row->style("background-color:#CCFF66");
       }  
    });
    ...

datagrid also supports csv output, so datagrid can be used as "report" tool.

   ...
   $grid->add('title','Title');
   $grid->add('body','Body')
   ...
   $grid->buildCSV();  //  force download 
   $grid->buildCSV('export_articles', 'Y-m-d.His');  // force download with custom name
   $grid->buildCSV('uploads/filename', 'Y-m-d');  // write on file 
    ...
Clone this wiki locally