0

I want to add csv and excel export button to one dataTable only. MyController file:

public function index(Request $request)
{
    $Databaseurl = Route('orders.index');
    if ($request->ajax()) {
        $orders = Order::all();
        return datatables()->of($orders)
            ->editColumn('price', function ($order) {
                return '<div class="text-nowrap text-center">'.number_format($order->price, 0, '.', ',').'</div>';
            })
            ->editColumn('address', function ($order) {
                return \Illuminate\Support\Str::limit($order->address,25);
            })
            ->editColumn('city', function ($order) {
                return '<div class="text-center">'.\Illuminate\Support\Str::limit($order->city,25).'</div>';
            })
            ->addIndexColumn()
            ->addColumn('action', function($order){
                return '<a href="'.route('order.edit',['id' => $order->id]).'" class="text-nowrap btn-sm btn-primary text-center"><i class="far fa-eye"></i> view</a>';
            })
            ->rawColumns(['action','city','price'])
            ->toJson();
    }
    return view('order.index',compact('Databaseurl'));
}

and the data table jquery codes:

<script type="text/javascript">
    $(function () {
        var table = $('.mydatatable').DataTable({
            "oLanguage": {
                "sUrl": "https://cdn.datatables.net/plug-ins/1.10.19/i18n/Persian.json",
            },
            "pageLength": 25,
            processing: true,
            serverSide: true,
            ajax: "{{ $Databaseurl }}",
            columns: [
                {data: 'id', name: 'id'},
                {data: 'customer_name', name: 'customer_name'},
                {data: 'address', name: 'address', searchable: false},
                {data: 'city', name: 'city'},
                {data: 'price', name: 'price', searchable: false},
                {data: 'created_at', name: 'created_at', searchable: false},
                {data: 'action', name: 'action', orderable:false,searchable: false},
            ],
        });
    });
</script>

Also I watched this video, but I prefer not to go through more difficult steps and solve this problem in the same controller and blade file. Because I only want this for one view table Is there a solution?

1 Answer 1

2

After some days, I found this way: first add this links to your blode file:

<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.0.3/css/buttons.dataTables.min.css">

<script src="https://cdn.datatables.net/buttons/1.6.4/js/dataTables.buttons.min.js"></script>

Then add this following codes to the js datatable codes:

 dom: 'Bfrtip',
 buttons: [
      'copy', 'excel',
 ],
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.