{"action":"created","issue":{"url":"https://api.github.com/repos/yajra/laravel-datatables/issues/547","repository_url":"https://api.github.com/repos/yajra/laravel-datatables","labels_url":"https://api.github.com/repos/yajra/laravel-datatables/issues/547/labels{/name}","comments_url":"https://api.github.com/repos/yajra/laravel-datatables/issues/547/comments","events_url":"https://api.github.com/repos/yajra/laravel-datatables/issues/547/events","html_url":"https://github.com/yajra/laravel-datatables/issues/547","id":153464518,"number":547,"title":"How to create HTML elements in service implementation","user":{"login":"mprandot","id":14298396,"avatar_url":"https://avatars.githubusercontent.com/u/14298396?v=3","gravatar_id":"","url":"https://api.github.com/users/mprandot","html_url":"https://github.com/mprandot","followers_url":"https://api.github.com/users/mprandot/followers","following_url":"https://api.github.com/users/mprandot/following{/other_user}","gists_url":"https://api.github.com/users/mprandot/gists{/gist_id}","starred_url":"https://api.github.com/users/mprandot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mprandot/subscriptions","organizations_url":"https://api.github.com/users/mprandot/orgs","repos_url":"https://api.github.com/users/mprandot/repos","events_url":"https://api.github.com/users/mprandot/events{/privacy}","received_events_url":"https://api.github.com/users/mprandot/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-05-06T14:36:13Z","updated_at":"2016-05-07T06:28:39Z","closed_at":null,"body":"Hello Yajra!\r\n\r\nAfter using DataTables for sometime, i want to transform it in services, but, i don't know who do it..\r\n\r\nI use datatables like this in my view : \r\n```\r\n <script>\r\n        $(function () {\r\n            var pathname = window.location.pathname;\r\n            $('#table-pessoa').DataTable({\r\n                processing: true,\r\n                serverSide: true,\r\n                ajax: {\r\n                    url: '{{route('pessoa.datatable')}}',\r\n                    data: {url: pathname}\r\n                },\r\n                columns: [\r\n                    {data: 'nome-fantasia', name: 'nome-fantasia'},\r\n                    {data: 'cpf-cnpj', name: 'cpf-cnpj'},\r\n                    {data: 'fone', name: 'fone'},\r\n                    {data: 'action', name: 'Ação', orderable: false, searchable: false}\r\n                ], \"language\": {\r\n                    \"url\": '{{asset('vendor/datatables/DataTable-pt-BR.json')}}'\r\n                }\r\n            });\r\n        });\r\n    </script>\r\n```\r\nand , like this in my controller : \r\n\r\n```\r\npublic function datatableAjax() {\r\n        if (str_contains(Input::get('url'), 'inativos')) {\r\n            $registros = $this->repository->getAll(true);\r\n        } else {\r\n            $registros = $this->repository->getAll();\r\n        }\r\n\r\n        return Datatables::of($registros)\r\n        ->addColumn('action', function ($registro) {\r\n            return botaoEditar($registro, 'pessoa.alterar') . ' ' .\r\n                   botaoDetalhes($registro, 'pessoa.detalhes') . ' ' . \r\n                   botaoExcluir($registro, 'pessoa.excluir')  ;\r\n        })\r\n        ->make(true);\r\n    }\r\n```\r\n\r\nIn this mode, i can create my buttons inside helpers botaoEditar botaoDetalhes ... \r\nBut, in services, I can create dynamic html button here ? You can pass one example?\r\n\r\n```\r\npublic function html()\r\n{\r\n    return $this->builder()\r\n        ->columns([\r\n            'id',\r\n            'name',\r\n            'email',\r\n            'created_at',\r\n            'updated_at',\r\n           //action here???\r\n        ])\r\n        ->parameters([\r\n            'dom' => 'Bfrtip',\r\n            'buttons' => ['csv', 'excel', 'pdf', 'print', 'reset', 'reload'],\r\n        ]);\r\n}\r\n```\r\n\r\nThanks for your help!\r\n"},"comment":{"url":"https://api.github.com/repos/yajra/laravel-datatables/issues/comments/217611172","html_url":"https://github.com/yajra/laravel-datatables/issues/547#issuecomment-217611172","issue_url":"https://api.github.com/repos/yajra/laravel-datatables/issues/547","id":217611172,"user":{"login":"yajra","id":2687997,"avatar_url":"https://avatars.githubusercontent.com/u/2687997?v=3","gravatar_id":"","url":"https://api.github.com/users/yajra","html_url":"https://github.com/yajra","followers_url":"https://api.github.com/users/yajra/followers","following_url":"https://api.github.com/users/yajra/following{/other_user}","gists_url":"https://api.github.com/users/yajra/gists{/gist_id}","starred_url":"https://api.github.com/users/yajra/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yajra/subscriptions","organizations_url":"https://api.github.com/users/yajra/orgs","repos_url":"https://api.github.com/users/yajra/repos","events_url":"https://api.github.com/users/yajra/events{/privacy}","received_events_url":"https://api.github.com/users/yajra/received_events","type":"User","site_admin":false},"created_at":"2016-05-07T06:28:39Z","updated_at":"2016-05-07T06:28:39Z","body":"You can update your DT code to services by doing something like this:\r\n\r\n- Transfer your DT response code to `ajax()` method of the DataTable class and extract your repository call inside the `query()` method like below:\r\n```php\r\npublic function ajax() {\r\n    return $this->datatables->eloquent($this->query())\r\n        ->addColumn('action', function ($registro) {\r\n            return botaoEditar($registro, 'pessoa.alterar') . ' ' .\r\n                   botaoDetalhes($registro, 'pessoa.detalhes') . ' ' . \r\n                   botaoExcluir($registro, 'pessoa.excluir')  ;\r\n        })\r\n        ->make(true);\r\n}\r\n\r\npublic function query() {\r\n    if (str_contains(Input::get('url'), 'inativos')) {\r\n        return $this->repository->getAll(true);\r\n    } else {\r\n        return $this->repository->getAll();\r\n    }\r\n}\r\n```\r\n\r\n- You can optionally use the html builder or use the same script you have now. Just make sure that you have the correct ajax url in place. If you want to use the builder, you can use addAction method for that.\r\n```php\r\npublic function html()\r\n{\r\n    return $this->builder()\r\n        ->columns([\r\n            'id',\r\n            'name',\r\n            'email',\r\n            'created_at',\r\n            'updated_at',\r\n        ])\r\n        ->addAction()\r\n        ->parameters([\r\n            'dom' => 'Bfrtip',\r\n            'buttons' => ['csv', 'excel', 'pdf', 'print', 'reset', 'reload'],\r\n        ]);\r\n}\r\n```\r\n\r\n- Add a dedicated method on your controller that will handle all DT response.\r\n```php\r\npublic function index(MyDataTable $dataTable)\r\n{\r\n    return $dataTable->render('table.view.index');\r\n}\r\n```\r\n\r\nYou can also check this [demo](http://datatables.yajrabox.com/service) for ref."}}