Using Laravel Inertia with vue 3

Using Laravel Inertia with vue 3

MedTaha4EverMedTaha4Ever Posts: 2Questions: 1Answers: 0

Using Laravel Inertia with vue 3
The data was passed from controller to vue but datatable can't render it correctly
it renders four rows and thats correct but with empty cells
Error messages shown:

Description of problem:
The vue code:

<script setup>
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout.vue';
import { Head } from '@inertiajs/inertia-vue3';
import DataTable from 'datatables.net-vue3';


defineProps({
    cars: Array,
    // columns: Array,
})
</script>

<template>

    <Head title="Voitures" />

    <AuthenticatedLayout>
        <template #header>
            <h2 class="font-semibold text-xl text-gray-800 leading-tight">
                Voitures
            </h2>
        </template>


        <DataTable :data="cars" class="display">
            <thead>
                <tr>
                    <th>id</th>
                    <th>immat</th>
                    <th>model</th>
                </tr>
            </thead>
        </DataTable>

    </AuthenticatedLayout>
</template>

<style>
@import 'datatables.net-dt';
</style>

The Controller Code:

class CarsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        return Inertia::render('Cars', [
            'cars' => Cars::all(),
            'columns' => ['id', 'immat', 'marque', 'model', 'date_cir', 'contract_id'],
        ]);
    }

and the vue debugger screenshot:

Answers

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954

    Looks like the JSON data is is an array of objects but the error you are seeing is indicating that Datatables is configured for an array of arrays. Start with the troubleshooting steps at the link provided in the error:
    https://datatables.net/manual/tech-notes/4

    I suspect you need to use columns.data but not sure as you didn't show your Datatables init code. Also you may need to use ajax.dataSrc to define where the data is located in the JSON response. See the Ajax docs for more details. Use the browser's network inspector to see what is actually returned to determine how to configure Datatables.

    Kevin

  • MedTaha4EverMedTaha4Ever Posts: 2Questions: 1Answers: 0

    Thank you Kthorngren
    I inspected the response and here is the screenshot

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954

    Looks like your rows are in the object props.cars which means you need to set ajax.dataSrc to "props.cars". And use columns.data to define the columns.

    Kevin

  • draw134draw134 Posts: 12Questions: 4Answers: 0
    edited December 2022

    hi @kthorngren could you set an example? for that one? We have similar issue which I also posted here https://datatables.net/forums/discussion/comment/215320#Comment_215320. Thanks

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    edited December 2022

    @draw134 Based on the screenshot above:

    $('#example').dataTable( {
      "ajax": {
        "url": "data.json",
        "dataSrc": "props.cars"
      },
      "columns": [
        { "data": "contract_id" },
        { "data": "date_cir" },
        { "data": "id" },
        { "data": "immat" },
        { "data": "marque" },
        { "data": "model" }
      ]
    } );
    

    See your thread for another example.

    Kevin

Sign In or Register to comment.