如何在按钮上的Laravel中使用Ajax?

时间:2019-06-01 12:30:22

标签: javascript php ajax laravel

我有一个ajax表,我想使用ajax过滤所有活动课程的结果。

这是我尝试过的-查看

按钮-过滤活动记录

<a href="{{ route('admin.courses.index',['$active_records'=>'active']) }}" style="margin-left:10px;" class="btn btn-success">
            Active <span class="badge badge-light">{{$course_count_active}}</span>
          <span class="sr-only">total courses</span></a>

单击按钮时我需要过滤结果的表

<p>
        <ul class="list-inline">
            <li><a href="{{ route('admin.courses.index') }}" style="{{ request('show_deleted') == 1 ? '' : 'font-weight: 700' }}">@lang('global.app_all')</a></li> |
            <li><a href="{{ route('admin.courses.index') }}?show_deleted=1" style="{{ request('show_deleted') == 1 ? 'font-weight: 700' : '' }}">@lang('global.app_trash')</a></li>
        </ul>
    </p>


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

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


                        <th>@lang('global.courses.fields.title')</th>
                        <th>@lang('global.courses.fields.safety-topic')</th>
                        <th>@lang('global.courses.fields.course-image')</th>
                        <th>@lang('global.courses.fields.start-date')</th>
                        <th>@lang('global.courses.fields.due-date')</th>
                        <th>@lang('global.courses.fields.status')</th>

                        @if( request('show_deleted') == 1 )
                        <th>&nbsp;</th>
                        @else
                        <th>&nbsp;</th>
                        @endif
                    </tr>
                </thead>
            </table>
        </div>
    </div>
@stop

这是ajax脚本

@section('javascript')
    <script>
        @can('course_delete')
            @if ( request('show_deleted') != 1 ) window.route_mass_crud_entries_destroy = '{{ route('admin.courses.mass_destroy') }}'; @endif
        @endcan
        $(document).ready(function () {
            window.dtDefaultOptions.ajax = '{!! route('admin.courses.index') !!}?show_deleted={{ request('show_deleted') }}';
            window.dtDefaultOptions.columns = [@can('course_delete')
                @if ( request('show_deleted') != 1 )
                    {data: 'massDelete', name: 'id', searchable: false, sortable: false},
                @endif
                @endcan

                {data: 'title', name: 'title'},
                {data: 'safety_topic.topic', name: 'safety_topic.topic'},
                {data: 'course_image', name: 'course_image'},
                {data: 'start_date', name: 'start_date'},
                {data: 'due_date', name: 'due_date'},
                {data: 'status', name: 'status', searchable:true},
                {data: 'actions', name: 'actions', searchable: false, sortable: false}
            ];
            processAjaxTables();
        });
    </script>
@endsection

我当前按钮是刷新页面,但是结果没有被过滤。

0 个答案:

没有答案