【问题标题】:Using typeahead.js with laravel将 typeahead.js 与 laravel 一起使用
【发布时间】:2014-12-20 15:58:58
【问题描述】:

我正在尝试将 twitter typeahead.js 与 laravel 一起使用,但它不起作用。这是我的代码

create.blade.php

<!-- app/views/nerds/create.blade.php -->

<!DOCTYPE html>
<html>
<head>
    <title>Crear Detalle</title>
    <link rel="stylesheet" href="{{ asset('css/bootstrap.min.css') }}">
    <link rel="stylesheet" type="text/css" href="{{ URL::asset('css/main.css') }}" />
    <script type="text/javascript" src="{{ URL::asset('js/jquery-2.1.1.min.js') }}" ></script>
    <script type="text/javascript" src="{{ URL::asset('js/bootstrap.min.js') }}" ></script>
    <script type="text/javascript" src="{{ URL::asset('js/typeahead.js') }}" ></script>
</head>
<script>
$(document).ready(function()
{
    $.ajax({
    type: "POST",
        url: "http://chapincar.dev/notas_detalle/create",
        data: "autocomplete="+$("input[name=autocomplete]").val(),
        success: function(data)
        {
            json = $.parseJSON(data);//parseamos el json
            var codigo = json
            console.log(codigo)

            $('#remote .typeahead').typeahead({
              minLength: 3,
              highlight: true,
            },
            {
              name: 'codigo',
              source: codigo
            });

        },
        error: function(data)
        {
            json = $.parseJSON(data);//parseamos el json
            var error = json
            console.log(error)  
        }
    });
});
</script>
<body>

<div class="container">

<nav class="navbar navbar-inverse">
    <div class="navbar-header">
        <a class="navbar-brand" href="{{ URL::to('nota_detalle') }}">Panel de Detalles de Ordenes</a>
    </div>
    <ul class="nav navbar-nav">
        <li><a href="{{ URL::to('nota_detalle') }}">Ver todos los Detalles</a></li>
        <li><a href="{{ URL::to('nota_detalle/create') }}">Crear un Detalle</a>
    </ul>
</nav>


<h1>Crear Detalle</h1>

<!-- if there are creation errors, they will show here -->
{{ HTML::ul($errors->all() )}}

{{ Form::open(array('url' => 'nota_detalle', 'class' => '')) }}

    <table>
        <tr>
            <td class="ancho">
                <div class="form-group">
                    {{ Form::label('codigo_nota', 'Codigo Orden') }}
                    {{ Form::text('codigo_nota', Input::old('codigo_nota'), array('class' => 'form-control')) }}
                </div>
            </td>
            <td class="ancho">
                <a href="#" class="btn btn-default"
                   data-toggle="modal"
                   data-target="#modalCliente">Buscar</a>
            </td>
        </tr>
        <tr>
            <td class="ancho">
                <div class="form-group">
                    {{ Form::label('cantidad_detalle', 'Cantidad') }}
                    {{ Form::text('cantidad_detalle', Input::old('cantidad_detalle'), array('class' => 'form-control')) }}
                </div>
            </td>

        </tr>
        <tr>
            <td class="ancho">
                <div class="form-group">
                    {{ Form::label('descripcion_detalle', 'Descripción') }}
                    {{ Form::textarea('descripcion_detalle', Input::old('descripcion_detalle'), array('class' => 'form-control')) }}
                </div>
            </td>
        </tr>
        <tr>
            <td class="ancho">
                <div class="form-group">
                    {{ Form::label('precioIVA_detalle', 'Precio con IVA') }}
                    {{ Form::number('precioIVA_detalle', Input::old('precioIVA_detalle'), array('class' => 'form-control')) }}
                </div>
            </td>
        </tr>
        <tr>
            <td class="ancho">
                <div class="form-group">
                    {{ Form::label('precioSinIVA_detalle', 'Precio sin IVA') }}
                    {{ Form::number('precioSinIVA_detalle', null, array('class' => 'form-control', 'size' => '30x4')) }}
                </div>
            </td>
        </tr>
        <tr>
            <td class="ancho">
                <div class="form-group">
                    {{ Form::label('precioTotal_detalle', 'Precio Total') }}
                    {{ Form::number('precioTotal_detalle', null, array('class' => 'form-control')) }}
                </div>
            </td>
        </tr>
    </table>

    {{ Form::submit('Agregar Detalle!', array('class' => 'btn btn-primary')) }}

{{ Form::close() }}

<!-- Modal -->
<div class="modal fade" id="modalCliente" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&amp;times;</button>
                <h4 class="modal-title" id="myModalLabel">Modal title</h4>
            </div>
            <div class="modal-body">
                 <div class="form-group remote">
                    <label for="query">Search:</label>
                    <input class="form-control" name="autocomplete" id="autocomplete" placeholder="Start typing something to search..." type="text">
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal" aria-hidden="true">Cerrar</button>

            </div>
        </div>
  </div>
</div>
{{Form::close()}}
</div>
</body>
</html

>

这是我的 routes.php 文件

 Route::post("nota_detalle/create", function(){
    $search = Input::get("autocomplete");
    $query = DB::table("notas_cabecera")->where('chapa_vehiculo', 'LIKE', '%'.$search.'%')->get(['codigo_nota']);
    Response::json($query);
});

这些是我的错误日志:

这是我的数据库:

https://www.dropbox.com/s/1li3hhylw7ukbv0/Imagen%202.png?dl=0

你能帮帮我吗

【问题讨论】:

  • 可能有很多事情你尝试过调试它吗?,服务器端你收到query=...而不是typeahead=...,还有你的jQuery error: function(data) { console.log(data); ...在哪里?

标签: php jquery laravel-4 typeahead.js


【解决方案1】:

因此,根据@Loz Cherone 在您的 laravel 路由器中服务器端的评论,参数是 query。 另一件事是你手动使用 echo json_encode,不要使用这个使用内置的 laravel 函数return Response::json($query); 它有点干净,你不想在你的路由文件中使用 echo。 有关 laravel 响应的更多信息 - http://laravel.com/docs/4.2/responses#special-responses

如果您有更多问题,请检查浏览器上的控制台,以帮助我们了解更多详细信息。

【讨论】:

  • 我改了代码,但是ir也不起作用,请你帮忙,我把错误日志放在最上面
  • 你使用的是哪个版本的 laravel?你做过composer update吗?
  • 我已经这样做了。我用的是4.3.16版本的laravel