Ajax DataTables没有显示,而是返回错误

时间:2017-11-29 16:03:47

标签: ajax datatables laravel-5.4

所以我对Ajax DataTable有这个问题,当我加载页面时会出现这个错误:Error

我不知道我做错了什么。它应该显示一个表,其中包含存储在数据库中的数据,并且每行添加一个“视图”,“编辑”,“删除”按钮。删除按钮执行软删除。

这是我的控制器的代码:

<?php

namespace App\Http\Controllers\Admin;

use App\Supplier;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\StoreSuppliersRequest;
use App\Http\Requests\Admin\UpdateSuppliersRequest;
use Yajra\Datatables\Datatables;
use DB;
class SuppliersController extends Controller
{
    /**
     * Display a listing of Group.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        if (! Gate::allows('supplier_access')) {
            return abort(401);
        }


    $user = \Auth::user();

    if (request()->ajax()) {
        $query = Supplier::query();



          if (\Auth::getUser()->role_id==1) {

          $q = Supplier::select(['name','anschrift']);
         }

        $template = 'actionsTemplate';
        if(request('show_deleted') == 1) {

    if (! Gate::allows('supplier_delete')) {
        return abort(401);
    }
            $q->onlyTrashed();
            $template = 'restoreTemplate';
        }
        $table = DataTables::of($q)->make(true);

        $table->setRowAttr([
            'data-entry-id' => '{{$id}}',
        ]);
        $table->addColumn('massDelete', '&nbsp;');
        $table->addColumn('actions', '&nbsp;');
        $table->editColumn('actions', function ($row) use ($template) {
            $gateKey  = 'supplier_';
            $routeKey = 'admin.suppliers';

            return view($template, compact('row', 'gateKey', 'routeKey'));
        });
        $table->editColumn('Name', function ($row) {
            return $row->Name ? $row->Name : '';
        });
        $table->editColumn('anschrift', function ($row) {
            return $row->anschrift ? $row->anschrift : '';
        });


        return $table->make(true);
    }

    return view('admin.suppliers.index');
}

这是表格所在的HTML:

<div class="panel panel-default">
    <div class="panel-heading">
        @lang('quickadmin.qa_list')
    </div>

    <div class="panel-body table-responsive">
        <table class="table table-bordered table-striped ajaxTable @can('supplier_delete') @if ( request('show_deleted') != 1 ) dt-select @endif @endcan">
            <thead>
                <tr>
                    @can('supplier_delete')
                        @if ( request('show_deleted') != 1 )<th style="text-align:center;"><input type="checkbox" id="select-all" /></th>@endif
                    @endcan

                    <th>Name</th>
                    <th>Anschrift</th>
                    <!-- <th>Adminsnachname</th> -->
                    @if( request('show_deleted') == 1 )
                    <th>&nbsp;</th>
                    @else
                    <th>&nbsp;</th>
                    @endif
                </tr>
            </thead>
        </table>
    </div>
</div>

处理它的javascript代码:

<script>
    @can('supplier_delete')
        @if ( request('show_deleted') != 1 ) window.route_mass_crud_entries_destroy = '{{ route('admin.suppliers.mass_destroy') }}'; @endif
    @endcan
    $(document).ready(function () {
        window.dtDefaultOptions.ajax = '{!! route('admin.suppliers.index') !!}?show_deleted={{ request('show_deleted') }}';
        window.dtDefaultOptions.columns = [
            @can('supplier_delete')
            @if ( request('show_deleted') != 1 )
                {data: 'massDelete', name: 'id', searchable: false, sortable: false},
            @endif
            @endcan
            {data: 'Name', name: 'Name'},
            {data: 'anschrift', name: 'anschrift'},
            // {data: 'admin.lastname', lastname: 'admin.lastname'},
            {data: 'actions', name: 'actions', searchable: false, sortable: false}
        ];
        processAjaxTables();
    });
</script>

processAjaxTables:

function processAjaxTables() {
$('.ajaxTable').each(function () {
    window.dtDefaultOptions.processing = true;
    window.dtDefaultOptions.serverSide = true;
    if ($(this).hasClass('dt-select')) {
        window.dtDefaultOptions.select = {
            style: 'multi',
            selector: 'td:first-child'
        };

        window.dtDefaultOptions.columnDefs.push({
            orderable: false,
            className: 'select-checkbox',
            targets: 0
        });
    }
    $(this).DataTable(window.dtDefaultOptions);
    if (typeof window.route_mass_crud_entries_destroy != 'undefined') {
        $(this).siblings('.actions').html('<a href="' + window.route_mass_crud_entries_destroy + '" class="btn btn-xs btn-danger js-delete-selected" style="margin-top:0.755em;margin-left: 20px;">'+window.deleteButtonTrans+'</a>');
    }
});

}

到现在为止,我仍然没有弄清楚我做错了什么

0 个答案:

没有答案