[英]jQuery Autocomplete in Laravel5 not working
这是基本代码。 我似乎无法在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中注释了几行,则会显示数据库的数据(名字),但自动完成功能不起作用。
这就是修改后的Controller函数:
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-source
嘿,我认为另一个人在正确的轨道上。 问题出在您的源json。 您正在打印格式正确的JSON,只需将其放在正确的密钥对中即可自动完成。 根据source
属性的Jquery UI自动完成文档,如果您要使用数组,则必须使用label:
和value:
正确设置其格式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/
但是,如果您只想搜索它,也可以只给它一个值,它将提交名字作为输入标签值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.