Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create a default column for all views? #1024

Closed
jackrsantana opened this issue Feb 21, 2017 · 3 comments
Closed

How to create a default column for all views? #1024

jackrsantana opened this issue Feb 21, 2017 · 3 comments
Labels

Comments

@jackrsantana
Copy link

I would like to know how I can include a column with the include and edit buttons for all screens, without duplicating the code for each view?

I was able to create the column with the buttons, but the way I did it, I would have to repeat the code for each view. This would increase maintenance and possibly propagate bugs.

if ($request->ajax()) {
            $cidades = Cidade::with('estado')->get();

            return Datatables::of($cidades)
                ->addColumn('action', function ($cidades) {
                    $editar  =
                        '<a href="' . route($this->dados['rota'] . '.edit', $cidades->id) . '" class="ui icon button yellow">' .
                        '   <i class="icon write"></i>' .
                        '</a>';
                    $deletar = '<form action="' . route($this->dados['rota'] . '.destroy', $cidades->id) . '" method="post" style="display: inline">' .
                        csrf_field() .
                        method_field('DELETE') .
                        '   <button type="submit" class="ui icon button red">' .
                        '       <i class="icon trash"></i>' .
                        '</button >' .
                        '</form >';

                    return $editar . $deletar;
                })
                ->make(true);
        }

System details

  • Operating System Windows 10
  • PHP Version 7.1
  • Laravel Version 5.4
  • Laravel-Datatables Version 1.10.13
@yajra
Copy link
Owner

yajra commented Feb 23, 2017

Extract your buttons code to a partial view then re-use it.

->addColumn('action', function ($cidades) {
    return view('path.to.buttons', compact('cidaded')->render();
})

@yajra yajra added the question label Feb 23, 2017
@jackrsantana
Copy link
Author

I was able to do what you suggested. I moved the buttons to a separate view. However, I'm now not sure how I can send the ids to the buttons so that they are synchronized with their proper records when rendered by the view.

<a href="{{HOW DO I GET THE ID FROM HERE}}/edit" class="ui icon button yellow">
    <i class="icon write"></i>
</a>

<form action="{{HOW DO I GET THE ID FROM HERE}}" method="post" style="display: inline">
    {{ csrf_field() }}
    {{ Form::hidden("_method", "DELETE") }}

    <button type="submit" class="ui icon button red">
        <i class="icon trash"></i>
    </button>
</form>

@yajra
Copy link
Owner

yajra commented Mar 15, 2017

You can use a generic variable name like model:

->addColumn('action', function ($model) {
    return view('path.to.buttons', compact('model')->render();
})
<form action="{{$model->id}}" method="post" style="display: inline">
    {{ csrf_field() }}
    {{ Form::hidden("_method", "DELETE") }}

    <button type="submit" class="ui icon button red">
        <i class="icon trash"></i>
    </button>
</form>

@yajra yajra closed this as completed Mar 15, 2017
@github-actions github-actions bot locked and limited conversation to collaborators Oct 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants