你好,当我在文本框中搜索数据时,为什么会重复?
这是我的Ajax代码。请检查此代码,因为它是重复代码的问题。另外,请确保其语法正确。
$('#search').on('keyup',function(){
$value=$(this).val();
$.ajax({
type : 'get',
url : '{{ URL::to('admin/employeemaintenance/search') }}',
data : {'search':$value},
success:function(data){
$('tbody').html(data);
}
});
})
我的控制器
请检查此代码,它可能有问题,我可能有一些巨大的数据字段,这里仍然不包括用于更新和删除的按钮:
public function search(Request $request){
if($request->ajax())
{
$output="";
$employees = DB::table('employeefms')->where('last_name','LIKE','%'.$request->search.'%')
->orWhere('first_name','LIKE','%'.$request->search.'%')->get();
if($employees)
{
foreach($employees as $key => $employee){
if($employee->gender==0)
{
$gender="Male";
}
else
{
$gender="Female";
}
$output.='<tr>'.
'<td>'.$employee->employee_no.'</td>'.
'<td>'.$employee->last_name.'</td>'.
'<td>'.$employee->first_name.'</td>'.
'<td>'.$employee->middle_name.'</td>'.
'<td>'.$employee->nick_name.'</td>'.
'<td>'.$gender.'</td>'.
'<td>'.$employee->birthdate.'</td>'.
'<td>'.$employee->age.'</td>'.
'<td>'.$employee->birthplace.'</td>'.
'<td>'.$employee->province.'</td>'.
'<td>'.$employee->doMarriage.'</td>'.
'<td>'.$employee->height.'</td>'.
'<td>'.$employee->weight.'</td>'.
'<td>'.$employee->bloodtype.'</td>'.
'<td>'.$employee->nationality.'</td>'.
'<td>'.$employee->religion.'</td>'.
'<td>'.$employee->civil_stats.'</td>'.
'<td>'.$employee->sss_no.'</td>'.
'<td>'.$employee->tin_id.'</td>'.
'<td>'.$employee->phil_no.'</td>'.
'<td>'.$employee->pagibig_no.'</td>'.
'<td>'.$employee->address_no.'</td>'.
'<td>'.$employee->street_no.'</td>'.
'<td>'.$employee->brgy.'</td>'.
'<td>'.$employee->municipality.'</td>'.
'<td>'.$employee->cur_province.'</td>'.
'<td>'.$employee->region.'</td>'.
'<td>'.$employee->zipcode.'</td>'.
'<td>'.$employee->per_address_no.'</td>'.
'<td>'.$employee->per_street_no.'</td>'.
'<td>'.$employee->per_brgy.'</td>'.
'<td>'.$employee->per_municipality.'</td>'.
'<td>'.$employee->per_province.'</td>'.
'<td>'.$employee->per_region.'</td>'.
'<td>'.$employee->per_zipcode.'</td>'.
'<td>'.$employee->mobile_no.'</td>'.
'<td>'.$employee->tel_no.'</td>'.
'<td>'.$employee->email_ad.'</td>'.
'<td>'.$employee->guard_name.'</td>'.
'<td>'.$employee->guard_add.'</td>'.
'<td>'.$employee->guard_relat.'</td>'.
'<td>'.$employee->grd_mobile_no.'</td>'.
'<a href="/admin/employeemaintenance/'.$employee->id.'/edit" class="btn btn-primary btn-sm"><i class="fa fa-edit"></i>'.
'</tr>';
}
return Response($output);
}
}
}
我的搜索文本框
<input type="text" name="search" id="search" class="form-control">
答案 0 :(得分:0)
不要将html放在laravel控制器中,您可以将$ employees作为数据返回,然后在ajax成功操作中添加html
答案 1 :(得分:-1)
public function search(Request $request){
if($request->ajax())
{
$employees = DB::table('employeefms')->where('last_name','LIKE','%'.$request->search.'%')
->orWhere('first_name','LIKE','%'.$request->search.'%')->get();
if(!empty($employees))
{
return json_encode(array("msg"=>"success", "data"=>$employees));
}
return json_encode(array("msg"=>"error"));
}
}
\\\\\\\\\\ Ajax
$.ajax({
type : 'get',
url : '{{ URL::to('admin/employeemaintenance/search') }}',
data : {'search':$value},
success:function(data){
var data1 = jQuery.parseJSON(data);
if(data1.msg == "success"){
$.each(eval(data1.data), function(){
//html here
})
},
//no data found
}
});