Problem with Datatables in Laravel 5.2

Asked

Viewed 450 times

0

Hello, I tried using Datatables server-side and not server-side but it does not display the datatables. I do everything according to the document (https://datatables.net/) and in tutorials on the internet, but it does not work and also shows no error (pages load normally).

My Controller:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Http\Requests\ValidarFuncRequest;

use Redirect;
use Datatables;

use App\Funcionario;
use App\Dependente;

class FuncionariosController extends Controller
{
    public function index(){

        $funcionarios = Funcionario::all();
        return view('funcionarios.lista', ['funcionarios' => $funcionarios]);

    }

    public function novoForm(){

        return view('funcionarios.formFuncionario');

    }

    public function salvarFuncionario(ValidarFuncRequest $request){

        $funcionario = new Funcionario();

        $funcionario->create($request->all());

        \Session::flash('msg_sucesso', 'Funcionário cadastrado!');

        return Redirect::to('funcionarios/novoForm');

    }

    public function editar($id){

        $funcionario = Funcionario::findOrFail($id);

        return view('funcionarios.formFuncionario', ['funcionario' => $funcionario]);

    }

    public function atualizar($id, ValidarFuncRequest $request){

        $funcionario = Funcionario::findOrFail($id);
        $funcionario -> update($request->all());

        \Session::flash('msg_sucesso', 'Funcionário atualizado!');

        return Redirect::to('funcionarios/'.$funcionario->id.'/editar');

    }

    public function listarDpt($id){

        $funcionario = Funcionario::find($id);

        return view('funcionarios.listaDpt', ['funcionario' => $funcionario]);

    }

    public function deletar($id){

        try{

            $funcionario = Funcionario::findOrFail($id);

            $funcionario -> delete();

            \Session::flash('msg_sucesso', 'Funcionário deletado com sucesso!');

        } catch(\Illuminate\Database\QueryException $e){

            var_dump($e->errorInfo);

            \Session::flash('msg_erro', 'Funcionários com dependente(s) não podem ser deletado!');

        }


        return Redirect::to('funcionarios');

    }

    public function novoDpt($id){

        $funcionario = Funcionario::findOrFail($id);

        return view('funcionarios.novoDept', ['funcionario'=>$funcionario]);
    }



}

My view:

@extends('layouts.app')

@section('content')
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="//cdn.datatables.net/1.10.12/css/jquery.dataTables.css">

<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default">
                <div class="panel-heading">
                    Funcionários
                    <a class="pull-right" href="{{url('funcionarios/novoForm')}}">Adicionar Funcionário</a>
                </div>

                <div class="panel-body">

                        @if(Session::has("msg_sucesso"))
                            <div class="alert alert-success"> {{ Session::get('msg_sucesso')}} </div>

                        @elseif(Session::has('msg_erro'))
                            <div class="alert alert-danger"> {{Session::get('msg_erro')}} </div>

                        @endif

                        <table class="table table-bordered" id="meusFuncionarios">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Nome</th>
                                    <th>Sexo</th>
                                    <th>Ações</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($funcionarios as $funcionario)
                                    <tr>
                                        <td>{{$funcionario -> id}}</td>
                                        <td>{{$funcionario -> nome}}</td>
                                        <td>{{$funcionario -> sexo}}</td>
                                        <td>
                                            <a href="funcionarios/{{$funcionario -> id}}/editar" class="btn btn-default">Editar</a>

                                            <script>
                                                function ConfirmarDelete(){

                                                    var confirma = confirm("Deseja mesmo exluir {{$funcionario -> nome}}");

                                                    if(confirma){

                                                        return true;

                                                    } else{

                                                        return false;

                                                    }
                                                }
                                            </script>

                                            {!! Form::open(['method' => 'DELETE', 'url' => 'funcionarios/'.$funcionario->id, 'style' => 'display: inline;',  'onsubmit' => 'return ConfirmarDelete()']) !!}
                                            <button class="btn btn-default">Excluir</button>
                                            {!! Form::close() !!}

                                            <a href="funcionarios/{{$funcionario->id}}/listarDpt" class="btn btn-default">Dependente(s)</a>

                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="//cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>

<script>
    $(document).ready(function(){
        $('#meusFuncionarios').DataTable();
    });
</script>
@endsection

My rout:

<?php

Route::get('/', function () {
    return view('welcome');
});

Route::auth();

Route::get('home', 'HomeController@index');

Route::get('funcionarios', 'FuncionariosController@index');
Route::get('funcionarios/novoForm', 'FuncionariosController@novoForm');
Route::get('funcionarios/{funcionario}/editar', 'FuncionariosController@editar');
Route::post('funcionarios/salvarFuncionario', 'FuncionariosController@salvarFuncionario');
Route::patch('funcionarios/{funcionario}', 'FuncionariosController@atualizar');
Route::delete('funcionarios/{funcionario}', 'FuncionariosController@deletar');

Route::get('dependentes', 'DependentesController@index');
Route::get('dependentes/formDpt', 'DependentesController@novoForm');
Route::post('dependentes/salvarDpt', 'DependentesController@salvar');
Route::delete('dependentes/{dependente}', 'DependentesController@deletar');
Route::get('dependentes/{dependente}/editarDpt', 'DependentesController@editar');
Route::patch('dependentes/{dependente}', 'DependentesController@atualizar');


Route::get('funcionarios/{funcionario}/listarDpt', 'FuncionariosController@listarDpt');
Route::get('funcionarios/{funcionario}/novoDept', 'FuncionariosController@novoDpt');

Route::get('dependentes/{funcionario}/novoForm', 'DependentesController@novoForm');

Route::get('projetos', 'ProjetosController@index');
Route::get('projetos/novoForm', 'ProjetosController@novoForm');
Route::post('projetos/salvarProjeto', 'ProjetosController@salvarProjeto');
Route::get('projetos/{projeto}/editarProjeto', 'ProjetosController@editarProjeto');
Route::patch('projetos/{projeto}', 'ProjetosController@atualizarProjeto');
Route::delete('projetos/{projeto}', 'ProjetosController@deletarProjeto');
Route::get('projetos/{projeto}/listaFunc', 'ProjetosController@listaFunc');

Route::get('projetos/{funcionario}/addFunc', 'ProjetosController@addFunc');
Route::patch('projetos/{projeto}', 'ProjetosController@addBotao');
  • you are using which loading is ajax: https://datatables.net/examples/ajax/simple.html from this link or what is the type: in the various examples: https://datatables.net/examples/index

  • I’m using the beginning of the site, which is without ajax. I wanted at least it to work (in all tutors I saw it works normal) without ajax and then try using the server-side, but none works... I think even the Variable is not loading the . js and . css files, but I tried several ways and it’s the same.

  • 1

    analyzing so far I would make a first test with the debug of the browser itself press F12, guide console and check if you have any problem in javascript is usually that the problem and only you have possibility to test. Another point is that it has repeated function calling this causes javascript error and it does not get to execute . Datatables(), the following is done to remove those javascript that are in <td> leave only the PHP code and run to see...

  • 1

    Thank you very much man, it did not directly solve the problem but it was a light at the end of the tunnel. Thank you very much! kkkk

  • I managed to solve the problem then?

1 answer

0

The problem is that you have to go to the app.blade.php folder (where the head and body is to put the links and scripts) and put the files there. js and .css. I had done this but the problem is that javascript has to stay above any other script and, in my case, it was down there.

  • I understood the problem .... nice to have solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.