Skip to content Skip to sidebar Skip to footer

Update Status Of Each Dynamic Row Using Checkbox

I have a form with dynamic rows, after fetching record I want to update status of selected rows by checkbox. I'm successfully getting each row checkbox values and dynamic row id i

Solution 1:

What you are looking for is to update multiple checkboxes in one go. So, you need to store both selected & unselected checkboxes.

Your jQuery

let checkedIds = [];
let unCheckedIds = [];
$('.updatestatusclass').each(function () {
    if ($(this).is(":checked")) {
        checkedIds.push($(this).attr('data-id'));
    } else {
        unCheckedIds.push($(this).attr('data-id'));
    }
});

$.ajax({
    url: "{{ url('/updatestatus') }}",
    method: 'POST',
    data: {
        checkedIds: checkedIds,
        checkedIdStatus: 'active', //do your thingunCheckedIds: unCheckedIds,
        unCheckedIdStatus: 'inactive', //do your thing
    },
    dataType: 'json',
    success: function (response) {
        alert('Updated Successfully!');
    },
    error: function (response) {
        alert("Not Updated, Try again.");
    }
});

In your Controller

if ($request->ajax()) {
    Services::whereIn('id', $request->get('checkedIds'))
        ->update(array(
            'status' =>  $request->get('checkedIdStatus')
        ));
    Services::whereIn('id', $request->get('unCheckedIds'))
        ->update(array(
            'status' =>  $request->get('unCheckedIdStatus')
        ));
}

Hope this helps. Cheers!

Post a Comment for "Update Status Of Each Dynamic Row Using Checkbox"