jeudi 18 juin 2020

Multiple Live Search Laravel using AJAX

Can someone help me to find out what's wrong with my code?

I'm trying to search in 2 forms using AJAX

here is my code

Blade

$(document).ready(function(){

 load_data();

 function load_data(orderno = '', jobno= '')
 {
  $.ajax({
   url:"",
   method:'GET',
   data:{orderno:orderno,jobno:jobno},
   dataType:'json',
   success:function(data)
   {
    $('tbody').html(data.table_data);
    $('#total_records').text(data.total_data);
   }
  })
 }

 $(document).on('keyup', '#search', function(){
  var orderno = $(this).val();
  var jobno = $(this).val();
  load_data(orderno,jobno);
 });


 $(document).on('keyup', '#searchkanban', function(){
  var orderno = $(this).val();
  var jobno = $(this).val();
  load_data(orderno,jobno);
 });



});
</script> 

and this is my Controller

    {
      if($request->ajax())
      {
          $output = '';
          $query = $request->get('orderno');
          $query2 = $request->get('jobno');


          if($query != '' && $query2 == '')
          {
            $data = DB::table('scankanban')
              ->where('sk_orderNo', 'like', '%'.$query.'%')

              ->orderBy('id', 'asc')
              ->get();

          }

          else if($query != '' && $query2 != '')
          {
            $data = DB::table('scankanban')
              ->where('sk_orderNo', 'like', '%'.$query.'%')
              ->Where('sk_jobNo', 'like', '%'.$query2.'%')

              ->orderBy('id', 'asc')
              ->get();

          } 
          else // get all if not found
          {
            $data = DB::table('scankanban')
              ->orderBy('id', 'asc')
              ->get();
          }

          $total_row = $data->count();

          if($total_row > 0)
          {
              foreach($data as $row)
              {
                $output .= '
                <tr>
                <td>'.$row->sk_orderNo.'</td>
                <td>'.$row->sk_jobNo.'</td>
                <td>'.$row->sk_orderKanban.'</td>
                <td>'.$row->sk_scanID.'</td>
                <td>'.$row->sk_status.'</td>
                </tr>
                ';
              }
          }
          else
          {
              $output = '
              <tr>
                <td align="center" colspan="5">No Data Found</td>
              </tr>
              ';
          }
          $data = array(
              'table_data'  => $output,
              'total_data'  => $total_row
          );

          echo json_encode($data);
        }
    } 
  1. I can get all data it means the else code is working
  2. When I type in form #search, I can get query search.
  3. When I typed in form #search and form #searchkanban, the result was empty table like not found. Sometimes get data but wrong data. I updated this code from https://www.webslesson.info/2018/04/live-search-in-laravel-using-ajax.html. Before only use 1 search form, now I want to use 2 search forms.

Thanks in advance



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

Aucun commentaire:

Enregistrer un commentaire