mercredi 27 mars 2019

Why the if condition does not filtering the results?

I want to create advance search filtering staff data according to certain criteria chosed by users. This application developed using laravel 5. I am querying the data using ajax function and if statement to filter the criteria. The results appear but it does not filter any condition in the if statement.

The controller of the filtering condition is this:

  public function kakitangan(Request $request)
  {
    $query = DB::table('itemregistrations')
             ->select('itemregistrations.ItemRegistrationID','itemregistrations.name', 'itemregistrations.Nobadan');

    if ($request->section != ""){
        $query->where('SectionID', $request->section);
    }

    $newitem = $query->get();

    return response::json($newitem);
  }

I also have tried this:

    $query = DB::table('itemregistrations')
             ->select('itemregistrations.ItemRegistrationID','itemregistrations.name', 'itemregistrations.Nobadan');

    if(request('section')) {
        $query->where('SectionID', request('section'));
    }
  $newitem = $query->get();

  return response::json($newitem);

But the result is the same..all data in itemregistrations appear in the output page. Although I select another section criteria.

This is the view page code for selection:

   <div class="form-group">
            <div class="row">
              <div class="col-lg-2"> 
                
              </div>
              <div class="col-lg-2"> 
                
              </div>
            </div>
          </div>

Button to call ajax function to get the query:

   <button class="btn btn-primary btn-md" id="cari">Cari</button>

The code for results appear:

   <script type="text/javascript">
      $( "#cari" ).click(function() {

        var seksyen = $("#section").val();
         $.ajax({
            url: '',
            data: {'section': seksyen},
            dataType: 'json',
            success: function (data) {
                console.log(data);
                $('#datatable tr').not(':first').not(':last').remove();
                var html = '';
                for(var i = 0; i < data.length; i++){
                    html += '<tr>'+
                                '<td>' + data[i].name + '</td>' +
                            '</tr>';
                    }   
                $('#datatable tr').first().after(html);
            },
            error: function (data) {
            }
        });
      });
   </script>

Should be when the user select a section, only staffs of the section appear. But now all staffs appear when select any section.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2TAB5Ty
via IFTTT

Aucun commentaire:

Enregistrer un commentaire