skip to Main Content

I am new over here and in laravel.

and this is my first project.

so please help me.


[This is something that I want

how to achieve it](https://phpout.com/wp-content/uploads/2023/12/Gv9yv.png)


I have this code:

@foreach($booksData as $book)
 <td id="total">{{$book->number_books}}
    <br>
      <h6 class="text-success" data-id="{{ $book->id }}">Available: {{ ($book->number_books) -  ($book->number_borrowedbooks) }} </h6>
 </td>
 <td style="width: 25px;">
   <inputdata-id="{{$book->id}}"data-forwhat="books" class="toggle-class"type="checkbox" data-onstyle="success" data-offstyle="danger"data-toggle="toggle"data-on="Active"data-off="Inactive" {{$book->status ? 'checked' : ''}}>
</td>
@endforeach

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js">     
 </script>
<script>
 jQuery(document).ready(function ($) {
    console.log('jQuery is loaded!');
    $('.toggle-class').change(function(){
        var status =$(this).prop('checked') == true ? 1 : 0;
        var forwhat = $(this).data('forwhat') || 'books';
        var id = $(this).data('id');
        var token = $('meta[name="csrf-token"]').attr('content');
            $.ajax({
                type: "GET",
                dataType: "json",
                url: '/changeStatus',
                data: {
            'status': status,
            'id': id,
            'forwhat': forwhat,
            '_token': token
        },
                success: function(data){
                    console.log(data.success)
                   
                },
                error: function (xhr, status, error) {
                console.error(xhr.responseText);
            }
            })
    });
});
</script>

this code is working perfectly fine.
now my senior has asked me if available = 0 then status will automatically be inactive and when it is 1 it will automatically be active rest time how it is working will be perfectly ok

Thanking You
for all your time and efforts.

2

Answers


  1. To automatically toggle the status based on the availability, you can adjust the script to do this check before making the AJAX call. Here’s the updated script:

    jQuery(document).ready(function ($) {
    console.log('jQuery is loaded!');
    
    $('.toggle-class').change(function(){
        var status = $(this).prop('checked') ? 1 : 0;
        var forwhat = $(this).data('forwhat') || 'books';
        var id = $(this).data('id');
        var token = $('meta[name="csrf-token"]').attr('content');
        
        // Check if available books are 0
        var available = parseInt($(this).closest('td').find('.text-success').text().match(/d+/));
        if (available === 0) {
            status = 0; // Set status to 0 (Inactive)
            $(this).bootstrapToggle('off'); // Toggle switch visually to 'Inactive'
        } else {
            $(this).bootstrapToggle('on'); // Toggle switch visually to 'Active'
        }
    
        $.ajax({
            type: "GET",
            dataType: "json",
            url: '/changeStatus',
            data: {
                'status': status,
                'id': id,
                'forwhat': forwhat,
                '_token': token
            },
            success: function(data){
                console.log(data.success);
            },
            error: function (xhr, status, error) {
                console.error(xhr.responseText);
            }
        });
    });
    

    });

    Login or Signup to reply.
  2. If you want the status to be active based on whether the availability is more than 0 once the page is loaded, then you can do this.

    <td style="width: 25px;">
      <input data-id="{{$book->id}}" data-forwhat="books" class="toggle-class" type="checkbox" data-onstyle="success" data-offstyle="danger" data-toggle="toggle" data-on="Active" data-off="Inactive" {{ ($book->status || ($book->number_books - $book->number_borrowedbooks)) ? 'checked' : '' }}>
    </td>
    

    Here the condition is added such that when either book status is true or availability is greater than 0 then status will be active.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search