【问题标题】:Laravel 5.1 and Typeahead.jsLaravel 5.1 和 Typeahead.js
【发布时间】:2017-01-03 08:28:57
【问题描述】:

我正在尝试在我的 Laravel 5.1 刀片视图中使用 typeahed.js。我尝试重新创建 The Basics 类型的示例,但在尝试搜索它们时未显示状态。

这里是代码 我的主要布局

<html>
<head>
    <title>App Name - @yield('title')</title>
    {{--<link rel="stylesheet" href="{{ URL::asset('assets/js/search.js') }}">--}}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script type="text/javascript" src="{{ URL::asset('js/bootstrap.min.js') }}"></script>


    {{--<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>--}}


    <script type="text/javascript" src="{{ URL::asset('js/search.js') }}"></script>

    <link href="{{ URL::asset('css/app.css') }}" rel="stylesheet">
    <link href="{{ URL::asset('css/custom.css') }}" rel="stylesheet">

    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

    <script src="https://cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.11.1/typeahead.bundle.min.js"></script>





</head>
<body>
@section('sidebar')

@show

<div class="container">
    @yield('content')
</div>
</body>
</html>

我正在尝试在我的刀片视图中查看它

@extends('layouts.main')



@section('content')

    <div id="the-basics">
        <input class="typeahead" type="text" placeholder="States of USA">
    </div>
@endsection

@section('scripts')
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.11.1/typeahead.bundle.min.js"></script>
    <script>
        var substringMatcher = function(strs) {
            return function findMatches(q, cb) {
                var matches, substringRegex;

                // an array that will be populated with substring matches
                matches = [];

                // regex used to determine if a string contains the substring `q`
                substrRegex = new RegExp(q, 'i');

                // iterate through the pool of strings and for any string that
                // contains the substring `q`, add it to the `matches` array
                $.each(strs, function(i, str) {
                    if (substrRegex.test(str)) {
                        matches.push(str);
                    }
                });

                cb(matches);
            };
        };

        var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
                      'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
                      'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
                      'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
                      'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
                      'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
                      'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
                      'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
                      'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
        ];

        $('#the-basics .typeahead').typeahead({
                                                  hint: true,
                                                  highlight: true,
                                                  minLength: 1
                                              },
                                              {
                                                  name: 'states',
                                                  source: substringMatcher(states)
                                              });
    </script>

@stop

【问题讨论】:

    标签: php jquery laravel-5.1 typeahead.js laravel-blade


    【解决方案1】:

    本周早些时候我也做过同样的事情,我使用Bloodhound 来获取/格式化 ajax 结果。这是我的代码:

    <input type="text" class="input-sm form-control typeahead-city" id="cities">
    

    还有 javascript:

    // define the data source
    var cities = new Bloodhound({
        datumTokenizer: function(datum) {
             return Bloodhound.tokenizers.whitespace;
        },
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: {
            wildcard: '%QUERY', // this is the replacement on the url
            url: '/citysearch/%QUERY',  // this is the url the request should be made to
            transform: function(response) {
                // convert the response to a object that contains the `value` key
                var results = $.map(response["_embedded"]["city:search-results"], function(city) {
                                 return {
                                    value: city.matching_full_name,
                                    full: city
                              };
                          });
                return results;
            }
        }
    });
    
    // Initialize typeahead input with Bloodhound object as the source
    $('.typeahead-city').typeahead(null, {
        display: 'value',
        source: cities,
        highlighter: function(item) {
            return "<img src='/img/marker_search.png'>&nbsp;" + item
        },
    });
    

    我使用了typeahead.bundle.min.js,其中包含寻血猎犬和预输入。

    【讨论】:

    • 感谢回复我发现我的 js 文件在我的 php 页面加载时没有被包含在内。我让它工作。再次感谢所有帮助。
    猜你喜欢
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多