Skip to content
Advertisement

Select Multiple ids and Pass to controller at once

Am tried to select multiple of ids from the concept of delete multiple ids, but am get the error and ids not passed

this is code i tried

  <button style="margin-bottom: 10px" class="btn btn-primary delete_all" data-url="{{ 
      url('receiveAllUnit') }}">Received All</button>
     $(document).ready(function () {


    $('#master').on('click', function(e) {
     if($(this).is(':checked',true))
     {
        $(".sub_chk").prop('checked', true);
     } else {
        $(".sub_chk").prop('checked',false);
     }
    });


    $('.delete_all').on('click', function(e) {


        var allVals = [];
        $(".sub_chk:checked").each(function() {
            allVals.push($(this).attr('data-id'));
        });


        if(allVals.length <=0)
        {
            alert("Please select row.");
        }  else {


            var check = confirm("Are you sure you want to Receive All Units?");
            if(check == true){


                var join_selected_values = allVals.join(",");


                $.ajax({
                    url: $(this).data('url'),
                    type: 'POST',
                    headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                    data: 'ids='+join_selected_values,
                    success: function (data) {
                        if (data['success']) {
                            $(".sub_chk:checked").each(function() {
                                $(this).parents("tr").remove();
                            });
                            alert(data['success']);
                        } else if (data['error']) {
                            alert(data['error']);
                        } else {
                            alert('Whoops Something went wrong!!');
                        }
                    },
                    error: function (data) {
                        alert(data.responseText);
                    }
                });


              $.each(allVals, function( index, value ) {
                  $('table tr').filter("[data-row-id='" + value + "']").remove();
              });
            }
        }
    });


    $('[data-toggle=confirmation]').confirmation({
        rootSelector: '[data-toggle=confirmation]',
        onConfirm: function (event, element) {
            element.trigger('confirm');
        }
    });


    $(document).on('confirm', function (e) {
        var ele = e.target;
        e.preventDefault();


        $.ajax({
            url: ele.href,
            type: 'POST',
            headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
            success: function (data) {
                if (data['success']) {
                    $("#" + data['tr']).slideUp("slow");
                    alert(data['success']);
                } else if (data['error']) {
                    alert(data['error']);
                } else {
                    alert('Whoops Something went wrong!!');
                }
            },
            error: function (data) {
                alert(data.responseText);
            }
        });


        return false;
    });
});

kindly can you show me how i can fetch those ids and passed to controller

am expect to update one column like this

 RotateDevice::whereIn([ ['DeviceId',$ids]
                                        ])
                              ->update([
                                'status'=>0,
                              ]);

Advertisement

Answer

In Ajax Call replace From

data: 'ids='+join_selected_values,

To

data: {ids:join_selected_values},

and in Controller

$idsString=$request->input('ids');
$idsArray=explode(",",trim($idsString,','));//explode to split ids to array , and trim to ensure if ids received with , at the end or beginning of string. 
if(!empty($idsArray)){
     RotateDevice::whereIn('DeviceId',$idsArray)
          ->update(['status'=>0]);
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement