vkrtecek/table

Basic table builder.

v1.1.5 2019-01-23 09:44 UTC

This package is not auto-updated.

Last update: 2024-04-28 02:00:51 UTC


README

License

The library focuses on easy table rendering of arrays of objects. No connections to database, only insert array.

License

The Table library is open-sourced software licensed under the MIT license.

Instalation

composer require vkrtecek/table

Examples

Let's say we have for all examples testing class like below:

class TestObject
{
    private $id;
    private $name;
    private $age;
    private $birthdate;
    
    public function __construct($id, $name, $age, $birthdate = NULL) {
        $this->id = $id;
        $this->name = $name;
        $this->age = $age;
    }
    
    public function getId() { return $this->id; }
    public function getName() { return $this->name; }
    public function getAge() { return $this->age; }
    public function getBirthDate() { return $this->birthdate; }
}

Basic usage

If we want to render table with collection of TestObject, do:

$data = [
    new TestObject(1, 'John', 38),
    new TestObject(2, 'Susane', 35),
    new TestObject(3, 'Paul', 13),
    new TestObject(4, 'Joe', 25),
    new TestObject(5, 'Lucia', 80),
    new TestObject(6, 'Štěpán', 29, '1989-03-23'),
];

$table = \Vkrtecek\Table\Table::create($data)
   ->addColumn('ID of person')->setContent('id')
   ->addColumn('Name')->setContent('name')
   ->addColumn('Age')->setContent('age');
                

The string passed by setContent() must be the same string as signature of property's getter without get. So for example if the TestObject has method getAge(), setContent() must pass string 'age' or 'Age'.

And in your View call:

<style>
    <?= $table->renderCSS(); ?>
</style>
.
.
.
<body>
    <?= $table->renderHTML(); ?>
</body>

or

$table->renderHTML(['css' => true]);

so the result will look like

ID of personNameAge
1John38
2Susane35
3Paul13
4Joe25
5Lucia80
6Štěpán29

Advanced

Callbacks

We can specify a callback function instead of property name:

$table->addColumn('Name')->setContent(function (TestObject $obj) {
    return '<em class="red">' . $obj->getName() . '</em>';
});
ID of personNameAge
1John38
2Susane35
3Paul13
4John38
5Susane35
6Paul13

Sorting and filtering table data

Sometimes we need sort data by some attribute:

$table->addColumn('Name')->setOrderable()->setContent('name');

and now by clicking on the table column header, we can sort the rows by this column. Or setOrderable() pass one parameter of type callable to specify the style of sorting.

By typing code below the field for filtering of specific column data will appear:

$table->addColumn('Name')->setSearchable()->setContent('name');

If we don't want to show all rows and enable paging, which will render input for number of rows:

$table->enableListing();

Column class

Column can has it's own HTML class

$table->addColumn('ColName')->setClass('red');

Column filtering

Also for filtering by one single column, there is method setSoloSearchable() which pass string - URL attribute:

$table->addColumn('Name')->setSoloSearchable('url_name')->setContent('name');

will after any table action show URL like http://my_Server/?...url_name=<value> and similar for date column

$table->addColumn('Name')->setDateFromToSearchable('url_from', 'url_to')->setContent('birthade');

the URL will look like http://my_Server/?...url_from=<val_from>&url_to=<val_to> for filtering column by "between dates".

If one of these filters are set, the button for show/hide the navigation row will appear above the table. If the "SHOW" button is clicked, navigation row will appear as the second THEAD row.

Additional

If we want to sort and filter data by framework (e.g. QueryBuilder) and pass to table only filtered result set, call

$table = Table::create($rows)->setTotalItemCount(count($rows));

and table skip sorting, filtering and paging. By this integer value is also counted number of pages in listing, so insert the right number.

If is need to customize URL, use method setNavigationNames like below:

$table->setNavigationNames([
    'limit' => 'cust_limit',
    'orderBy' => 'cust_order_by',
    'order' => 'cust_order',
    'page' => 'cust_page',
    'pattern' => 'cust_pattern',
    'url' => 'my_Server'
])

will cause the URL will look after some table click action like http://my_Server/?cust_order_by=Name&cust_order=ASC&cust_limit=5&cust_page=1&cust_pattern=

Table is now multilingual and you can pass your own translations:

$table->renderHTML([
    'translations => [
        'Show navigation bar' => [string],  //button content
        'Hide navigation bar' => [string],  //button content
        'Search by' => [string],            //above table input's content
        'From' => [string],                 //in navigation row for dates
        'To' => [string],                   //in navigation row for dates
        'pattern' => [string],              //in navigation row for string searches
        'of' => [string],                   //in status bar (1 - 15 of 365)
    ]
]);