PHP code example of ivanharmat / crewgrid

1. Go to this page and download the library: Download ivanharmat/crewgrid 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/ */

    

ivanharmat / crewgrid example snippets




namespace App\Livewire;

use App\Models\Order;
use CrewGrid\Columns\Column;
use CrewGrid\Grid;
use Illuminate\Contracts\Database\Eloquent\Builder;

class OrdersGrid extends Grid
{
    protected function query(): Builder
    {
        return Order::query()->with('customer');
    }

    protected function columns(): array
    {
        return [
            Column::make('Date', 'created_at')
                ->sortable()
                ->filterDateRange()
                ->format(fn ($value) => $value->format('M j, Y')),

            Column::make('Customer', 'customer_id')
                ->filterMultiSelect(fn () => Customer::pluck('name', 'id')->all())
                ->format(fn ($value, $row) => e($row->customer?->name)),

            Column::make('Reference', 'reference')
                ->sortable()
                ->searchable()
                ->filterText(),

            Column::make('Total', 'total')
                ->sortable()
                ->format(fn ($value) => '$'.number_format($value, 2)),

            Column::make('Actions', 'id')
                ->view('orders.grid-actions'), // gets $row and $value
        ];
    }
}

protected function loadPreferences(): array
{
    return auth()->user()->grid_preferences[$this->preferenceKey()] ?? [];
}

'remember_view' => true,              // config: on for every grid
public ?bool $rememberView = false;   // or off for one

public function mount(): void
{
    parent::mount();

    if ($this->sortField === '') {
        $this->sortField = 'last_updated';
        $this->sortDirection = 'desc';
    }
}

if (! $this->viewWasRestored() && empty($this->filters)) {
    $this->filters['status'] = ['Active' => true];
}

#[Url(as: 'show', except: '0')]
public string $estimateType = '0';

protected function rememberedProperties(): array
{
    return parent::rememberedProperties() + ['estimateType' => 'show'];
}

public function rowClass($row): string
{
    return $row->overdue ? 'row-overdue' : '';
}

public string $estimateType = '0';

protected function toolbarView(): ?string
{
    return 'estimates.partials.estimate-type';
}

'pagination_position' => 'bottom',   // config: "bottom", "top" or "both"
public ?string $paginationPosition = 'both';   // or per grid

public string $loadMode = 'pager';    // classic paginator (default)
public string $loadMode = 'infinite'; // "Load More" appends rows

// config/crewgrid.php
'theme' => 'bootstrap3',

// config/crewgrid.php
'classes' => [
    'bootstrap5' => ['button' => 'btn btn-primary btn-sm'],
],

public array $classes = ['button' => 'btn btn-danger btn-sm'];

$this->uiClass('action.danger');   // the theme's danger action button
$this->uiClass('action.warning');  // not shipped -> plain 'action', until you define it

'classes' => [
    'tailwind' => [
        'action.warning' => 'inline-flex items-center rounded bg-amber-500 px-2 py-0.5 text-xs text-white',
    ],
],

Column::make('Links', 'id')
    ->html()
    ->format(fn ($value, $row) => $this->actionLink('Estimate', url('estimates/'.$row->estimate_id), 'info'));

// config/crewgrid.php
'icons' => [
    'filter' => '<i class="bi bi-funnel"></i>',                 // Bootstrap Icons
    'sort_asc' => '<svg class="h-3 w-3" viewBox="0 0 20 20">…</svg>',  // inline SVG
    'loading' => '',                                            // render nothing
],

Column::make('Date', 'timestamp')->icon('<i class="fa fa-calendar"></i>');

$this->actionLink('Estimate', $url, 'info', '<i class="fa fa-file-o"></i>');

// config/crewgrid.php
'classes' => [
    'tailwind' => [
        'button' => 'inline-flex items-center gap-1 rounded-md border border-gray-300 bg-white px-2.5 py-1 text-sm',
        'action.info' => 'inline-flex items-center rounded bg-purple-600 px-2 py-0.5 text-xs text-white',
    ],
],

'export' => true,        // the button on every grid; a grid can override via public ?bool $exportable
'export_chunk' => 500,   // rows read per query while streaming

protected function exportFilename(): string
{
    return 'orders-'.date('Y-m-d').'.xlsx';
}
bash
php artisan vendor:publish --tag=crewgrid-config
php artisan vendor:publish --tag=crewgrid-views
bash
php artisan vendor:publish --tag=crewgrid-views
js
// tailwind.config.js
content: [
    './vendor/ivanharmat/crewgrid/resources/views/**/*.blade.php',
    './vendor/ivanharmat/crewgrid/src/**/*.php',
    // ...
],