【问题标题】:Laravel 5.1 typeahead.js unable to find any dataLaravel 5.1 typeahead.js 找不到任何数据
【发布时间】:2015-07-23 19:44:13
【问题描述】:

我正在使用 laravel 5.1,我尝试在我的表单中添加一些自动完成功能,它不会返回错误但总是返回找不到任何错误。 我是 javascript 的新手。 请帮我解决它

这是我的模板:

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

这是我的路线:

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

这是我的控制器

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);
    }

}

这是我的 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>'
            ]
        }
    });
});

【问题讨论】:

    标签: javascript php laravel-5 typeahead.js


    【解决方案1】:

    我可以推荐另一个自动完成的 JavaScript 库吗? SELECT2 怎么样。链接在这里。 https://select2.github.io/examples.html 。这很容易实现。我之前使用过 typeahead,我花了一两天的时间才让它工作。

    【讨论】:

    • 感谢 dakine,抱歉我的回复晚了,它的工作方式很迷人,select2 很容易实现,而且风格也更好
    猜你喜欢
    • 2017-01-03
    • 2015-10-27
    • 2016-06-10
    • 2015-11-16
    • 2016-01-14
    • 2015-08-28
    • 1970-01-01
    • 2016-01-05
    • 2015-12-19
    相关资源
    最近更新 更多