从yajra数据表填充数据时出现服务器错误

时间:2020-09-10 12:28:35

标签: laravel-5 datatable yajra-datatable

我试图通过laravel中的yajra数据表来呈现表,但是当我运行代码时,它给了我以下错误

未捕获的错误:DataTables警告:表ID = contact_table-Ajax错误。有关此错误的更多信息,请参见http://datatables.net/tn/7

我几乎已经尝试了所有方法,但是此错误不会消失,可以请一些帮助,这是我的代码:

控制器

private function indexCustomer()
{
    if (!auth()->user()->can('customer.view')) {
        abort(403, 'Unauthorized action.');
    }

    $business_id = request()->session()->get('user.business_id');

    $query = Contact::leftjoin('transactions AS t', 'contacts.id', '=', 't.contact_id')
                ->leftjoin('customer_groups AS cg', 'contacts.customer_group_id', '=', 'cg.id')
                ->where('contacts.business_id', $business_id)
                ->onlyCustomers()
                ->select(['contacts.contact_id', 'contacts.name', 'contacts.created_at', 'total_rp', 'cg.name as customer_group', 'city', 'state', 'country', 'landmark', 'mobile', 'contacts.id', 'is_default',
                    DB::raw("SUM(IF(t.type = 'sell' AND t.status = 'final', final_total, 0)) as total_invoice"),
                    DB::raw("SUM(IF(t.type = 'sell' AND t.status = 'final', (SELECT SUM(IF(is_return = 1,-1*amount,amount)) FROM transaction_payments WHERE transaction_payments.transaction_id=t.id), 0)) as invoice_received"),
                    DB::raw("SUM(IF(t.type = 'sell_return', final_total, 0)) as total_sell_return"),
                    DB::raw("SUM(IF(t.type = 'sell_return', (SELECT SUM(amount) FROM transaction_payments WHERE transaction_payments.transaction_id=t.id), 0)) as sell_return_paid"),
                    DB::raw("SUM(IF(t.type = 'opening_balance', final_total, 0)) as opening_balance"),
                    DB::raw("SUM(IF(t.type = 'opening_balance', (SELECT SUM(IF(is_return = 1,-1*amount,amount)) FROM transaction_payments WHERE transaction_payments.transaction_id=t.id), 0)) as opening_balance_paid"),
                    'email', 'tax_number', 'contacts.pay_term_number', 'contacts.pay_term_type', 'contacts.credit_limit', 'contacts.custom_field1', 'contacts.custom_field2', 'contacts.custom_field3', 'contacts.custom_field4', 'contacts.type'
                    ])
                ->groupBy('contacts.id');

    $contacts = Datatables::of($query)
        ->addColumn('address', '{{implode(array_filter([$landmark, $city, $state, $country]), ", ")}}')
        ->addColumn(
            'due',
            '<span class="display_currency contact_due" data-orig-value="{{$total_invoice - $invoice_received}}" data-currency_symbol=true data-highlight=true>{{($total_invoice - $invoice_received)}}</span>'
        )
        ->addColumn(
            'return_due',
            '<span class="display_currency return_due" data-orig-value="{{$total_sell_return - $sell_return_paid}}" data-currency_symbol=true data-highlight=false>{{$total_sell_return - $sell_return_paid }}</span>'
        )
        ->addColumn(
            'action',
            '<div class="btn-group">
                <button type="button" class="btn btn-info dropdown-toggle btn-xs" 
                    data-toggle="dropdown" aria-expanded="false">' .
                    __("messages.actions") .
                    '<span class="caret"></span><span class="sr-only">Toggle Dropdown
                    </span>
                </button>
                <ul class="dropdown-menu dropdown-menu-left" role="menu">
            @if(($total_invoice + $opening_balance - $invoice_received - $opening_balance_paid)  > 0)
                <li><a href="{{action(\'TransactionPaymentController@getPayContactDue\', [$id])}}?type=sell" class="pay_sale_due"><i class="fas fa-money-bill-alt" aria-hidden="true"></i>@lang("contact.pay_due_amount")</a></li>
            @endif
            @if(($total_sell_return - $sell_return_paid)  > 0)
                <li><a href="{{action(\'TransactionPaymentController@getPayContactDue\', [$id])}}?type=sell_return" class="pay_purchase_due"><i class="fas fa-money-bill-alt" aria-hidden="true"></i>@lang("lang_v1.pay_sell_return_due")</a></li>
            @endif
            @can("customer.view")
                <li><a href="{{action(\'ContactController@show\', [$id])}}"><i class="fas fa-eye" aria-hidden="true"></i> @lang("messages.view")</a></li>
            @endcan
            @can("customer.update")
                <li><a href="{{action(\'ContactController@edit\', [$id])}}" class="edit_contact_button"><i class="glyphicon glyphicon-edit"></i> @lang("messages.edit")</a></li>
            @endcan
            @if(!$is_default)
            @can("customer.delete")
                <li><a href="{{action(\'ContactController@destroy\', [$id])}}" class="delete_contact_button"><i class="glyphicon glyphicon-trash"></i> @lang("messages.delete")</a></li>
            @endcan
            @endif
            @can("customer.view")
                <li class="divider"></li>
                <li>
                    <a href="{{action(\'ContactController@show\', [$id])."?view=contact_info"}}">
                        <i class="fas fa-user" aria-hidden="true"></i>
                        @lang("contact.contact_info", ["contact" => __("contact.contact") ])
                    </a>
                </li>
                <li>
                    <a href="{{action(\'ContactController@show\', [$id])."?view=ledger"}}">
                        <i class="fas fa-scroll" aria-hidden="true"></i>
                        @lang("lang_v1.ledger")
                    </a>
                </li>
                @if(in_array($type, ["both", "supplier"]))
                    <li>
                        <a href="{{action(\'ContactController@show\', [$id])."?view=purchase"}}">
                            <i class="fas fa-arrow-circle-down" aria-hidden="true"></i>
                            @lang("purchase.purchases")
                        </a>
                    </li>
                    <li>
                        <a href="{{action(\'ContactController@show\', [$id])."?view=stock_report"}}">
                            <i class="fas fa-hourglass-half" aria-hidden="true"></i>
                            @lang("report.stock_report")
                        </a>
                    </li>
                @endif
                @if(in_array($type, ["both", "customer"]))
                    <li>
                        <a href="{{action(\'ContactController@show\', [$id])."?view=sales"}}">
                            <i class="fas fa-arrow-circle-up" aria-hidden="true"></i>
                            @lang("sale.sells")
                        </a>
                    </li>
                @endif
                <li>
                    <a href="{{action(\'ContactController@show\', [$id])."?view=documents_and_notes"}}">
                        <i class="fas fa-paperclip" aria-hidden="true"></i>
                         @lang("lang_v1.documents_and_notes")
                    </a>
                </li>
            @endcan
            </ul></div>'
        )
        ->editColumn('opening_balance', function ($row) {
            $html = '<span class="display_currency" data-currency_symbol="true" data-orig-value="' . $row->opening_balance . '">' . $row->opening_balance . '</span>';

            return $html;
        })
        ->editColumn('credit_limit', function ($row) {
            $html = __('lang_v1.no_limit');
            if (!is_null($row->credit_limit)) {
                $html = '<span class="display_currency" data-currency_symbol="true" data-orig-value="' . $row->credit_limit . '">' . $row->credit_limit . '</span>';
            }

            return $html;
        })
        ->editColumn('pay_term', '
            @if(!empty($pay_term_type) && !empty($pay_term_number))
                {{$pay_term_number}}
                @lang("lang_v1.".$pay_term_type)
            @endif
        ')
        ->editColumn('total_rp', '{{$total_rp ?? 0}}')
        ->editColumn('created_at', '{{@format_date($created_at)}}')
        ->removeColumn('total_invoice')
        ->removeColumn('opening_balance_paid')
        ->removeColumn('invoice_received')
        ->removeColumn('state')
        ->removeColumn('country')
        ->removeColumn('city')
        ->removeColumn('type')
        ->removeColumn('id')
        ->removeColumn('is_default')
        ->removeColumn('total_sell_return')
        ->removeColumn('sell_return_paid')
        ->filterColumn('address', function ($query, $keyword) {
            $query->whereRaw("CONCAT(COALESCE(landmark, ''), ', ', COALESCE(city, ''), ', ', COALESCE(state, ''), ', ', COALESCE(country, '') ) like ?", ["%{$keyword}%"]);
        });
    $reward_enabled = (request()->session()->get('business.enable_rp') == 1) ? true : false;
    if (!$reward_enabled) {
        $contacts->removeColumn('total_rp');
    }
    return $contacts->rawColumns(['action', 'opening_balance', 'credit_limit', 'pay_term', 'due', 'return_due'])
                    ->make(true);
}

这是我的JS

var contact_table_type = $('#contact_type').val();
if (contact_table_type == 'supplier') {
    var columns = [
        { data: 'action', searchable: false, orderable: false },
        { data: 'contact_id', name: 'contact_id' },
        { data: 'supplier_business_name', name: 'supplier_business_name' },
        { data: 'name', name: 'name' },
        { data: 'email', name: 'email' },
        { data: 'tax_number', name: 'tax_number' },
        { data: 'pay_term', name: 'pay_term', searchable: false, orderable: false },
        { data: 'opening_balance', name: 'opening_balance', searchable: false },
        { data: 'created_at', name: 'contacts.created_at' },
        { data: 'mobile', name: 'mobile' },
        { data: 'due', searchable: false, orderable: false },
        { data: 'return_due', searchable: false, orderable: false },
        { data: 'custom_field1', name: 'custom_field1', searchable: false, orderable: false},
        { data: 'custom_field2', name: 'custom_field2', searchable: false, orderable: false},
        { data: 'custom_field3', name: 'custom_field3', searchable: false, orderable: false},
        { data: 'custom_field4', name: 'custom_field4', searchable: false, orderable: false},
    ];
} else if (contact_table_type == 'customer') {
    var columns = [
        { data: 'action', searchable: false, orderable: false },
        { data: 'contact_id', name: 'contact_id' },
        { data: 'name', name: 'name' },
        { data: 'email', name: 'email' },
        { data: 'tax_number', name: 'tax_number' },
        { data: 'credit_limit', name: 'credit_limit' },
        { data: 'pay_term', name: 'pay_term', searchable: false, orderable: false },
        { data: 'opening_balance', name: 'opening_balance', searchable: false },
        { data: 'created_at', name: 'contacts.created_at' }
    ];

    if ($('#rp_col').length) {
        columns.push({ data: 'total_rp', name: 'total_rp' });
    }
    Array.prototype.push.apply(columns, [{ data: 'customer_group', name: 'cg.name' },
        { data: 'address', name: 'address', orderable: false },
        { data: 'mobile', name: 'mobile' },
        { data: 'due', searchable: false, orderable: false },
        { data: 'return_due', searchable: false, orderable: false },
        { data: 'custom_field1', name: 'custom_field1'},
        { data: 'custom_field2', name: 'custom_field2'},
        { data: 'custom_field3', name: 'custom_field3'},
        { data: 'custom_field4', name: 'custom_field4'},
        ]);

查看

<table class="table table-bordered table-striped" id="contact_table">
                <thead>
                    <tr>
                        <th>@lang('messages.action')</th>
                        <th>@lang('lang_v1.contact_id')</th>
                        @if($type == 'supplier') 
                            <th>@lang('business.business_name')</th>
                            <th>@lang('contact.name')</th>
                            <th>@lang('business.email')</th>
                            <th>@lang('contact.tax_no')</th>
                            <th>@lang('contact.pay_term')</th>
                            <th>@lang('account.opening_balance')</th>
                            <th>@lang('lang_v1.added_on')</th>
                            <th>@lang('contact.mobile')</th>
                            <th>@lang('contact.total_purchase_due')</th>
                            <th>@lang('lang_v1.total_purchase_return_due')</th>
                        @elseif( $type == 'customer')
                            <th>@lang('user.name')</th>
                            <th>@lang('business.email')</th>
                            <th>@lang('contact.tax_no')</th>
                            <th>@lang('lang_v1.credit_limit')</th>
                            <th>@lang('contact.pay_term')</th>
                            <th>@lang('account.opening_balance')</th>
                            <th>@lang('lang_v1.added_on')</th>
                            @if($reward_enabled)
                                <th id="rp_col">{{session('business.rp_name')}}</th>
                            @endif
                            <th>@lang('lang_v1.customer_group')</th>
                            <th>@lang('business.address')</th>
                            <th>@lang('contact.mobile')</th>
                            <th>@lang('contact.total_sale_due')</th>
                            <th>@lang('lang_v1.total_sell_return_due')</th>
                        @endif
                        <th>
                            @lang('lang_v1.contact_custom_field1')
                        </th>
                        <th>
                            @lang('lang_v1.contact_custom_field2')
                        </th>
                        <th>
                            @lang('lang_v1.contact_custom_field3')
                        </th>
                        <th>
                            @lang('lang_v1.contact_custom_field4')
                        </th>
                    </tr>
                </thead>
                <tfoot>
                    <tr class="bg-gray font-17 text-center footer-total">
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td
                            @if($type == 'supplier')
                                colspan="5"
                            @elseif( $type == 'customer')
                                @if($reward_enabled)
                                    colspan="8"
                                @else
                                    colspan="7"
                                @endif
                            @endif>
                                <strong>
                                    @lang('sale.total'):
                                </strong>
                        </td>
                        <td><span class="display_currency" id="footer_contact_due" data-currency_symbol ="true"></span></td>
                        <td><span class="display_currency" id="footer_contact_return_due" data-currency_symbol ="true"></span></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                    </tr>
                </tfoot>
            </table>

任何人都可以帮助我确定最严重的问题。预先谢谢你。

0 个答案:

没有答案