PHP code example of zk / laravel-datagrid

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

    

zk / laravel-datagrid example snippets


use Zk\DataGrid\DataGrid;
use App\Models\Post;

class PostGrid extends DataGrid {

    public function prepareItems(): void
    {
        $this->fromQuery(Post::query());
    }

    public function prepareColumns(): void
    {
        // Serial No
        $this->addColumn([
            'title' => '#',
            'column' => '',
            'type' => 'serial-no',
        ]);

        // Title
        $this->addColumn([
            'column' => 'title',
            'type' => 'string',
            'sortable' => true,
            'filterable' => true,
            'searchable' => true,
        ]);

        // Status
        $this->addColumn([
            'column' => 'status',
            'type' => 'number',
            'sortable' => true,
            'formatter' => fn($item) => $item->status ? 'Active' : 'Inactive',
        ]);
    }
}

$grid = app(\App\DataGrid\PostGrid::class);
return view('post.index', ['grid' => $grid]);

$exportData = $grid->export(); // Params: $options = []

'export' => function ($item, $column, $options) {
    // Return formatted value for export
}

'searchable' => true,

'searchable' => function($query, $searchTerm, $column) {
    // Custom query logic
}

'filterable' => true,

'filterable' => function($query, $filterTerm, $column) {
    // Custom filter logic
}

'filterParams' => [
    'type' => 'select', // Options: select, checkbox, radio
    'options' => [
        ['label' => 'All', 'value' => ''],
        ['label' => 'Active', 'value' => 1],
        ['label' => 'Inactive', 'value' => 0],
    ],
    'attributes' => '', // Custom HTML attributes
]
bash
php artisan vendor:publish --tag=zk-datagrid
blade
{!! $grid->render() !!}