Как отобразить данные, полученные с помощью AJAX? - PullRequest
0 голосов
/ 07 марта 2020

У меня есть две формы: form1 содержит month, year, salarie_id и chantier_id, переключатели 1st Fortnight или 2nd Fortnight и кнопку cherche (поиск), которая позволяет восстановить данные, а также отображать форму2 и скрыть форму1. Form2 содержит таблицу, которую я хочу заполнить данными, которые были восстановлены AJAX.

Мой вопрос: как я могу отобразить эти данные в таблице form2?

payer.blade. php

Form1

<div class=" col-md-10 col-md-offset-1">
    <div class="form-group col-md-3">
        <label for="titre">Annee</label>
    </div>
    <div class="form-group col-md-5">
        <input type="text" name="annee" id="annee" class="form-control">
    </div>
</div>

<div class=" col-md-10 col-md-offset-1">
    <div class="form-group col-md-3">
        <label for="titre">Mois</label>
    </div>
    <div class="form-group col-md-5">
        <input type="text" name="mois" id="mois" class="form-control">
    </div>
</div>

<div class=" col-md-10 col-md-offset-1 ">
    <div class="form-group col-md-4">
        <label for="titre">periode</label>
    </div>
    <div class="form-check form-check-inline col-md-3">
        <input type="radio" class="form-check-input" value="1" name="quanzaime">
        <label class="form-check-label" for="q1">1er quanzaime</label>
    </div>
    <div class="form-check form-check-inline col-md-3">
        <input type="radio" class="form-check-input" value="2" name="quanzaime">
        <label class="form-check-label" for="q2">2eme quanzaime</label>
    </div>
</div>

<div class=" col-md-10 col-md-offset-1 ">
    <div class="form-group col-md-3">
        <label for="titre">Salarie</label>
    </div>
    <div class="form-group col-md-5">
        <select class="form-control" id="salarie_id" name="salarie_id">
            <option selected disabled>Select salarie</option>
            @foreach($salaries as $salarie)
            <option value="{{ $salarie->id }}">{{ $salarie->nom}} {{ $salarie->prenom }}---{{ $salarie->id }}</option>
            @endforeach
        </select>
    </div>
</div>
<div class=" col-md-10 col-md-offset-1 ">
    <div class="form-group col-md-3">
        <label for="titre">Chantier</label>
    </div>
    <div class="form-group col-md-5">
        <select class="form-control" id="chantier_id" name="chantier_id">
            <option selected disabled>Select Chantier</option>
            @foreach($chantiers as $chantier)
            <option value="{{ $chantier->id }}">{{ $chantier->chantier}}</option>
            @endforeach
        </select>
    </div>
</div>

<div class="col-md-2 col-md-offset-5">
    <button class="btn btn-theme " type="submit" id="hide">cherche</button>
</div>
</div>

form2

<table id="example" class="table table-striped table-bordered" style="width:100%">
    <thead>
    <tr>
        <th><input type="checkbox" id="check_all"></th>
        <th>Anne</th>
        <th>mois</th>
        <th>matricule</th>
        <th>prenom</th>
        <th>salaire Net</th>
        <th>sold</th>
        <th>edit</th>
        <th>delete</th>
        <th>details</th>
    </tr>
    </thead>
    <tbody>
    @foreach($dataP $salarie)
    <tr id="{{$salarie->id}}">
        <td><input type="checkbox" class="checkbox" name="customer_id[]" value="{{$salarie->id}}"/></td>
        <td><input type="hidden" class='year' class="form-control"/></td>
        <td><input type="hidden" class='month' class="form-control"/></td>
        <td>{{ $salarie->id }}</td>
        <td>{{ $salarie->nom }}</td>
        <td>{{ $salarie->prenom }}</td>
        <td>{{ $salarie->sold }}</td>
        <td><a href="{{ url('salaries/'.$salarie->id.'/edit') }}" class="btn btn-theme03 btn-sm">Editer</a></td>
        <form action="{{ url('salaries', ['id' => $salarie->id]) }}" method="post">
            {{ method_field('DELETE') }}
            {{ csrf_field() }}
            <td><button type="submit" class="btn btn-theme04 btn-sm" role="button">Delete</button></td>
        </form>
        <td><a href="{{ url('salaries/'.$salarie->id) }}" class="btn btn-theme02 btn-sm" role="button">Show</a></td>
    </tr>
    @endforeach
    </tbody>
