view-components / grids

MIT License
86 stars 17 forks source link

Delete form #8

Closed Gurzhii closed 8 years ago

Gurzhii commented 8 years ago

Got an issue here.

public function generate()
    {

        $provider = new EloquentDataProvider(PostEloquentModel::class);
        $input = $this->defaultInputSource();
        $grid = new Grid(
            $provider,
            [
                new TableCaption('Blog'),
                new Column('id'),
                new Column('title'),
                new Column('slug'),
                (new Column('edit'))
                    ->setValueCalculator(function ($row) {
                        return BaseGrid::generateEditFormButton($row->id, 'admin.blog.edit');
                    }),
                (new Column('delete'))
                    ->setValueCalculator(function ($row) {
                       return BaseGrid::generateDeleteFormButton($row->id, 'admin.blog.destroy');
                    }),
                new PaginationControl($input('page', 1), $this->page_size),
                new ColumnSortingControl('title', $input->option('sort')),
            ]
        );

        $customization = new BootstrapStyling();
        $customization->apply($grid);

        $this->setHtml($grid->render());

        return $this;
    }

Here is generateDeleteFormButton

 public static function generateDeleteFormButton($id = 0, $route = '')
    {
        return view('admin.partials.custom_grid_columns.delete_column', [
            'id' => $id,
            'route' => $route
        ]);
    }

and also delete_column view

{!! Form::open(['method' => 'DELETE', 'route' => [$route, $id]]) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}

On first button i got no rendered form tag https://monosnap.com/file/qTPHn3Z9JCnxo6JJogRLVuFqCBsL5A.png and because of this flow my click expected as GET request https://monosnap.com/file/J5slnZgbyEIacj1mKnLvYIdUgfIL62.png also nothing happed

Gurzhii commented 8 years ago

Also checked source, i got first form here https://monosnap.com/file/IQkoFXqel9sSSlnugBay9VUCJvZEy4.png

Gurzhii commented 8 years ago

I think its because of im trying to render form in non-closed form https://monosnap.com/file/qMigzn5A9EkB2FfPWnCX5sh5NxIbtm.png

Gurzhii commented 8 years ago

I changed in ManagedList.php, line 239

static::FORM_ID => new Part(
                new Tag('form', ['data-role' => 'managed_list_form']),
                static::FORM_ID,
                static::CONTAINER_ID
            ),

to

static::FORM_ID => new Part(
                new Tag('div', ['data-role' => 'managed_list_form']),
                static::FORM_ID,
                static::CONTAINER_ID
            ),

and all rendering like i'm expecting - https://monosnap.com/file/gsXmcNswCuPKhAh2zXP3ucOkQ5A8LL.png for what are u using this form?

Upd. I checked up code, and saw that filters using this form, but is it possible to close it earlier than at the end of the table?

Nayjest commented 8 years ago

Looks like you create form inside another form (grids are placed into forms, but you can change that modifying components tree)

Gurzhii commented 8 years ago

What is your solution for delete button?

Gurzhii commented 8 years ago

Here is the answer: if u need delete button works fine u need to include laravel.js(https://gist.github.com/soufianeEL/3f8483f0f3dc9e3ec5d9) and also did the trick with replacing your form by

<a href="{{ route($route, [$id])  }}" data-method="delete" data-token="{{csrf_token()}}" data-confirm="Are you sure?" class="btn btn-danger">Delete</a>
Nayjest commented 8 years ago

a) Place form for submitting "delete" request after (or before) grid and add onclick event for pushing deleted record ID into form & submitting it (looks like best way)

b) Detach "form" component from grid & attach to another place, for example inside "control_row" component (becouse there are all inputs that should be placed inside this form). (require no javascript, but you will have alot of sex with grids package)

c) Delete records by GET request, but you will need to make confirmation to avoid auto-visiting links by some browser plugins & provide token for security reasons (looks overcomplicated, but all that security features can be optional depending on your business/quality requirements)

d) Send HTTP request for deleting record via ajax

Nayjest commented 8 years ago

Nice solution too, thanx for sharing

Nayjest commented 8 years ago

Also I started to work on documentation, that's drafts, so you will not find links ot it inside readme, but it may be useful:

https://github.com/view-components/view-components/blob/master/doc/cookbook.md

https://github.com/view-components/view-components/blob/master/doc/components.md

Nayjest commented 8 years ago

Some advices:

wdog commented 6 years ago

I just added e simple minimal page to the wiki. Feel free to change everything