简体   繁体   English

Laravel 5-数据库提前输入

[英]Laravel 5 - Typeahead from database

I'm trying to integrate Typeahead into a project in Laravel 5. I don't understand much about JQuery. 我正在尝试将Typeahead集成到Laravel 5中的项目中。我对JQuery不太了解。 I used static data and this works but I want to use dinamic data from Database. 我使用了静态数据,并且可以使用,但是我想使用数据库中的动态数据。 Then, I need to change in JQuery the parameter "source" and maybe something more. 然后,我需要在JQuery中更改参数“ source”,也许还需要更多。 I saw some examples but none of them work, and I have not been able to understand them correctly. 我看到了一些示例,但没有一个起作用,而且我无法正确理解它们。 Any suggestions? 有什么建议么? Thanks. 谢谢。

routes/web 路线/网站

Route::get('partials/buscador', ['as'=>'search','uses'=>'SearchController@search']);

Controller 控制者

class SearchController extends Controller
{
    public function search(Request $request) {
        $data = LangNoticia::where("title","LIKE","%{$request->input('query')}%")->get();
        return response()->json($data);
    }
}

View (the view is a partial one that is called in all the pages) 视图 (视图是在所有页面中调用的局部视图)

          <div class="typeahead__container">
                <div class="typeahead__field">

                    <span class="typeahead__query">
                        <input class="search_input" name="search" placeholder="{!!__('header.buscar') !!}" autocomplete="off" type="search">
                    </span>

                    <span class="typeahead__button">
                        {!! HTML::image('images/web/icons/lupa.svg', 'buscar',  array('height' => '30', 'class' => 'buscar') ) !!}
                    </span>

                </div>
            </div>

JavaScript 的JavaScript

    $(document).ready(function(){

    var src = "{{ route('search') }}";

    $.typeahead({
        input: '.search_input',
        minLength: 1,
        maxItem: 8,
        maxItemPerGroup: 6,
        order: "asc",
        cache: true,

        /* source -> This I need to change to dinamic data from database (see controller) */

        source: {
            data: [
            "One",
            "Two",
            "Three"
        ]
        },

        callback: {
            onInit: function (node) {
                console.log('Typeahead Initiated on ' + node.selector);
            }
        }
    });

});

Try this 尝试这个

Controller 控制者

class SearchController extends Controller
{
    public function search(Request $request) {
        $data = LangNoticia::where("title","LIKE","%{$request->text}%")->pluck("title");
        return response($data->all());
    }
}

Javascript Java脚本

$(document).ready(function(){

    var src = "partials/buscador";

    $.typeahead({
        input: '.search_input',
        minLength: 1,
        maxItem: 8,
        maxItemPerGroup: 6,
        order: "asc",
        cache: true,

        /* source -> This I need to change to dinamic data from database (see controller) */
        source: {
            data: $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                url: src,
                method: 'GET',
                dataType: 'json',
                data: {
                    text: $('.search_input').val()
                },
                success: function(data){
                    return data;
                }
            });
        },

        callback: {
            onInit: function (node) {
                console.log('Typeahead Initiated on ' + node.selector);
            }
        }
    });

});

Finally, I have made it work in a different way, in the absence of showing the results when the form is submitted: 最后,在提交表单时没有显示结果,我以不同的方式使它工作:

Laravel - search with typeahead: show the results on submit Laravel-使用typeahead搜索:提交时显示结果

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

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