你好,我正在用ajax和mysql在laravel中开发一个带有livesearch的登录应用程序,但是当我试图显示我的数据库表时,我总是得到一个空的tableview。经过一些调试,我发现程序停止了 if($request->ajax())
所以里面没有交流 if($request->ajax())
括号。
这是我的密码:
查看:
<body>
<br />
<div class="container box">
<h3 align="center">Recherche de PDR</h3>
<br /> @if(isset(Auth::user()->email))
....
<br />
<div class="panel panel-default">
<div class="panel-heading">Recherche de PDR</div>
<div class="panel-body">
<input type="text" name="search" id="search" class="form-control" placeholder="Recherche PDR" />
<div class="table-responsive">
<h3 align="center">Total PDR : <span id="total_records"></span></h3>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Pièce</th>
<th>ID-Pièce</th>
<th>Status</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
fetch_pdr_data();
function fetch_pdr_data(query = '')
{
$.ajax({
url:"{{ route('recherche.action') }}",
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_pdr_data(query);
});
});
</script>
控制器:
<?php
namespace App\Http\Controllers;
use App\Pdrs;
use DB;
use Illuminate\Http\Request;
class PDRController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('recherche.index');
}
function action(Request $request)
{//working
if($request->ajax())
{//not working
$query = $request->get('query');
if($query != '')
{
$data = DB::table('pdrs')
->where('Désignation', 'like', '%'.$query.'%')
->orWhere('Status', 'like', '%'.$query.'%')
->get();
}
else
{
$data = DB::table('pdrs')
->orderBy('ID-Pièce', 'desc')
->get();
}
$total_row = $data->count();
if($total_row > 0)
{
foreach($data as $row)
{
$output .= '
<tr>
<td>'.$row->Désignation.'</td>
<td>'.$row->ID-Pièce.'</td>
<td>'.$row->Status.'</td>
</tr>
';
}
}
else
{
$output = '
<tr>
<td align="center" colspan="5">No Data Found</td>
</tr>
';
}
$data = array(
'table_data' => $output,
'total_data' => $total_data
);
echo json_encode($data);
}
}
这是我的web.php:
Route::get('/recherche', 'PDRController@index');
Route::get('/recherche/action', 'PDRController@action')->name('recherche.action');
暂无答案!
目前还没有任何答案,快来回答吧!