Datagrid is PHP component to creating tables from a set of data.
Here: http://datagrid.sajgal.com.
The recommended way to install Datagrid is through Composer.
# Install Composer
curl -sS https://getcomposer.org/installer | php
Next, run the Composer command to install the latest stable version of Datagrid:
composer require roarbb/datagrid
After installing, you need to require Composer's autoloader:
require 'vendor/autoload.php';
Let's have some array of data:
$data = array(
'row1' => array(
'name' => 'Tyree',
'surname' => 'Schmidt',
'age' => '35',
'position' => 'Developer',
'pin' => '7366',
),
'row2' => array(
'name' => 'Cleve',
'surname' => 'Streich',
'age' => '38',
'position' => 'Management',
'pin' => '7290',
),
'row3' => array(
'name' => 'Mossie',
'surname' => 'Lesch',
'age' => '41',
'position' => 'Sales',
'pin' => '6521',
),
'row4' => array(
'name' => 'Kayla',
'surname' => 'Paucek',
'age' => '21',
'position' => 'Developer',
'pin' => '9478',
),
'row5' => array(
'name' => 'Elva',
'surname' => 'Haley',
'age' => '17',
'position' => 'Management',
'pin' => '4532',
),
);
Basic implementation of Datagrid:
$datagrid = new Datagrid();
$datagrid->setData($data);
Then in template file:
<h1>Datagrid</h1>
<?php echo $datagrid ?>
If you want to build table from valid JSON REST API response:
$datagrid->setDataUrl('http://datagrid.sajgal.com/?getJson');
If you are using Bootstrap you may need to set css class to Datagrid.
$datagrid->setTableClass('table');
You can set header row of Datagrid.
Count of array elements passed as argument should match count of items in each row element of $data
variable.
$datagrid->addHeader(['First name', 'Surname', 'Age', 'Position', 'PIN Code']);
If you want to enable sorting of Datagrid rows
$datagrid->isSortable();
If you want to hide some of columns with sensitive content
$datagrid->hideColumns(['pin']);
You can also enable pagination. You should pass rows per page integer as first argument.
$datagrid->setPagination(10);
If you want to add some buttons at the end of each row. You can use placeholders: {name}
will be translated to Tyree
etc.
$datagrid->addAction('Delete Row', 'http://datagrid.sajgal.com/delete/{name}');