0

I have a toggle button to set status from pending to approved, it works fine but I cannot sort the values based on pending or approved. enter image description here Every row value is displayed as PendingApprovedPendingApproved (in console.log). (In database its correctly saved with values 0 and 1). Any solution would be helpful. Thank You

  <td>
              <input data-id="{{$college->id}}" class="toggle-class" type="checkbox" data-onstyle="danger"        
                 data-offstyle="info" data-toggle="toggle" 
                data-on="Pending" data-off="Approved" {{$college->status?'checked':''}}>
                                    </td>

<script>
    $('.toggle-class').change(function() {

        var status = $(this).prop('checked') == true ? 1 : 0;
        var id = $(this).data('id');
        $.ajax({

            type: "GET",
            dataType: "json",
            url: "/changeStatus",
            data: {
                'status': status,
                'id': id
            },

            success: function(data) {
                console.log(data.success)
          
            }
        });
    })
</script>

changeStatus

 public function changeStatus(Request $request)
    {

        $colleges = College::find($request->id);
        if ($colleges->status == '1') {
            $colleges->status = 0;
        } else {
            $colleges->status = 1;
        }
        $colleges->save();

        return response()->json(['success' => 'Status change successfully.']);
    }

filter

 <script>
var filters = ["", "", "",""];

function applyNewFilters() {
   
  console.log(filters);
  var rows = $('#myTable').find('tr');
  for(var i = 1; i < rows.length; i++) {
    var row = $(rows[i]); // we're re-applying all filters anyway
    row.show();
  }
  for(var i = 2; i < rows.length; i++) {
    var row = $(rows[i]);
    for(var f = 0; f < filters.length; f++) {
      //column is going to be equal to f
      var col = $(row.find('td')[f]).text();
      
      console.log(col);
      if(col.indexOf(filters[f]) < 0) {
        row.hide();
      }
    }
  }
}

$('.filter-input').on('change', function() {
  var $modifiedInput = $(this);
  
  console.log($modifiedInput.val());
  var column = $modifiedInput.attr('data-col');
    filters[column] = $modifiedInput.val();
    applyNewFilters();
});
</script>
4
  • Show example code of your changeStatus function in Laravel, see what it returns Commented Dec 21, 2022 at 6:36
  • I have added the code above, it returns 0 or 1. Commented Dec 21, 2022 at 6:44
  • there is no orderBy() function defined Commented Dec 21, 2022 at 6:47
  • @AbdullaNilam even with the function it doesnot work because its taking PendingApprovedPendingApproved value for each row. Commented Dec 21, 2022 at 6:52

2 Answers 2

1

Could you replace $colleges->status == '1' with $request->status == '1' on the changeStatus method first and see what will happen?

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

1 Comment

that doesnot update my status and values changes on page refresh.
1

What do you need exactly? If you need to sort index based on status :

College::orderBy('status')->get();

4 Comments

I want to add a textbox below status (search by status) and when the user inputs Pending it should be filtered (all pending at top)
I think it's better to use check box instead of text box to filter data based on status. Are you agree?
Any problem? Is your problem solved?
I have added the filter script above, can I use the same function to filter status?

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.