Búsqueda instantánea en Laravel


Buscador instantáneo en Laravel:

resources\views\welcome.blade.php

@extends('adminlte::page')

@section('title''Buscador instantaneo')

@section('content_header')
    <h1>Buscador instantaneo</h1>
@stop

@section('content')
    <div class="container">
        <div class="col-8">
            <div class="input-group mb-3">
                <input type="text" class="form-control" id="texto" placeholder="Ingrese...">
                <div class="input-group-append">
                    <span class="input-group-text">Buscar</span>
                </div>
            </div>
            <div id="resultados" class="bg-light border"></div>
        </div>
        <div class="col-8 bg-light border" id="contenedor">
            @if(!isset($texto))
                @include('nombres.paginas')
            @endif
        </div>
        <div id="cargando" hidden><h1>CARGANDO...</h1></div>
    </div>
@stop

@section('css')
    <link rel="stylesheet" href="/css/admin_custom.css">
@stop

@section('js')
<script>
    window.addEventListener('load'function(){
        document.getElementById("texto").addEventListener("keyup", () => {
            var x = document.getElementById("contenedor");
            if((document.getElementById("texto").value.length)>=1) {
                x.style.display = "none";
                fetch(`/nombres/buscador?texto=${document.getElementById("texto").value}`, {
                    method:'get'
                })
                .then(response => response.text())
                .then(html => {document.getElementById("resultados").innerHTML = html })
            } else {
                x.style.display = "block";
                document.getElementById("resultados").innerHTML = ""
            }
        }) 
    });
</script>
@stop

resources\views\nombres\paginas.blade.php

<table class="table">
    <thead>
      <tr>
        <th scope="col">ID</th>
        <th scope="col">Descripcion</th>
      </tr>
    </thead>
    <tbody>

        @if(count($nombres))
            @foreach ($nombres as $item)
            <tr>
                <th scope="row">{{ $item->id }}</th>
                <td>{{ $item->descripcion }}</td>
            </tr>
            @endforeach
        @endif

    </tbody>
</table>

routes\web.php

// MUESTRA LOS PRIMEROS REGISTROS
Route::get('nombres''App\Http\Controllers\ScrollController@index');

// BUSCADOR EN TIEMPO REAL
Route::get('nombres/buscador''App\Http\Controllers\ScrollController@buscador');

app\Http\Controllers\ScrollController.php

    public function index(){
        $nombres = Articulo::paginate(10);
        return view("welcome"compact("nombres"));
    }

    public function buscador(Request $request){
        $nombres = Articulo::where("descripcion"'like'$request->texto."%")->take(10)->get();
        return view("nombres.paginas"compact("nombres"));
    }




Comentarios

Entradas más populares de este blog

Ruta hacia el desarrollador web full stack en Soluciones++

Soluciones++ para VBA