Jraery Autocomplete与Laravel 4无法正常工作

时间:2015-01-02 12:17:43

标签: php jquery-ui laravel

我正在使用Laravel 4.2.13进行jquery自动完成。我正在尝试在boostrap模式中搜索表单中的值。我正在传递获取值并正确接收响应,但文本输入未填充值。 这是我的create.blade.php视图

<!DOCTYPE html>
<html>
<head>
    <title>Crear Detalle</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="//codeorigin.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="//codeorigin.jquery.com/ui/1.10.2/jquery-ui.min.js"></script>
    <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/bootstrap.min.js') }}" ></script>

</head>

<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>
                <?= Form::open(); ?>
                <?= Form::label('auto', 'Escriba el Numero de Chapa: '); ?>
                <?= Form::text('auto', '', array('id' => 'auto')); ?>
                <br />
                <?= Form::label('response', 'Codigo de la Orden: '); ?>
                <?= Form::text('response', '', array('id' =>'response', 'disabled' => 'disabled')); ?>
                <?= Form::close(); ?>
                <button type="submit" class="btn btn-primary">Search</button>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal" aria-hidden="true">Cerrar</button>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function() {
        $("#auto").autocomplete({
            source: "create/getdata",
            minLength: 1,
            select: function( event, ui ) {
            $('#response').val(ui.item.id);
            }
        });
    });
</script>
</body>
</html>

这是我的路线档案:

Route::get('nota_detalle/create/getdata', 'SearchController@index');

这是我的SearchController文件

<?php

class SearchController extends BaseController {

    public function index()
    {
        $term = Str::upper(Input::get('term'));

        $results = NotaCabecera::select("codigo_nota", "chapa_vehiculo")->where('chapa_vehiculo', 'LIKE', '%'.$term.'%')->get();
        //dd($results);

        $data = array();
        foreach ($results as $result) :
            //$data[] = $result->codigo_nota.' '.$result->chapa_vehiculo;
            $data[] = array('value' => $result->chapa_vehiculo, 'id' => $result->codigo_nota);
        endforeach;

        return Response::json($data);
    }


}

这是我的模式:

enter image description here

这些是我的日志:

enter image description here

enter image description here

有什么问题?还有一个问题,为什么它使用PHP语法它不一样有什么区别? (我按照教程进行了操作)。

谢谢。

2 个答案:

答案 0 :(得分:0)

这个例子肯定对我有用。

&#13;
&#13;
// Javascript

<script type="text/javascript">
$(function(){
 $("#auto").keyup(function(){
$("#auto").autocomplete({
source:"{{URL('getdata')}}",
minLength: 3
});
$("#auto").autocomplete("widget").height(200);
 });
});
</script>
&#13;
   // view

<h2>Laravel Autocomplete form Database data</h2>
{{ Form::open() }}

{{ Form::label('auto', 'Find a color: ') }}
{{ Form::text('auto', '', array('id' => 'auto'))
}}
{{form::submit('Search', array('class' => 'button expand'))}}
{{ Form::close() }}



// routes.php



Route::any('getdata', function()
{
$term = Input::get('term');
$data = DB::table("words")->where('word', 'LIKE', $term.'%')->get();
$return_array = array();
foreach ($data as $v) {
}
return Response::json(array('value' => $v->word ));
});
&#13;
&#13;
&#13;

Github:Github repo

答案 1 :(得分:0)

使用值对所选字段进行别名:

$results = NotaCabecera::select("codigo_nota as value", "chapa_vehiculo")->where('chapa_vehiculo', 'LIKE', '%'.$term.'%')->get();