简体   繁体   English

Laravel 5.1 typeahead.js无法找到任何数据

[英]Laravel 5.1 typeahead.js unable to find any data

i'm using laravel 5.1 and i try to add some autocomplete into my form, it is not return an error but it's always return unable to find any. 我正在使用laravel 5.1,我尝试将一些自动完成功能添加到表单中,它不会返回错误,但始终会返回无法找到的任何内容。 i'm very new in javascript. 我在javascript中非常新。 please help me to fix it 请帮我解决

here is my template: 这是我的模板:

{!! Form::label('Name', 'Name:') !!}
{!! Form::text('name',null,['class'=>'form-control', 'id'=>'users']) !!}

here is my route : 这是我的路线:

Route::get('/', 'SearchController@index');
Route::get('/query','SearchController@query');

here is my controller 这是我的控制器

class SearchController extends Controller {

    public function index() {
        return View::make('template');
    }

    public function query() {
        $query = Input::get('name');
        $res = User::where('name', 'LIKE', "%$query%")->get();
        return Response::json($res);
    }

}

here is my main.js 这是我的main.js

jQuery(document).ready(function($) {
    var sugest = new Bloodhound({
        remote: '/bookstore/query?name=%QUERY%',
        // '...' = displayKey: '...'
        datumTokenizer: Bloodhound.tokenizers.whitespace('name'),
        queryTokenizer: Bloodhound.tokenizers.whitespace
    });

    sugest.initialize();

    $("#users").typeahead({
        hint: true,
        highlight: true,
        minLength: 2
    }, {
        source: sugest.ttAdapter(),
        name: 'engine',
        displayKey: 'name',
        templates: {
            empty: [
                '<div class="empty-message">unable to find any</div>'
            ]
        }
    });
});

Can I suggest another autocomplete javascript library. 我可以建议另一个自动完成的JavaScript库。 How about SELECT2. SELECT2怎么样。 here is the link. 链接在这里。 https://select2.github.io/examples.html . https://select2.github.io/examples.html it's easy to implement. 这很容易实现。 I used typeahead before and it took me a day or two to get it work. 我以前用过typeahead,花了我一两天的时间才能开始工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM