PHP Classes

Universal PHP Data Grid: Display and process data to edit database records

Recommend this page to a friend!
  Info   Example   Screenshots   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStar 39%Total: 506 All time: 5,786 This week: 39Up
Version License PHP version Categories
gd 2.0.3MIT/X Consortium ...7HTML, Databases, Libraries, PHP 7
Description 

Author

This package can display and process data to edit database records.

It provides several classes that can display data retrieved from database table records in a table grid and shows forms that can be used to edit the data in the view grid.

The package can be integrated in applications that use the frameworks Simfony, Laravel, Zend, Yii2 and others.

The package can be extend using plugins.

Innovation Award
PHP Programming Innovation award nominee
February 2019
Number 5
Data grids are way to display and edit data retrieved from databases in a grid that shows data in tables.

This package can display and process data in grids in a way that can work together modern PHP frameworks such as Simfony, Laravel, Zend, and Yii2.

Manuel Lemos
Picture of Ar Gabid
  Performance   Level  
Innovation award
Innovation award
Nominee: 1x

 

Example

<?php
/**
 * @author GD Lab <dev.gdgrid@gmail.com>
 */

require_once 'init.php';

use
Seytar\Routing\Router;

use
Illuminate\Http\Request;

Router::bootstrap(function($ex)
{
   
header('Content-Type: text/html; charset=utf-8');
    echo
'404 - Page Not Found';
});

Route::get('/', function()
{
    include
'users.php';
});

Route::get('/delete/{id}', function($id)
{
    if (
$user = User::find($id))

       
$user->delete();

   
header('Location:/');

    exit;
});

Route::get('/create', function()
{
   
define('ROUTE', 'create');

   
$provider = new User;

    include
'form.php';
});

Route::get('/update/{id}', function($id)
{
   
define('ROUTE', 'update');

   
$provider = User::find($id);

    include
'form.php';
});

Route::post('/create', function()
{
   
define('ROUTE', 'create');

   
$provider = new User;

   
$request = Request::capture();

   
$validator = (new ValidatorFactory())->make($request->all(), $provider->rules());

   
$provider->loadData($request->all());

    if (
$validator->fails())
    {
       
$provider->setErrors($validator->messages()->toArray());
    }
    else
    {
       
$provider->save();

       
header('Location:/');

        exit;
    }

    include
'form.php';
});

Route::post('/update/{id}', function($id)
{
   
define('ROUTE', 'update');

   
$provider = User::find($id);

   
$request = Request::capture();

   
$validator = (new ValidatorFactory())->make($request->all(), $provider->rules());

   
$provider->loadData($request->all());

    if (
$validator->fails())
    {
       
$provider->setErrors($validator->messages()->toArray());
    }
    else
    {
       
$provider->save();

       
header('Location:/');

        exit;
    }

    include
'form.php';
});


Details

Grid-Data

The PHP 7 Grid-Data Library.

The main purpose of the Library is to automatically generate tables, forms and representations of certain entities in the views. If the form and column field settings of the entity are not specified, then these settings are taken from the column types and their names in the database table.

For all this, you need to implement a specific interface in the entity itself, or connect a separate class that implements the interface itself and pass it to the generator.

Install


If you want to run the test application (included in the "testapp" folder of the Library), therefor you will 
have to install all necessary dependencies from composer.json:

{ "require": {

"gdgrid/gd": "dev-master"

}, "require-dev": {

"illuminate/database": "5.7",
"illuminate/filesystem": "5.7",
"illuminate/translation": "5.7",
"illuminate/validation": "5.7",
"symfony/var-dumper": "^4.1",
"seytar/php-router": "dev-master"

}, "autoload": {

"classmap": [
  "testapp/models/"
]

} }


# Usage example

1. Using Entity itself.

``Your model Class:``

```php
<?php

use Illuminate\Database\Eloquent\Model as Eloquent;

use gdgrid\gd\IGridFormProvider;

use gdgrid\gd\IGridTableProvider;

class User extends Eloquent implements IGridFormProvider, IGridTableProvider
{
    protected $fillable = ['name', 'image', 'email', 'gender', 'character'];

    public $timestamps = false;

    protected $errors = [];
    
    ... 
    
    public function gridFields(): array
    {
        return [
            'name' => 'Hero',
            'email' => 'Email',
            'image' => 'Photo',
            'character' => 'Description',
            'gender' => 'Gender',
        ];
    }
    
    public function gridInputTypes(): array
    {
        return [
            'name' => 'text',
            'email' => 'email',
            'image' => 'text',
            'character' => 'textarea',
            'gender' => 'radio',
        ];
    }
    
    public function gridInputOptions(): array
    {
        return [
            'gender' => ['Female', 'Male'],
        ];
    }
    
    public function gridInputSizes(): array
    {
        return [
            'name' => 100,
            'email' => 100,
            'image' => 255,
            'character' => 1000,
        ];
    }
    
    public function gridSafeFields(): array
    {
        return ['id'];
    }
    
    public function gridInputErrors(): array
    {
        return $this->errors;
    }

    public function gridTableCellPrompts()
    {
        return '(no data)';
    }

    public function gridInputPrompts(): array
    {
        return [];
    }
}

```

``View File:``

```php
<?php

use gdgrid\gd\GridTable;
use gdgrid\gd\GridForm;

$provider = new User;

$items = $provider->filter(Request::capture()->all())->get()->all();

$table = (new GridTable($provider))->loadColumns();

$table->plugin()->setConfig('bulk-actions', ['view' => false, 'set_query' => false]);

$table->plugin()->hook('filter', function(GridForm $plugin)
{
    $plugin->loadInputs()->setValues(Request::capture()->all());
});

$table->disableEmbedPlugin('pagination');

$table->setProviderItems($items)->setCell('image', function($data)
{
    return $data->image ? '<img src="' . $data->image . '" />' : null;
});

echo $table->render();

```

2. Using Data Provider.

``In this case it is not neccessary to implement interfaces in your entity class.``

``Your model Class:``

```php
<?php

use Illuminate\Database\Eloquent\Model as Eloquent;

class User extends Eloquent
{
    protected $fillable = ['name', 'image', 'email', 'gender', 'character'];

    public $timestamps = false;

    protected $errors = [];
    
    ... 
}

```

``View File:``

```php
<?php

use gdgrid\gd\bundle\Grid as BundleGrid;
use gdgrid\gd\Grid;
use gdgrid\gd\GridData;
use gdgrid\gd\GridDataProvider;
use gdgrid\gd\GridForm;
use gdgrid\gd\GridTable;
use Illuminate\Http\Request;

$provider = new User;

# The "isStoreOutdated" method checks if the current dataProvider`s instance is outdated in the BundleGrid`s cache:

$items = BundleGrid::capture()->isStoreOutdated('someStoreKey') 
    
    ? $provider->filter(Request::capture()->all())->get()->all() : [];

$dataProvider = new GridDataProvider($provider);

$dataProvider->setDataProvider((new GridData)
    ->setPdo(DB::capsule()->getConnection()->getPdo())
    ->setTable('users')
    ->setLocale('en'));

$dataProvider->fetchData();

$dataProvider->mergeData([
    'safeFields'   => [
        'id',
    ],
    'inputOptions' => [
        'gender' => ['Female', 'Male']
    ]
]);

$table = (new GridTable($dataProvider))->loadColumns();

if (sizeof($items)) $table->setProviderItems($items);

# Use of the Bundle Grid simplifies all initializations produced above in a single line:
//    $table = BundleGrid::capture() # method "capture" for create/access the GridBundle`s singleton.
//          ->store('someStoreKey') # method "store" (optional) for serialization/access the current GridBundle instance.
//          ->setProvider($provider)
//          ->fetchData(DB::capsule()->getConnection()->getPdo(), 'users')
//          ->mergeData([
//              'inputOptions' => [
//                  'gender' => ['FEMALE', 'MALE']
//              ]
//          ])->table();

# Serialize changes in the current BundleGrid`s instance
# (The methods "store/restore" brings ability for further access the dataProvider`s instance from BundleGrid`s cache):
//    if (BundleGrid::capture()->isStoreOutdated('someStoreKey')) BundleGrid::capture()->restore('someStoreKey', 3600);

$table->plugin()->setConfig('bulk-actions', ['view' => false, 'set_query' => false]);

$table->plugin()->hook('filter', function(GridForm $plugin, Grid $grid)
{
    $plugin->loadInputs()->setValues(Request::capture()->all());
});

# Can Disable the Embedded Plugins:
//    $table->disableEmbedPlugins();

# Pagination disabled. To enable it, you must specify quantity of records
# in the "totalCount" configuration parameter:
//    $table->plugin()->setConfig('pagination', ['totalCount' => ???]);

$table->disableEmbedPlugin('pagination');

# Can Format the values in the data table cells:
//    $table->setFormatAll(['truncate' => 5]);
//    $table->formatter()->mergeFormats([['strtoupper', []]]);
//    $table->setFormat([
//        [['name', 'email'], ['trim', 'strip_tags']],
//        ['character', ['strip_html']],
//    ]);

$table->setCell('image', function($data)
{
    return $data->image ? '<img src="' . $data->image . '" />' : null;
});

echo $table->render();

```

The full code of the represented examples you can find in the "testapp" directory of the Library.
Just copy/paste files to the document root of your application.

Screenshots (1)  
  • GridTable
  Files folder image Files (55)  
File Role Description
Files folder imagesrc (12 files, 5 directories)
Files folder imagetestapp (6 files, 3 directories)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 98%
Total:506
This week:0
All time:5,786
This week:39Up
User Ratings User Comments (1)
 All time
Utility:58%StarStarStar
Consistency:66%StarStarStarStar
Documentation:-
Examples:41%StarStarStar
Tests:-
Videos:-
Overall:39%StarStar
Rank:3866
 
You cn't install this package errors in .
6 years ago (adriano ghezzi)
0%Star