简体   繁体   中英

Laravel & Ajax load data without refreshing page

I'm currently working on a code that displays users activity log. Ajax will call the route and gets the response (json) results. I need to be able to display the log records on admin dashboard without refreshing the page.

Ajax

 $(document).ready(function() {
        $.ajax({
            type: 'get',
            url:"{{ route('users.activity') }}",
            dataType: 'json',
            success: function (data) {
                $.each(data, function() {
                    $.each(this, function(index, value) {
                        console.log(value);
                        $('#activity').append('' +
                            '<div class="sl-item">' +
                            '<div class="sl-left bg-success"> <i class="ti-user"></i></div>' +
                            '<div class="sl-right">' +
                            '<div class="font-medium">' + value.causer.username + '<span class="sl-date pull-right"> ' + value.created_at + ' </span></div>' +
                            '<div class="desc">' + value.description + '</div>' +
                            '</div>' +
                            '</div>');
                    });
                });
            },error:function(){
                console.log(data);
            }
        });
 });

If I understood, you can do this:

$(document).ready(function() {
    var requesting = false;
    var request = function() { 
    requesting = true;
    $.ajax({
        type: 'get',
        url:"{{ route('users.activity') }}",
        dataType: 'json',
        success: function (data) {
            $.each(data, function() {
                $.each(this, function(index, value) {
                    console.log(value);
                    $('#activity').append('' +
                        '<div class="sl-item">' +
                        '<div class="sl-left bg-success"> <i class="ti-user"></i></div>' +
                        '<div class="sl-right">' +
                        '<div class="font-medium">' + value.causer.username + '<span class="sl-date pull-right"> ' + value.created_at + ' </span></div>' +
                        '<div class="desc">' + value.description + '</div>' +
                        '</div>' +
                        '</div>');
                        requesting = false;
                });
            });
        },error:function(){
            console.log(data);
        }
    });
   };
  if(!requesting){
      setTimeout(request, 1000);
  }
 });

Every seconds it does a request to your users.activity route, so there is an update every second.

You need to send last record id in server. so you can get only new data. my updated answer based on @stackedo answer .

$(document).ready(function () {
    var lastId = 0; //Set id to 0 so you will get all records on page load.
    var request = function () {
    $.ajax({
        type: 'get',
        url: "{{ route('users.activity') }}",
        data: { id: lastId }, //Add request data
        dataType: 'json',
        success: function (data) {
            $.each(data, function () {
                $.each(this, function (index, value) {
                    console.log(value);
                    lastId = value.id; //Change lastId when we get responce from ajax
                    $('#activity').append('' +
                        '<div class="sl-item">' +
                        '<div class="sl-left bg-success"> <i class="ti-user"></i></div>' +
                        '<div class="sl-right">' +
                        '<div class="font-medium">' + value.causer.username + '<span class="sl-date pull-right"> ' + value.created_at + ' </span></div>' +
                        '<div class="desc">' + value.description + '</div>' +
                        '</div>' +
                        '</div>');

                });
            });
        }, error: function () {
            console.log(data);
        }
    });
    };
   setInterval(request, 1000);
});

In controller add were condition to query :

UserActivity::where('id','>',$request->id)->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM