Laravel推送和堆栈不适用于Ajax

时间:2019-01-04 14:06:26

标签: javascript php jquery laravel datatable

我在Laravel视图刀片文件中使用@push@stack。在刀片文件中,我有初始的Jquery DataTable。正常刷新后一切正常。

但是当我使用JSON格式的ajax请求呈现View时,则使用DataTable。也许,与javascript绑定有关的一些问题。

这是我的代码

@extends('layouts.admin.master')
@section('content')

@push('script')
<script >
var t = $('#lead-list').DataTable( {
    processing: true,
    serverSide: true,
    ajax: "{!! route('store-manager.lead.list') !!}",
    columns: [
        { data: 'rownum', name: 'rownum' },
        { data: 'created_at', name: "{!! __('dashboard.creation_date') !!}" },
        { data: 'name', name: "{!! __('dashboard.lead_name') !!}" },
        { data: 'assign_to.first_name', name: "{!! __('dashboard.assign_to') !!}" },
        { data: 'source.name', name: "{!! __('dashboard.lead_source') !!}" },
        { data: 'status.name', name: "{!! __('dashboard.lead_status') !!}" },
        { data: 'country.name', name: "{!! __('dashboard.country') !!}" },
        { data: 'city.name', name: "{!! __('dashboard.city') !!}" },
        { data: 'city.name', name: "{!! __('dashboard.action') !!}" },
    ],
    initComplete: function () {
        this.api().columns().every(function () {
            var column = this;
            var input = document.createElement("input");
            $(input).appendTo($(column.footer()).empty())
            .on('change', function () {
                column.search($(this).val(), false, false, true).draw();
            });
        });
    },
    dom: 'Bfrtip',
    buttons: [
        'colvis'
    ]
});
t.on( 'order.dt search.dt', function () {
    t.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
        cell.innerHTML = i+1;
    } );
} ).draw();
</script>
@endpush

    <div class="container-fluid">
        <div class="row clearfix">
            <!-- Task Info -->
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="card">
                    @include('elements.common.alert')

                    <div class="body">
                        <div class="table-responsive">
                            <table id="lead-list" class="table table-hover text-nowrap table-condensed">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>@lang('dashboard.creation_date')</th>
                                        <th>@lang('dashboard.lead_name')</th>
                                        <th>@lang('dashboard.assign_to')</th>
                                        <th>@lang('dashboard.lead_source')</th>
                                        <th>@lang('dashboard.lead_status')</th>
                                        <th>@lang('dashboard.country')</th>
                                        <th>@lang('dashboard.city')</th>
                                        <th>@lang('dashboard.action')</th>
                                    </tr>
                                </thead>
                                <tfoot>
                                    <th>#</th>
                                    <th>@lang('dashboard.creation_date')</th>
                                    <th>@lang('dashboard.lead_name')</th>
                                    <th>@lang('dashboard.assign_to')</th>
                                    <th>@lang('dashboard.lead_source')</th>
                                    <th>@lang('dashboard.lead_status')</th>
                                    <th>@lang('dashboard.country')</th>
                                    <th>@lang('dashboard.city')</th>
                                    <th>@lang('dashboard.action')</th>
                                </tfoot>
                            </table>
                        </div>
                        {{-- {{$leads->links()}} --}}
                    </div>
                </div>
            </div>
            <!-- #END# Task Info -->
        </div>
    </div>
@endsection

当我在该ajax调用之后重新绑定数据表时,它可以正常工作。但是对于每个视图,我都有不同的DataTable列和数据。因此,我不得不一次又一次地编写相同的代码。让我知道是否有任何简便的方法可以使代码工作而无需重新绑定DataTable。

0 个答案:

没有答案