【问题标题】:jQuery Autocomplete in Laravel5 not workingLaravel5 中的 jQuery 自动完成功能不起作用
【发布时间】:2015-04-30 15:20:18
【问题描述】:

这是基本代码。我似乎无法让它在 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 的数据(名字),但自动完成功能不起作用。

这就是修改后的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);
}

【问题讨论】:

    标签: php jquery json autocomplete laravel-5


    【解决方案1】:

    我认为问题可能出在服务器端代码上。尝试将表单项名称从“auto”更改为“term”,如下所示:

    ...
    public function getData() {
    $term =  Request::input('term', 'r');
    ...
    

    更多信息请查看http://api.jqueryui.com/1.10/autocomplete/#option-source

    【讨论】:

      【解决方案2】:

      嘿,我认为另一个人是在正确的轨道上。问题在于您的源 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/

      但如果你只想搜索它,你也可以只给它一个值,它会将名字作为输入标签值提交。

      【讨论】:

        猜你喜欢
        • 2014-01-16
        • 2016-05-03
        • 1970-01-01
        • 2015-08-11
        • 1970-01-01
        • 1970-01-01
        • 2016-01-12
        相关资源
        最近更新 更多