这是基本代码。我似乎无法让它在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'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>
它不起作用。当您向输入文本大小写一些内容时,它就是无所事事事件。但是,如果我在Controller中注释一些行,则会显示DB的数据(名字),但自动完成不起作用。
这就是修改后的控制器功能:
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);
}
我认为问题可能出在服务器端代码上。尝试将表单项名称从"auto"更改为"term",如下所示:
...
public function getData() {
$term = Request::input('term', 'r');
...
查看更多信息http://api.jqueryui.com/1.10/autocomplete/#option-源
嘿,我觉得另一个人走对了路。问题在于您的源json。您正在打印格式正确的JSON,它只需要位于正确的密钥对中即可自动完成。根据source
属性的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数组的工作小提琴https://jsfiddle.net/7u73czt8/
但如果你只想搜索它,你也可以只给它一个值,它会提交第一个名字作为输入标签值。