У меня проблема с генерацией кнопок экспорта с таблицами данных Yajra laravel. Я знаю, что обычным способом является создание класса, расширяющего набор данных, но я не использую этот способ, потому что мой набор данных возвращается моим API.
Вот мой код для генерации моего набора данных:
Api:
$filter_products = $request->filter_products;
$filter_versions = $request->filter_versions;
$registrations = Registration::where('activations.account_id',$account_id)
->join('activations','registrations.activation_id','=','activations.id')
->join('products', 'registrations.product_id','=','products.id')
->join('computers', 'registrations.computer_id','=','computers.id')
->join('sessions', 'registrations.id','=','sessions.registration_id')
->select('products.name as product','registrations.id as id','registrations.custom_label as identifier','registrations.product_version as version','computers.name as computer_name',DB::raw('MAX(sessions.created_at) as last_session_date'),DB::raw('count(sessions.id) as session_count'))
->whereIn('products.id',is_array($filter_products) ? $filter_products : [$filter_products])
->whereIn('registrations.product_version',is_array($filter_versions) ? $filter_versions : [$filter_versions])
->groupBy('registrations.id')
->get();
return DataTables::of($registrations)
->addColumn('action', function ($row) {
$btn = '<a id="'.$row->id.'" class="far-trash edit btn btn-primary btn-sm" data-toggle="modal" data-target="#modal_remove_registration"> X </a>';
return $btn;
})
->rawColumns(['action'])
->make(true);
Клиент:
$('#registrationTable').DataTable({
"language": {
@if ( Config::get('app.locale') == 'en')
"url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/English.json",
@elseif ( Config::get('app.locale') == 'fr' )
"url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/French.json",
@endif
},
buttons: [
'copy', 'excel', 'pdf'
],
processing: true,
serverSide: true,
"order": [[5, 'DESC']],
ajax: {
url: '{{ config('services.passport.api_url'). 'registrations/' }}' + localStorage.getItem('current_account_id'),
headers: {
'Authorization': `Bearer ` + localStorage.getItem('access_token'),
},
type: 'POST',
data: function (d) {
d.filter_products = $("#filter_products").val();
d.filter_versions = $("#filter_versions").val();
},
},
responsive: true,
columns: [
{data: 'id', name: 'id'},
{data: 'identifier', name: 'identifier'},
{data: 'product', name: 'product'},
{data: 'version', name: 'version'},
{data: 'computer_name', name: 'computer_name'},
{data: 'last_session_date', name: 'last_session_date'},
{data: 'session_count', name: 'session_count'},
{data: 'action', name: 'action', orderable: false, searchable: false}
]
});
Это ответ json, который я получаю по умолчанию без моих фильтров:
{"draw":3,"recordsTotal":1,"recordsFiltered":1,"data":[{"product":"Lexibar","id":"704","identifier":null,"version":"2.08.02","computer_name":"Anne-Laure","last_session_date":"2019-11-18 14:42:16","session_count":"1816","action":"<a id=\"704\" class=\"far-trash edit btn btn-primary btn-sm\" data-toggle=\"modal\" data-target=\"#modal_remove_registration\"> X <\/a>"}],"input":{"draw":"3","columns":[{"data":"id","name":"id","searchable":"true","orderable":"true","search":{"value":"","regex":"false"}},{"data":"identifier","name":"identifier","searchable":"true","orderable":"true","search":{"value":"","regex":"false"}},{"data":"product","name":"product","searchable":"true","orderable":"true","search":{"value":"","regex":"false"}},{"data":"version","name":"version","searchable":"true","orderable":"true","search":{"value":"","regex":"false"}},{"data":"computer_name","name":"computer_name","searchable":"true","orderable":"true","search":{"value":"","regex":"false"}},{"data":"last_session_date","name":"last_session_date","searchable":"true","orderable":"true","search":{"value":"","regex":"false"}},{"data":"session_count","name":"session_count","searchable":"true","orderable":"true","search":{"value":"","regex":"false"}},{"data":"action","name":"action","searchable":"false","orderable":"false","search":{"value":"","regex":"false"}}],"order":[{"column":"5","dir":"DESC"}],"start":"0","length":"10","search":{"value":"","regex":"false"},"filter_products":["1"],"filter_versions":["2.08.02"]}}
Он работает очень ну, кроме одной вещи, у меня нет кнопок экспорта, есть ли способ сделать это? Я не хочу использовать рендер, так как мой API возвращает данные.