在Laravel中成功完成AJAX后刷新foreach循环数据

时间:2018-02-24 16:39:38

标签: laravel

每当我使用AJAX删除许可证密钥时,我想刷新foreach循环数据。删除部分已经开始工作了。现在唯一的问题是,它不会更新我的foreach循环中的数据。我试着找到这个问题的答案,我发现这个“Laravel refresh data after ajax”。我已经尝试过这里的步骤,但我似乎无法使其发挥作用。

泛音/ licenses_loop.blade.php

<div class="white-box">
        <h3 class="box-title">All Licenses <span class="pull-right">{{ $licenses->count() }}</span></h3>
        <hr>
            <p>Assigned <span class="pull-right">{{ $licenses_is_assigned->count() }}</span></p>
            <p>Unassigned <span class="pull-right">{{ $licenses_not_assigned->count() }}</span></p>
        <br>
        <button type="button" class="btn btn-info waves-effect waves-light" data-toggle="modal" data-target="#detailedViewModal" style="width: 100%;">Detailed View</button>
    </div>
    <div class="modal fade" id="detailedViewModal" tabindex="-1" role="dialog" aria-labelledby="detailedModalLabel">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title" id="detailedModalLabel">Detailed View</h4>
                </div>
                <div class="modal-body">
                    <div class="table-responsive">
                        <table id="detailedViewTable" class="table">
                            <thead>
                                <tr>
                                    <th>Product</th>
                                    <th>Licenses</th>
                                    <th>Assigned</th>
                                    <th>Unassigned</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($licenses_container as $product_name => $license_counts)
                                    <tr>
                                        <td>{{ $product_name }}</td>
                                        <td>{{ $license_counts['assigned'] + $license_counts['unassigned'] }}</td>
                                        <td>{{ $license_counts['assigned'] }}</td>
                                        <td>{{ $license_counts['unassigned'] }}</td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

LicenseController.php

public function ajax(Request $request)
    {
        $html = view('partials.licenses_loop', compact('view'))->render();

        return response()->json(compact('html'));
    }

license.js

function confirm_delete(id, element) {
    swal({   
        title: "Are you sure?",   
        text: "You will not be able to recover this license",  
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",   
        confirmButtonText: "Yes, delete it!",   
        closeOnConfirm: false
     }, function(){
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        $.ajax({
            url: "/tango/licenses/delete/" + id,
            type: 'DELETE',
            success: function(data, textStatus, jqXHR) 
            {
                swal({   
                    title: "Deleted!",   
                    text:  "License has been successfully deleted.",
                    timer: 2000,
                        type: "success",
                        showConfirmButton: true
                });
                $(element).parents('tr').hide(1000);
                renderTable();
            },
            error: function(jqXHR, status, error) 
            {
                console.log(status + ": " + error);
            }
        });
    });
}

function renderTable() {
    var $request = $.get('licenses'); // make request
    var $container = $('.license-count-container');

    $request.done(function(data) { // success
        $container.html(data.html);
    });
}

路由/ web.php

Route::post('/tango/licenses', 'LicenseController@ajax');

licenses.blade.php

<div class="col-md-3 col-lg-3 col-sm-5 license-count-container">
            @include('partials.licenses_loop')
        </div>

1 个答案:

答案 0 :(得分:0)

在您的Javascript中,您使用$.get来执行GET请求。但是,在路由文件中,您声明它是通过Route::post的POST路由。我猜服务器请求返回405: Method not allowed,然后从不执行服务器操作。