Php Laravel5中的jQuery自动完成不工作
这是基本代码。我似乎无法让它在Laravel 5中工作: routes.phpPhp Laravel5中的jQuery自动完成不工作,php,jquery,json,autocomplete,laravel-5,Php,Jquery,Json,Autocomplete,Laravel 5,这是基本代码。我似乎无法让它在Laravel 5中工作: routes.php Route::get('h2h', 'atp_players\H2hController@getIndex'); Route::get('h2h_getdata', 'atp_players\H2hController@getData'); H2hController.php namespace Atpstats\Http\Controllers\atp_players; use Atpstats\Http\Con
Route::get('h2h', 'atp_players\H2hController@getIndex');
Route::get('h2h_getdata', 'atp_players\H2hController@getData');
H2hController.php
namespace Atpstats\Http\Controllers\atp_players;
use Atpstats\Http\Controllers\Controller;
use Response;
use Request;
class H2hController extends Controller{
public function getIndex() {
return view('atp_players.h2h');
}
public function getData() {
$term = Request::input('auto', 'r');
$results = \DB::table('atp_players')->select('firstname')->get();
$data = array();
foreach($results as $result) {
if(strpos($result,$term) !== false) {
$data[] = ['value' => $result->firstname];
}
}
return Response::json($data);
}
}
视图:h2h.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Autocomplete test</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
</head>
<body>
<div class="container">
<div class="ui-widget">
<label for="">Find a player</label>
<input type="text" class="form-control input-sm" name="auto" id="auto" autocomplete="on">
</div>
<div class="form-group">
<label for="">Response</label>
<input type="text" class="form-control input-sm" name="response" id="response" disabled>
</div>
<script>
$('#auto').autocomplete({
type: "get",
source: 'h2h_getdata',
dataType: "json",
minLength: 1,
select:function(e,ui){
$('#response').val(ui.item.value);
}
});
</script>
</body>
</html>
我认为问题可能出在服务器端代码上。尝试将表单项名称从“自动”更改为“术语”,如下所示:
...
public function getData() {
$term = Request::input('term', 'r');
...
有关更多信息,请查看我认为问题可能出在服务器端代码上。尝试将表单项名称从“自动”更改为“术语”,如下所示:
...
public function getData() {
$term = Request::input('term', 'r');
...
要了解更多信息,请查看嘿,我认为另一个人的想法是正确的。问题在于您的源json。您正在打印格式正确的JSON,它只需要位于正确的密钥对中即可自动完成。根据源属性的Jquery UI自动完成文档,如果要使用数组,则必须使用label:和value:对其进行正确格式化 因此,只需将数据格式化为
public function getData() {
//$term = Request::input('auto', 'r');
$results = [['firstname' => 'Edwardo', 'ID' => '12'], ['firstname' => 'Edwarda', 'ID' => '13']];
//$results = \DB::table('atp_players')->select('firstname')->get();
$data = array();
foreach($results as $result) {
if(strpos($result,$term) !== false) {
$temp = array();
// This is what will be displayed by autocomplete
$temp['label'] = $result->firstname;
// This will be the value you get back through form (eg: User_ID)
$temp['value'] = $result->ID;
array_push($data, $temp);
}
}
return Response::json($data);
}
这里是一个预先填写好的json数组的工作文件
但是你也可以只给它一个值,如果你只想搜索它,它会提交名字作为输入标签值。嘿,我想另一个人是对的。问题在于您的源json。您正在打印格式正确的JSON,它只需要位于正确的密钥对中即可自动完成。根据源属性的Jquery UI自动完成文档,如果要使用数组,则必须使用label:和value:对其进行正确格式化 因此,只需将数据格式化为
public function getData() {
//$term = Request::input('auto', 'r');
$results = [['firstname' => 'Edwardo', 'ID' => '12'], ['firstname' => 'Edwarda', 'ID' => '13']];
//$results = \DB::table('atp_players')->select('firstname')->get();
$data = array();
foreach($results as $result) {
if(strpos($result,$term) !== false) {
$temp = array();
// This is what will be displayed by autocomplete
$temp['label'] = $result->firstname;
// This will be the value you get back through form (eg: User_ID)
$temp['value'] = $result->ID;
array_push($data, $temp);
}
}
return Response::json($data);
}
这里是一个预先填写好的json数组的工作文件
但是你也可以给它一个值,如果你只想搜索它,它会提交名字作为输入标签值