jeudi 6 décembre 2018

Laravel - Ajax live search duplicating data

my problem is the data duplicates when i added ajax live search searching or not, the data is duplicated why?

On my Controller i have this code, please check i think this is where the data is duplicated or am i wrong ?

function action(Request $request)
{
 if($request->ajax())
 {
  $output = '';
  $query = $request->get('query');
  if($query != '')
  {
   $data = DB::table('employeefms')
     ->where('last_name', 'like', '%'.$query.'%')
     ->orWhere('first_name', 'like', '%'.$query.'%')
     ->orWhere('employee_no', 'like', '%'.$query.'%')
     ->get();

  }
  else
  {
   $data = DB::table('employeefms')
     ->orderBy('last_name', 'desc')
     ->get();
  }
  $total_row = $data->count();
  if($total_row > 0)
  {
   foreach($data as $row)
   {
    $output .= '
    <tr>
     <td>'.$row->employee_no.'</td>
     <td>'.$row->last_name.'</td>
     <td>'.$row->first_name.'</td>
     <td>'.$row->middle_name.'</td>
     <td>'.$row->nick_name.'</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);
 }
}

this is my Ajax Code

can you also please check this ajax what if there is something that is triggering why it is duplicating?

<script type="text/javascript">

    $(document).ready(function(){

    fetch_customer_data();

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

    $(document).on('keyup', '#search', function(){
    var query = $(this).val();
    fetch_customer_data(query);
    });
    });

</script>

my Routes

Route::get('/admin/employeemaintenance/show/action', 'Admin\EmployeeFilemController@action')->name('admin.employeemaintenance.show.action');

my View Table

please see here if i need to add something here

 @foreach ($employees as $employee)
    <tbody>
      <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>



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

Aucun commentaire:

Enregistrer un commentaire