</table>

jQuery

<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);
            }
        });

        $("#form2").hide();
        let now = new Date();
        let year = now.getFullYear();
        let month = ("0" + (now.getMonth() + 1)).slice(-2);
        $('#annee').val(year);
        $('#mois').val(month);


        $("#hide").click(function () {
            $("#form1").hide();
            $("#form2").show();
            let chan = $("#chantier_id option:selected").text();
            $('#ch').text(chan);
            let sal = $("#salarie_id option:selected").text();
            $('#sa').text(sal);
            let ann = $("#annee").val();
            $('#an').text(ann);
            let moi = $("#mois").val();
            $('#mo').text(moi);
            $('.year').text(ann);
            let chan_id = $("#chantier_id option:selected").val();
            let sal_id = $("#salarie_id option:selected").val();
            $('.month').val(mois).parent().prepend(moi);
            $('.year').val(ann).parent().prepend(ann);
            var radioValue = $("input[name='quanzaime']:checked").val();
            if (radioValue) {
                console.log(radioValue);
            }
            $.ajax({
                type: 'get',
                url: "{{ route('salarie.payerP') }}",
                data: {
                    'quanzaime': radioValue,
                    'annee': ann,
                    'mois': moi,
                    'salarie_id': sal_id,
                    'chantier_id': chan_id
                },
                success: function (data1) {
                    console.log(data1);
                },
                error: function () {
                    console.log('error');
                },
            });
        });
    });
</script>

SalarieController. php

public function payer(){

    $pointages = Pointage::get();
    $salaries = Salarie::get();
    $chantiers = Chantier::get();
    return view('salarie.payer', compact('salaries','pointages','chantiers'));
  //dd($request);
}

 public function payerP(request $request){ 
  if($request->quanzaime == 1){
    $dataP = DB::table('salaries')
     ->join('pointages','pointages.salarie_id','salaries.id')
      ->selectRaw('SUM(pointages.sold) as sold,salaries.nom,salaries.prenom,salaries.id')
      ->whereRaw(DB::raw('YEAR(pointages.datep) = ' .$request->annee))
      ->whereRaw(DB::raw('MONTH(pointages.datep) = ' .$request->mois))
      ->whereRaw( DB::raw('DAY(pointages.datep)>0 AND DAY(pointages.datep)<16')) 
      ->Orwhere('pointages.salarie_id','=',$request->salarie_id)->OrWhere('pointages.chantier_id','=',$request->chantier_id)
      ->groupBy('pointages.salarie_id')
      ->get();
      return response()->json(['salarie'=>$dataP]);

    }elseif($request->quanzaime == 2){
      $dataP = DB::table('salaries')
     ->join('pointages','pointages.salarie_id','salaries.id')
      ->selectRaw('SUM(pointages.sold) as sold,salaries.nom,salaries.prenom,salaries.id')
      ->whereRaw(DB::raw('YEAR(pointages.datep) = ' .$request->annee))
      ->whereRaw(DB::raw('MONTH(pointages.datep) = ' .$request->mois))
      ->whereRaw( DB::raw('DAY(pointages.datep)>15 AND DAY(pointages.datep)<31')) 
      ->Orwhere('pointages.salarie_id','=',$request->salarie_id)->OrWhere('pointages.chantier_id','=',$request->chantier_id)
      ->groupBy('pointages.salarie_id')
      ->get();
      return response()->json(['salarie'=>$dataP]);

    }else{ 
      $dataP = DB::table('salaries')
     ->join('pointages','pointages.salarie_id','salaries.id')
      ->selectRaw('SUM(pointages.sold) as sold,salaries.nom,salaries.prenom,salaries.id')
      ->whereRaw(DB::raw('YEAR(pointages.datep) = ' .$request->annee))
      ->whereRaw(DB::raw('MONTH(pointages.datep) = ' .$request->mois))
      ->Orwhere('pointages.salarie_id','=',$request->salarie_id)->OrWhere('pointages.chantier_id','=',$request->chantier_id)
      ->groupBy('pointages.salarie_id')
      ->get();
      return response()->json(['salarie'=>$dataP]);

    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...