yajra / laravel-datatables

jQuery DataTables API for Laravel
https://yajrabox.com/docs/laravel-datatables
MIT License
4.74k stars 862 forks source link

Buttons Not Working [Datatable 6.8] #481

Closed minedun6 closed 8 years ago

minedun6 commented 8 years ago

Hi, I just followed your tutorials on how to implement datatable the table buttons but it doesn't display on the table, is there anyway to get some help ? here's the code:

`<?php

namespace App\DataTables;

use App\User; use Yajra\Datatables\Services\DataTable;

class UsersDataTable extends DataTable {

/**
 * Display ajax response.
 *
 * @return \Illuminate\Http\JsonResponse
 */
public function ajax()
{
    return $this->datatables
        ->eloquent($this->query())
        ->make(true);
}

/**
 * Get the query object to be processed by datatables.
 *
 * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
 */
public function query()
{
    $users = User::query();

    return $this->applyScopes($users);
}

/**
 * Optional method if you want to use html builder.
 *
 * @return \Yajra\Datatables\Html\Builder
 */
public function html()
{
    return $this->builder()
                ->columns($this->getColumns())
                ->ajax('')
                ->addAction(['width' => '80px'])
                ->parameters($this->getBuilderParameters());
}

/**
 * Get columns.
 *
 * @return array
 */
private function getColumns()
{
    return [
        'id',
        'name',
        'email',
        'created_at',
        'updated_at',
    ];
}

/**
 * Get filename for export.
 *
 * @return string
 */
protected function filename()
{
    return 'users';
}

} `

this is the users/products controller :

`use App\Product; use App\User; use Illuminate\Http\Request;

use App\Http\Requests; use Yajra\Datatables\Datatables; use App\DataTables\UsersDataTable;

class ProductsController extends Controller { /* * Display a listing of the resource. * @return \Illuminate\Http\Response */ public function index(UsersDataTable $dataTable) { return $dataTable->render('admin.products.index'); } `

and the view where i call it :

`{!! $dataTable->table() !!}

@section('functions')

<script src="https://cdn.datatables.net/buttons/1.0.3/js/dataTables.buttons.min.js"></script>
<script src="/vendor/datatables/buttons.server-side.js"></script>
{!! $dataTable->scripts() !!}

@endsection ` now as the requirements says, I'm using the 6.8 version of this package, and i have php version 5.5.12, but didn't get any errors so i beleive it works perfectly.

this is the output of the the script: (function(window,$){window.LaravelDataTables=window.LaravelDataTables||{};window.LaravelDataTables["dataTableBuilder"]=$("#dataTableBuilder").DataTable({"serverSide":true,"processing":true,"ajax":"","columns":[{"name":"id","data":"id","title":"Id","orderable":true,"searchable":true},{"name":"name","data":"name","title":"Name","orderable":true,"searchable":true},{"name":"email","data":"email","title":"Email","orderable":true,"searchable":true},{"name":"created_at","data":"created_at","title":"Created At","orderable":true,"searchable":true},{"name":"updated_at","data":"updated_at","title":"Updated At","orderable":true,"searchable":true},{"defaultContent":"","data":"action","name":"action","title":"Action","render":null,"orderable":false,"searchable":false,"width":"80px"}],"order":[[0,"desc"]],"buttons":["create",{"extend":"collection","text":"<i class=\"fa fa-download\"><\/i> Export","buttons":["csv","excel","pdf"]},"print","reset","reload"]});})(window,jQuery); and i'm importing the scripts and the css as told, not error in the console. I hope I'm not leaving anything.

yajra commented 8 years ago

@minedun6, you need to add a default dom value on your DT with B to render the buttons.

public function html()
{
    return $this->builder()
        ->columns([
            'id',
            'name',
            'email',
            'created_at',
            'updated_at',
        ])
        ->parameters([
            'dom' => 'Bfrtip',
            'buttons' => ['csv', 'excel', 'pdf', 'print', 'reset', 'reload'],
        ]);
}
waqas740 commented 7 years ago

Hi Yajra,

i want to use buttons(csv, excel and print) and also need show entries drop down. Here's the code:

