laravel-generator icon indicating copy to clipboard operation
laravel-generator copied to clipboard

[Feature] Use aginev/datagrid

Open shemgp opened this issue 7 years ago • 3 comments

Hello,

I would like to use https://github.com/aginev/datagrid with patches (https://github.com/aginev/datagrid/pull/6) in the generated table.blade.php as it has sorting and filters, etc and would like to have you input on how to go about it. I realize that I probably need to edit generateBladeTableBody() in ViewGenerator.php to make it work I want it also to only be an option and not a replacement. How would I go about it? would you accept an if statement in the generateBladeTableBody() to choose between original and datagrid?

Here's my sample table.blade.php:

<table class="table table-responsive" id="modems-table">
    <thead>
        <th>Serial</th>
        <th>MAC Address</th>
        <th>Modem Type</th>
        <th>Created/Updated by</th>
        <th colspan="3">Action</th>
    </thead>
    <tbody>
    @foreach($modems as $modem)
        <tr>
            <td>{!! $modem->serial !!}</td>
            <td>{!! $modem->mac_address !!}</td>
            <td>{!! $modem->modemType->name !!}</td>
            <td>{!! $modem->user->full_name !!}</td>
            <td>
                {!! Form::open(['route' => ['inventory.modems.destroy', $modem->id], 'method' => 'delete']) !!}
                <div class='btn-group'>
                    <a href="{!! route('inventory.modems.show', [$modem->id]) !!}" class='btn btn-default btn-xs'><i class="glyphicon glyphicon-eye-open"></i></a>
                    <a href="{!! route('inventory.modems.edit', [$modem->id]) !!}" class='btn btn-default btn-xs'><i class="glyphicon glyphicon-edit"></i></a>
                    {!! Form::button('<i class="glyphicon glyphicon-trash"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure?')"]) !!}
                </div>
                {!! Form::close() !!}
            </td>
        </tr>
    @endforeach
    </tbody>
</table>

Here's the one converted (similar) to using datagrid

<?php
use PrettyDateTime\PrettyDateTime;

$grid = new \Datagrid($modems, Request::get('f', []));
$grid
    ->setColumn('serial', 'Serial')
    ->setColumn('mac_address', 'MAC Address', [
        'sortable' => true,
        'has_filters' => true,
    ])
    ->setColumn('modemType.name', 'Modem Type', [
        'sortable'    => true,
        'has_filters' => true,
    ])
    ->setColumn('user.full_name', 'Created/Updated by', [
        'sortable'    => true,
        'has_filters' => true,
        'wrapper'=> function ($value, $row) {
            $tooltip = $row->updated_at ? $row->updated_at : $row->created_at;
            $tooltip = PrettyDateTime::parse(new DateTime($tooltip));
            return '<span title="'.$tooltip.'">'.$value.'</span>';
        }
    ])
    // Setup action column
    ->setActionColumn([
        'wrapper' => function ($value, $modem) {
            $out = Form::open(['route' => ['inventory.modems.destroy', $modem->id], 'method' => 'delete']);
            $out .= '<div class="btn-group">';
            $out .= '    <a href="'.route('inventory.modems.show', [$modem->id]).'" class="btn btn-default btn-xs"><i class="glyphicon glyphicon-eye-open"></i></a>';
            $out .= '    <a href="'.route('inventory.modems.edit', [$modem->id]).'" class="btn btn-default btn-xs"><i class="glyphicon glyphicon-edit"></i></a>';
            $out .= Form::button('<i class="glyphicon glyphicon-trash"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure?')"]);
            $out .= '</div>';
            $out .= Form::close();

            return $out;
        }
    ]);

    echo $grid->show();
?>

Thank you.

shemgp avatar Sep 14 '16 01:09 shemgp

Hi,

You know you can use datatables?

It doesn't have a per-column filter (only a global search field), but that can be easily achieved (see here)

underdpt avatar Sep 15 '16 12:09 underdpt

Hello, Yes, but it only works with js enabled. I tried using datatables and it would not show the data.  Probably because of a library problem. Plus, looking at the code, it's hard to customize the columns.   Hopefully, you would you accept a pull request. I kind of have it working locally with a patched Repository (need to patch to make filters filter using AND) and it'll need to add files to core-templates too.

shemgp avatar Sep 15 '16 12:09 shemgp

Here's the pull request: #296

shemgp avatar Sep 16 '16 03:09 shemgp