duanqinjiao5244 2015-07-23 19:44
浏览 62
已采纳

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

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. i'm very new in 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

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>'
            ]
        }
    });
});
  • 写回答

1条回答 默认 最新

  • dongweng6241 2015-07-23 21:02
    关注

    Can I suggest another autocomplete javascript library. How about SELECT2. here is the link. 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?