public function showBooking()
    {
        return view('adminlte::layouts.report.booking');
  }
 public function bookingDataTable(Request $request)
    {
        $bookings = Auth::user()->bookings();

        return Datatables::of($bookings)
            ->editColumn('status', function ($booking) {
              return $booking->status ? "Booked" : "Non-Booked";
            })
            ->filter(function ($query) use ($request) {
                if ($request->has('amount') and $request->get('amount') != 0) {

                    $query->whereColumn('paid_amount', '<', 'total_amount');

                }
                if ($request->has('date_from') and !is_null($request->get('date_from'))) {

                    $query->whereDate('date', '>=', $request->get('date_from'));

                }
                if ($request->has('date_to') and !is_null($request->get('date_to'))) {

                    $query->whereDate('date', '<=', $request->get('date_to'));

                }
                if ($request['search']['value'] !="")
                {
                    $value = $request['search']['value'];
                    if(substr($value ,0 ,1) =="B" || substr($value ,0 ,1) =="b")
                    {
                        $status = 1;
                    }
                    else if(substr($value ,0 ,1) =="N" ||substr($value ,0 ,1) =="n")
                    {
                        $status = 0;
                    }
                    else{
                        $status = $value;
                    }
                    $query  ->where('status','like','%'.$status.'%')
                            ->orWhere('halls.name', 'like', '%'.$value.'%')
                            ->orWhere('paid_amount', 'like', '%'.$value.'%')
                            ->orWhere('total_amount', 'like', '%'.$value.'%')
                            ->orWhere('date', 'like', '%'.$value.'%');
                }

            })
            ->make(true);
}

View File

`@extends('adminlte::layouts.app')

@section('htmlheader_title')
    {{ trans('adminlte_lang::message.home') }}
@endsection

@section('main-content')
    <div class="row">
        <div class="col-xs-12">

            <div class="box">
                <div class="box-header">
                    <div class="row">
                        <div class="col-sm-3">
                            <h3 class="box-title">List of Booking</h3>
                        </div>

                    </div>

                </div>
                <!-- /.box-header -->
                <div class="box-body">
                    <div class="well">
                        <h3>Custom Filter</h3>
                        <form method="POST" id="search-form" class="form-inline" role="form">
                            <div class="form-group">
                                <label>Amount</label>
                                <select class="form-control" name="amount">
                                    <option value="0">All</option>
                                    <option value="1">Pending Amount</option>

                                </select>
                            </div>

                            <div class="form-group">
                                <label for="email">Date From</label>
                                <input type="date" class="form-control" name="date_from" placeholder="search Date">
                            </div>
                            <div class="form-group">
                                <label for="email">Date To</label>
                                <input type="date" class="form-control" name="date_to" placeholder="search Date">
                            </div>

                            <button type="submit" class="btn btn-primary">Search</button>
                        </form>

                    </div>
                    <table class="table table-bordered table-striped" id="bookings-table">
                        <thead>
                        <tr>
                            <th>Hall name</th>
                            <th>Status</th>
                            <th>Paid Amount</th>
                            <th>Total Amount</th>
                            <th>Date</th>
                        </tr>
                        </thead>

                        <tfoot>
                        <tr>
                            <th>Hall name</th>
                            <th>Status</th>
                            <th>Paid Amount</th>
                            <th>Total Amount</th>

                            <th>Date</th>
                        </tr>
                        </tfoot>
                    </table>
                </div>
                <!-- /.box-body -->
            </div>
            <!-- /.box -->
        </div>
        <!-- /.col -->
    </div>
    <!-- /.row -->

@endsection
@push('scripts')
<script src="/vendor/datatables/buttons.server-side.js"></script>
<script>

    $(function () {

        var oTable = $('#bookings-table').DataTable({

            processing: true,
            serverSide: true,
            ajax: {
                url: '{!! url('/booking/datatable') !!}',
                type: 'post',
                data: function (d) {
                    d.amount = $('select option:selected').val();
                    d.date_from = $('input[name=date_from]').val();
                    d.date_to = $('input[name=date_to]').val();
                }
            },

            columns: [
                {data: 'hall_name', name: 'hall_name'},
                {data: 'status', name: 'status'},
                {data: 'paid_amount', name: 'paid_amount'},
                {data: 'total_amount', name: 'total_amount'},
                {data: 'date', name: 'date'}
            ],

        });

        $('#search-form').on('submit', function (e) {
            oTable.draw();
            e.preventDefault();
        });
    });
</script>
@endpush
`

Is there anyway to solve my problem?

reiter777 commented 7 years ago

hi @yajra i did what you post ->parameters([ 'dom' => 'Bfrtip', 'buttons' => ['csv', 'excel', 'pdf', 'print', 'reset', 'reload'], ]); but still can not see the Buttons, and also the button of show entries is missing when i put those lines man, how can i fix this problem man? is this working for laravel 5.4?

ibrahimsorady commented 5 years ago

@minedun6, you need to add a default dom value on your DT with B to render the buttons.

public function html()
{
    return $this->builder()
        ->columns([
            'id',
            'name',
            'email',
            'created_at',
            'updated_at',
        ])
        ->parameters([
            'dom' => 'Bfrtip',
            'buttons' => ['csv', 'excel', 'pdf', 'print', 'reset', 'reload'],
        ]);
}

only export working cant read csv, excel