doudun8705 2017-01-17 08:58
浏览 89

将导出按钮组添加到yajra datatable laravel包

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

Suppose I have a SubscriberController class like this :

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 :

$('#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 buttons option on DataTable initialization but when table is shown there are not any export buttons.

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

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.

  • 写回答

1条回答 默认 最新

  • douwei7976 2017-04-18 10:44
    关注

    Add DataTable class to handle query and view. Use

    php artisan datatables:make OrdersDataTable

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

    Sample code of OrdersDataTable class-

    <?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

    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私