1

I want to insert multi rows checked in my data table, when I click a button valider, everyone I have a problem in a laravel framework, I want to insert line check in a data table when click on button validate, this my code
the display of the salary list

<body>
    <div class="container" id="app">
      <div class="list-group">
       <div class="list-group-item">

    <h3>Pointage Mensuel</h3>
    <div class="col-md-6 col-md-offset-3">
        <h3>jour : {{$data['datek']}} chantier : {{$data['chantier_name']}}</h3>
    </div>
    <button class="btn btn-success add-all" data-url="">Valider Pointage de mois</button>
</div>
</div>

  <div class="list-group">
   <div class="list-group-item">
    <table class="table table-bordered">
        <tr>
            <th>Archive</th>
            <th><input type="checkbox" id="check_all"></th>
            <th>S.No.</th>
            <th>matricule</th>
            <th>nom & prenom</th>
            <th>salaire net</th>
            <th>nbre de jour </th>
            <th>prime</th>
        </tr>
        @if($salaries->count())
            @foreach($salaries as $key => $salarie)
                <tr id="tr_{{$salarie->id}}">
                  <td>archive</td>
                  <td><input type="checkbox" class="checkbox" data-id="{{$salarie->id}}"></td>
                  <td>{{ ++$key }}</td>
                  <td>{{ $salarie->matricule }}</td>
                  <td>{{ $salarie->nom }} {{ $salarie->prenom }}</td>
                  <td>{{ $salarie->salairenet }}</td>
                  <td><input type="text" name="nbreJ" class="form-control" value="{{$data['nbr']}}"></td>
                  <td><input type="text" name="prime" class="form-control" value="0"></td>
                </tr>
            @endforeach
        @endif
    </table>
 </div>
</div>
<!-------------------//////////////////////////------------->
</div> 
</body>

code ajax for checked all /uncheck and

<script type="text/javascript">
    $(document).ready(function () {
        $('#check_all').on('click', function(e) {
         if($(this).is(':checked',true)) {
            $(".checkbox").prop('checked', true);  
         } else {  
            $(".checkbox").prop('checked',false);  }  });
         $('.checkbox').on('click',function(){
            if($('.checkbox:checked').length == $('.checkbox').length){
                $('#check_all').prop('checked',true);
            }else{
                $('#check_all').prop('checked',false); }});
        $('.add-all').on('click', function(e) {
            var idsArr = [];  
            $(".checkbox:checked").each(function() {  
                idsArr.push($(this).attr('data-id'));});  
            if(idsArr.length <=0)  {  
                alert("Please select atleast one record to pointer.");  
            }  else {  
                    var strIds = idsArr.join(","); 
                    $.ajax({
                        url: "{{ route('salarie.multiple-add') }}",
                        type: 'POST',
                        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                        data: {
                            'ids' : strIds},
                        success: function (data) {
                            if (data['status']==true) {
                                $(".checkbox:checked").each(function() { 
                                    alert(strIds); });
                                alert(data['message']);
                            } else {
                                alert('Whoops Something went wrong!!');}
                            window.location.reload()},
                        error: function (data) {
                            alert(data.responseText);}});}  }); });
</script>

function controller addMultiple

   public function addMultiple(Request $request){
        $pointage=new Pointage();

        $pointage->datep=$request->datep;
        $pointage->nbrj=$request->nbrj;
        $pointage->prime=$request->prime;
        $pointage->solde=$request->solde;
     return response()->json(['status'=>true]); 
    }
4
  • What is your main goal here? to save multiple rows to the DB at once or do some validation? Commented Jun 8, 2019 at 7:13
  • save multiple rows to the DB at once Commented Jun 8, 2019 at 8:29
  • did you manage to solve this issue? Commented Jun 8, 2019 at 20:51
  • hey ,I have not yet solved this problem, it is a difficult problem, Commented Jun 9, 2019 at 9:12

1 Answer 1

1

Apologies for late answer laptop died on me while i was busy but one way you could do it is by using array names for example:

 <td><input type="checkbox" class="checkbox" name="row[$key][salarie]" data-id="{{$salarie->id}}"></td>

baiclly if you have multiple of these inputs with the same group it will make an array of inputs on your backend which you can loop through. to test this dd(request()); in your controller function above everything else. then you should be able to see what it returns in your console.

foreach(request(inputgroup) as $value){

   Pointage::create([
      'some_column' => $value['actualInputName']
   ]);
}

Update your function to something like this:

public function addMultiple(Request $request){
    dd(request());

    $pointage=new Pointage();

    foreach(request('row') as $row){
       // this is the important line $row is your request and ['salari'] is the name of the input   
       $pointage->salarie = $row['salarie'];
       $pointage->save();      
     }

     return response()->json(['status'=>true]); 
}
Sign up to request clarification or add additional context in comments.

3 Comments

Maybe I did not know how to use this method
Let me know where you got stuck, best thing to do would be set the array names as iv shown then dd() your request to see how the data is returned. Post your result and I can help further
@NajibMarzouk i have updated my answer to hopefully make it easier to understand

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.