我正在使用yajra laravel datatables软件包版本 6.0 ,我想添加导出按钮组但我不知道如何实现它。
假设我有一个SubscriberController
类,如下所示:
public function newsletterDatatable (Request $request) {
$subscribers = Subscriber::select(['sub_id', 'email', 'confirmed', 'created_at']);
$datatable = app('datatables')->of($subscribers)
->orderBy('created_at', 'desc')
->addColumn('checkbox', '<input type="checkbox" name="item_id[]" value="{{$sub_id}}">');
return $datatable->make(true);
}
在JS代码中我有:
$('#allSubscribersTable').DataTable({
processing: true,
serverSide: true,
"bSort": false,
"responsive": true,
dom: 'Bfrtip',
buttons: [
'copy', 'csv', 'excel', 'pdf', 'print'
],
ajax: {
url: '{!! route('admin.newsletterDatatable') !!}'
},
columns: [
{data: 'checkbox', "width": "20px"}
]
});
正如您所看到的,我在DataTable初始化时使用了buttons
选项,但是当显示表时没有任何导出按钮。
在Docs上的buttons-export页面上说我应该添加以下代码但我不知道在我的控制器中何处以及如何使用:
namespace App\DataTables;
use App\User;
use Yajra\Datatables\Services\DataTable;
class UsersDataTable extends DataTable {
//...some default stubs deleted for simplicity.
public function html() {
return $this->builder()
->columns($this->getColumns())
->parameters([
'buttons' => ['export'],
]);
}
...
如果有人知道请帮助我。
答案 0 :(得分:3)
添加DataTable类以处理查询和查看。使用
php artisan datatables:make OrdersDataTable
在这里使用ajax(),query()和html()方法。然后在您的SubscriberController类中使用App \ DataTables \ OrdersDataTable和渲染视图。
OrdersDataTable类的示例代码 -
<?php
namespace App\DataTables;
use App\Models\Order;
class OrdersDataTable extends DataTable
{
/**
* Display ajax response.
*
* @return \Illuminate\Http\JsonResponse
*/
public function ajax()
{
return $this->datatables
->eloquent($this->query())
->addColumn('action', function ($row) {
return '<a class="details-item" data-target="#modal-details-item" data-uri="'.url('admin/orders/'.$row->id).'" href=""><i class="fa fa-eye"></i> View Details</a> ';
})
->make(true);
}
/**
* Get the query object to be processed by dataTables.
*
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
*/
public function query()
{
//$query = Order::query();
$query = Order::select('orders.id', 'products.title', 'sites.name', 'orders.created_at')
->orderBy('orders.created_at', 'desc')
->leftJoin('products', 'products.id','=','orders.product_id')
->leftJoin('sites', 'sites.id','=','orders.site_id');
return $this->applyScopes($query);
}
/**
* Optional method if you want to use html builder.
*
* @return \Yajra\Datatables\Html\Builder
*/
public function html()
{
return $this->builder()
->columns($this->getColumns())
->parameters([
'dom' => 'Bfrtip',
'buttons' => ['csv', 'excel', 'print'],
]);
}
/**
* Get columns.
*
* @return array
*/
protected function getColumns()
{
return [
'id' => ['data' => 'id', 'name' => 'orders.id'],
'title' =>[ 'data' => 'title', 'name' => 'products.title'],
'name' => ['data' => 'name', 'name' => 'sites.name'],
'created_at' => ['data' => 'created_at', 'name' => 'orders.created_at'],
'action' => ['data' => 'action', 'name' => 'action', 'className' => "text-right", 'orderable'=> false, 'searchable' => false]
];
}
/**
* Get filename for export.
*
* @return string
*/
protected function filename()
{
return 'order_export_' . time();
}
}
在你的控制器中只渲染视图。有关详情,请查看此内容 - https://datatables.yajrabox.com/service