PHP code example of ramondev / html-table-builder

1. Go to this page and download the library: Download ramondev/html-table-builder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ramondev / html-table-builder example snippets


use Ramondev\HtmlTableBuilder\HtmlTableBuilder;

$table = new HtmlTableBuilder($users); // array or Eloquent Collection

return $table->render();

// In a controller
public function index()
{
    $table = new HtmlTableBuilder(User::all());

    return view('users.index', ['table' => $table]);
}

new HtmlTableBuilder(array|object $data)

$table->setColumns(['id', ['name', 'Full Name'], 'email']);

$table->appendColumn([['actions', 'Actions']]);

$table->removeColumns(['password', 'remember_token']);

$table->tableHeadFormat(fn (string $label) => strtoupper($label));

$table->columnFormat('created_at', fn ($row, $i) => $row->created_at->format('M j, Y'));

$table->columnFormat('status', fn ($row) => $row->is_active ? 'Active' : 'Inactive');

$table->each(function (HtmlTableBuilder $table, int $index, array $row) {
    if ($row['is_flagged']) {
        $table->setTableRowAttr(['class' => 'bg-red-50'], $index);
    }
});

$table->escapeOutput(false);                          // disable escaping for ALL columns
$table->escapeOutput(false, 'status');                // disable escaping for just the "status" column
$table->escapeOutput(false, ['status', 'actions']);   // disable escaping for several columns in one call

$table->setTableAttr(['class' => 'table table-striped', 'id' => 'users-table']);
$table->setTableHeadAttr(['class' => 'bg-gray-100']);
$table->setTableBodyAttr(['class' => 'divide-y']);

$table->setTableRowAttr(['class' => 'border-b']);          // every row
$table->setTableRowAttr(['class' => 'bg-yellow-50'], 3);   // just row index 3

$table->setTableDataAttr(['class' => 'px-4 py-2']);             // every cell
$table->setTableDataAttr(['class' => 'font-bold'], 0, 'name');  // row 0, "name" column only

$table->paginate(15);

$table->setPaginationPageName('users_page'); // links become ?users_page=2

$table->setPaginationLabels('← Prev', 'Next →');

$table->setPaginationWindow(1); // tighter: 1 ... 5 [6] 7 ... 12

$table->setPaginationNavAttr(['class' => 'mt-4']);
$table->setPaginationListAttr(['class' => 'flex gap-2']);
$table->setPaginationLinkAttr(['class' => 'pagination-link']);

$table->hidePaginationControls();

$meta = $table->getPaginationMeta();
// ['enabled' => true, 'currentPage' => 2, 'lastPage' => 5, 'total' => 68, 'pages' => [1, '...', 2, 3, 4, '...', 5], ...]

$table->emptyState('No users found.');

$table->emptyState('No users found.', ['class' => 'empty-row']);

$table->emptyState('<strong>No users</strong> match your filters.', [], escape: false);

use Ramondev\HtmlTableBuilder\HtmlTableBuilder;

$table = new HtmlTableBuilder($users);

$table->setColumns(['id', ['name', 'Full Name'], 'email', ['status', 'Status']]);

$table->columnFormat('status', fn ($row) =>
    $row->is_active
        ? '<span class="badge badge-success">Active</span>'
        : '<span class="badge badge-muted">Inactive</span>'
);
$table->escapeOutput(false, 'status');

$table->setTableAttr(['class' => 'table table-striped w-full']);
$table->setTableHeadAttr(['class' => 'bg-gray-100 text-left']);
$table->setTableRowAttr(['class' => 'border-b hover:bg-gray-50']);

$table->paginate(20);
$table->setPaginationLabels('← Previous', 'Next →');
$table->setPaginationWindow(2);

$table->emptyState('No users match your filters.', ['class' => 'py-8 text-center']);

return $table->render();
blade
{{-- users/index.blade.php --}}
<div class="users-table">
    {!! $table->render() !!}
</div>