将导出按钮组添加到 yajra 数据表 laravel 包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41692813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:08:23  来源:igfitidea点击:

add Export Button Group to yajra datatable laravel package

phpjquerylaraveldatatables

提问by A.B.Developer

I am using yajra laravel datatablespackage version 6.0and I want to add export button group But I do not know how can I implement that.

我正在使用yajra laravel 数据表6.0版,我想添加导出按钮组但我不知道如何实现。

Suppose I have a SubscriberControllerclass like this :

假设我有一个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);
    }

And in JS codes I have :

在 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"}
    ]
});

As you can see I used buttonsoption on DataTable initialization but when table is shown there are not any export buttons.

如您所见,我buttons在 DataTable 初始化时使用了选项,但是当显示表时,没有任何导出按钮。

On the buttons-exportpage on Docs is said that I should add below Code But I do not know where and how to use in my controller :

在Docs的按钮导出页面上,据说我应该在下面添加代码但我不知道在哪里以及如何在我的控制器中使用:

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'],
                    ]);
    }
...

If anyone know please help me.

如果有人知道请帮助我。

回答by SKM

Add DataTable class to handle query and view. Use

添加 DataTable 类来处理查询和查看。用

php artisan datatables:make OrdersDataTable

php工匠数据表:制作订单数据表

Utilize ajax(), query() and html() methods here. Then in your SubscriberController class use App\DataTables\OrdersDataTable and render view.

在此处使用 ajax()、query() 和 html() 方法。然后在您的 SubscriberController 类中使用 App\DataTables\OrdersDataTable 并呈现视图。

Sample code of OrdersDataTable class-

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();
}
}

And in your controller just render view. For details check this- https://datatables.yajrabox.com/service

而在您的控制器中,只需渲染视图。有关详细信息,请检查此 - https://datatables.yajrabox.com/service