Table with DataTables and Editor does not work for me

0

I'm doing an inventory application of computer equipment in PHP with Laravel. At the moment I want to show a table with all the data about the models, but I can not find the solution.

My index for this view is:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>jQuery Datatables</title>
<link rel="stylesheet" type="text/css" href="DataTables/css/jquery.dataTables.css">
<link rel="stylesheet" type="text/css" href="Editor/css/dataTables.editor.css">
<script type="text/javascript" language="javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" language="javascript" src="DataTables/js/jquery.dataTables.js"></script>
<script type="text/javascript" language="javascript" src="Editor/js/dataTables.editor.js"></script>
<script type="text/javascript" language="javascript">
    $(document).ready(function() {
        var datatable = $('#mytable').DataTable({
            ajax: $.ajax( {
                url: "/modelo",
                type: 'GET',
                dataType: 'json',
            } ),
            columns: [
                {data:"marca"},
                {data:"modelo"},
                {data:"part_number"},
                {data:"coste"},
                {data:"caractetisticas"},
                {
                    data: null,
                    defaultContent: '<a href="#" class="edit">Editar</a> / <a href="#" class="Eliminar">Delete</a>'
                }
            ],
        });
        editor = new $.fn.dataTable.Editor( {
            ajax: "actions.php",
            table: "#mytable",
            idSrc: "id",
            fields: [
                {label:'Marca' , name:'marca'},
                {label:'Modelo' , name:'modelo'},
                {label:'Part Number' , name:'part_number'},
                {label:'Coste' , name:'coste'},
                {label:'Características' , name:'caractetisticas'}
               ]
        });
        $('#mytable').on('click', 'a.edit', function (e) {
            e.preventDefault();
            editor
                .title( 'Editar modelo' )
                .buttons( { "label": "Update", "fn": function () { editor.submit() } } )
                .edit( $(this).closest('tr') );
        } );
        $('#mytable').on('click', 'a.remove', function (e) {
            e.preventDefault();
            editor
                .message( '¿Estás seguro de eliminar el modelo?' )
                .buttons( { "label": "Delete", "fn": function () { editor.submit() } } )
                .remove( $(this).closest('tr') );
        } );
        $('a.create').on('click', function (e) {
            e.preventDefault();
            editor
                .title( 'Crear nuevo modelo' )
                .buttons( { "label": "Add", "fn": function () { editor.submit() } } )
                .create();
        } );
    });
</script>
</head>
<body>
<a href="{!!URL::to('/modelo/create')!!}" class="create">Nuevo</a>
<table id="mytable" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>Marca</th>
            <th>Modelo</th>
            <th>Part Number</th>
            <th>Coste</th>
            <th>Características</th>
        </tr>
    </thead>

    <tfoot>
        <tr>
            <th>Marca</th>
            <th>Modelo</th>
            <th>Part Number</th>
            <th>Coste</th>
            <th>Características</th>
        </tr>
    </tfoot>
</table>

And this is my controller:

<?php

namespace inventario\Http\Controllers;

use Illuminate\Http\Request;
use inventario\Modelos;
use inventario\Http\Requests\ModelosRequest;

class ModeloController extends Controller
{
public function __construct(){
    //Para que no se pueda acceder a /usuario sin entrar por el login
    $this->middleware('auth');
    //Para dar permisos solo al usuario admin de acceder
    $this->middleware('admin');
}

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */

public function index(Request $request)
{
    if ($request->ajax()) {
        $modelos = Modelos::all();
        return response()->json($modelos);
    }
    return view('modelos.index');
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    return view('modelos.create');
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(ModelosRequest $request)
{
    if($request->ajax()){
        Modelos::create($request->all());
        return response()->json([
                "mensaje" => "creado"
            ]); 
    }

}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    //
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
    $mdoelos = Modelos::find($id);

    return response()->json(
            $modelos->toArray()
        );
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $id)
{
    $modelos = Modelos::find($id);
    $modelos->fill($request->all());
    $modelos->save();

    return response()->json([
            "mensaje"=>"listo"
        ]);
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
    $modelos = Modelos::find($id);
    $modelos->delete();

    return response()->json([
            "mensaje"=>"eliminado"
        ]);
}
}

I think my error is in the "ajax", but when I inspect the web, I notice that the data itself is loading them:

My loaded page looks like this (I do not know why, but neither the Edit and Delete buttons appear to me):

    
asked by Sergio Ramirez Dominguez 24.04.2017 в 18:29
source

2 answers

0

The AJAX call must be done by DataTables from the specified url, you should not put the result of the call as a parameter:

var datatable = $('#mytable').DataTable({
        ajax: "/modelo",
        columns: [
            {data:"marca"},
            {data:"modelo"},
            {data:"part_number"},
            {data:"coste"},
            {data:"caractetisticas"},
            {
                data: null,
                defaultContent: '<a href="#" class="edit">Editar</a> / <a href="#" class="Eliminar">Delete</a>'
            }
        ]
    });
    
answered by 26.04.2017 в 11:15
0

Even passing the ajax in the following way, it does not show it in my view either.

$(document).ready(function() {
        var datatable = $('#mytable').DataTable({
            ajax: {
                data: [
                {
                    "marca":"Prueba",
                    "modelo": "modelo",
                    "part_number": "12354",
                    "coste": "100",
                    "caracteristicas": "No se que hacer"
                }]
            },
            columns: [
                {data:"marca"},
                {data:"modelo"},
                {data:"part_number"},
                {data:"coste"},
                {data:"caracteristicas"},
                {{--{
                    data: null,
                    defaultContent: '<a href="#" class="edit">Editar</a> / <a href="#" class="remove">Eliminar</a>'
                }--}}

            ]
        });
    
answered by 27.04.2017 в 20:28