1. Go to this page and download the library: Download steelants/datatable 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/ */
steelants / datatable example snippets
namespace App\Livewire;
use App\Models\User;
use SteelAnts\DataTable\Livewire\DataTableComponent;
use Illuminate\Database\Eloquent\Builder;
use SteelAnts\DataTable\Traits\UseDatabase;
class UserTable extends DataTableComponent
{
Use UseDatabase;
// or UseDatabaseEloquent, if you want to receive model instead ov serialized array
// Get model query
public function query(): Builder
{
return User::query();
}
// Set headers
public function headers(): array
{
return [
'id' => 'ID',
'name' => 'Name',
'email' => 'E-mail',
];
}
// Set actions
public function actions($item) : array
{
return [
[
// livewire action
'type' => "livewire",
'action' => "remove",
'parameters' => $item['id'],
'text' => "Remove",
'actionClass' => 'text-danger',
'iconClass' => 'fas fa-trash',
'confirm' => 'Are you sure you want to delete this post?',
],
[
// url action
'type' => "url",
'url' => rounte('user.show', [id => $item['id']]),
'text' => "Show",
'iconClass' => 'fas fa-eye',
]
];
}
// Custom render of 'name' column
public function renderColumnName($value, $row){
return '<b>'.e($value).'</b>';
}
// Transform order column on raw order column (optional)
public function orderColumnName(){
return 'CAST(name AS STRING)';
}
// Livewire actions
public function remove($id){
User::find($id)->delete();
}
}
// headers
'comments.id' => 'Comments' // sorts by number of comments
'reactions.id' => 'Reactions' // sorts by number of reactions (morph-aware)
// sortBy
$sortBy = 'comments.id';
public function orderColumnName(): string
{
return 'LOWER(name)';
}
// Enable sorting
public bool $sortable = true;
// Enable pagination
public bool $paginated = true;
// Enable fulltext search
public bool $searchable = true;
public bool $searchableColumns = [];
//Enable filters
public bool $filterable = true;
// Define cast by header key
public function renderCasts(): array
{
return [
'is_active' => BoolAsIcon::class,
];
}
use SteelAnts\DataTable\RenderCasts\RenderCast;
class BoolAsIcon implements RenderCast
{
public function render($key, $value, $model)
{
return '<i class="' . ($value ? 'far fa-check-circle text-success' : 'far fa-times-circle text-danger') . '"></i>';
}
}