hassan khosro
hassan khosro

Reputation: 189

Change status with ajax in laravel

I'm using Laravel in my project. I have a voucher that maintains a status. I've used AJAX for changing the status and the AJAX code works successfully.

However the status did not change in my view until after I refresh the page. I'm looking for a way to solve this issue.

<button class="btn btn-link p-0 change" type="submit" data-id="{{ $voucher->id }}">
  @if ($voucher->status == 1)
    <i class="fa fa-toggle-on text-success"></i>
  @else
    <i class="fa fa-toggle-off text-muted"></i>
  @endif
</button> 
@if ($voucher->status == 1)
  <span class="badge badge-soft-success">
    enable
  </span> 
@else
  <span class="badge badge-soft-pink">
    disbale
  </span> 
@endif
$(".change").click(function() {
  var id = $(this).data("id");
  $.ajax({
    url: "vouchers/change-status/" + id,
    type: 'put',
    dataType: "JSON",
    data: {
      "id": id,
      "_method": 'put',
      "_token": "{{ csrf_token() }}",
    },
    success: function() {
      console.log("it Work");
      // $("tr#"+id).remove();
    }
  });

  console.log("It failed");
});

Upvotes: 2

Views: 1861

Answers (3)

Roman Meyer
Roman Meyer

Reputation: 2872

You can try this way:

Blade

Wrap your code in some holder with class status-enabled, when status is 1. Set special classes for elements which will be visible or not according status.

<div class="holder @if($voucher->status == 1) status-enabled @endif">
    <button class="btn btn-link p-0 change" type="submit" data-id="{{ $voucher->id }}">
        <i class="fa fa-toggle-on text-success enabled-visible"></i>
        <i class="fa fa-toggle-off text-muted enabled-invisible"></i>
    </button>

    <span class="badge badge-soft-success enabled-visible">
        enable
    </span>

    <span class="badge badge-soft-pink enabled-invisible">
        disbale
    </span>
</div>

CSS

Set rules:

    .holder .enabled-visible {
        display: none;
    }
    .holder .enabled-invisible {
        display: inline-block;
    }

    .holder.status-enabled .enabled-visible {
        display: inline-block;
    }
    .holder.status-enabled .enabled-invisible {
        display: none;
    }

JS

Toggle holder class when you have successfuly change status.

$(".change").click(function() {
    var id = $(this).data("id");
    $.ajax({
        url: "vouchers/change-status/" + id,
        type: 'put',
        dataType: "JSON",
        data: {
            "id": id,
            "_method": 'put',
            "_token": "{{ csrf_token() }}",
        },
        success: function() {
            $(this).closest('.holder').toggleClass('status-enabled');
        }
    });

    console.log("It failed");
});

Upvotes: 2

Sajad Haibat
Sajad Haibat

Reputation: 717

Just in your success function:

 success: function() {
      console.log("it Work");
      $("i.fa-toggle-on).hide();
      $("i.fa-toggle-off).show();
    }

Upvotes: 1

Aravinth E
Aravinth E

Reputation: 491

Html Tag no need of if condition php blade.

<button class="btn btn-link p-0 change" type="submit" data-id="{{ $voucher->id }}">
    @if ($voucher->status == 1)
        <i class="fa fa-toggle-on text-success"></i>
    @else
        <i class="fa fa-toggle-off text-muted"></i>
    @endif
</button>
<div id="alert-message"></div>  // add this line
@endrole

change this code into your code. script file

$(".change").click(function() {
        var id = $(this).data("id");

        var success = '<span class="badge badge-soft-success">' +
            'enable' +
            '</span>';

        var failed = '<span class="badge badge-soft-pink">' +
            'disbale' +
            '</span>';
        $.ajax({
            url: "vouchers/change-status/" + id,
            type: 'put',
            dataType: "JSON",
            data: {
                "id": id,
                "_method": 'put',
                "_token": "{{ csrf_token() }}",
            },
            success: function() {
                console.log("it Work");
                $('#alert-message').html(success);
                // $("tr#"+id).remove();
            }
        });

        $('#alert-message').html(failed);
    });

Upvotes: 0

Related Questions