0

I want to refresh foreach loop data whenever I delete a license key using AJAX. The delete part is working already. The only problem now is that, it doesn't update the data in my foreach loop. I've try searching for an answer to this problem and I found this "Laravel refresh data after ajax". I've tried following the steps here but I can't seem to make it work.

partials/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);
    });
}

routes/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
  • have you tried location.reload(); Commented Feb 24, 2018 at 18:09

1 Answer 1

0

In your Javascript you are using $.get to perform a GET request. However, on your route file, you are stating that it is a POST route through Route::post. I guess that the server request is returning a 405: Method not allowed and then the server action is never performed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.