Cannot show fetch data in datatable

Cannot show fetch data in datatable

fredemagifredemagi Posts: 2Questions: 1Answers: 0
edited October 2022 in Free community support

Hello,

I'm trying to show some fetch data in a datatable, but for some reason, it does not work. It gives the following error: DataTables warning: table id=all-users-datatable - Requested unknown parameter '0' for row 0, column 0

The code is a follows:

PHP:

$arr = [];

        $users = User::with('role')->get(['name', 'company', 'email', 'location', 'recent_login']);


        foreach($users as $user)
        {
            $arr['data'][] = (object) [
                               'name' => $user->name, 
                               'company' => $user->company, 
                               'email' => $user->email, 
                               'role' => $user->role->name, 
                               'location' => $user->location,
                               'recent_login' => $user->recent_login,
                               'actions' => 'Tester'
                              ];
        }



        return response()->json($arr);

HTML:

<table id="all-users-datatable" class="display" style="width: 100%;">
  <thead>
    <tr>
      <th>
        Name
      </th>
      <th>
        Company
      </th>
      <th>
        Email
      </th>
      <th>
        Role
      </th>
      <th>
        Location
      </th>
      <th>
        Recent login
      </th>
      <th>
        Actions
      </th>
    </tr>
  </thead>
</table>

jQuery:

 $('#all-users-datatable').DataTable(
    {
        processing: true,
        serverSide: true,
        ajax: '/get-users',
        columns: [
            { data: 'name' },
            { data: 'company' },
            { data: 'email' },
            { data: 'role' },
            { data: 'location' },
            { data: 'recent_login login' },
            { data: 'actions' }
            ]
    });

What am I missing?

Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide

This question has an accepted answers - jump to answer

Answers

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

    Something doesn't quite match up with the Requested unknown parameter '0' for row 0, column 0 and the use of columns.data. According to the tech note link in the error the Parameter as an integer states that Datatables is expecting the data to be an array on an object.

    Are you initializing or trying to get the Datatable aPI before the above initialization code?

    Have you looked at the response using the browser's network inspector tool?

    Can you post a link to your page so we can take a look? If not the Debugger might give the developers enough information to help.

    Kevin

  • fredemagifredemagi Posts: 2Questions: 1Answers: 0

    I got it working. Some data seemed to be corrupted, so I added

    columnDefs: [{
                "defaultContent": "-",
                "targets": "_all"
              }]
    
    

    and it worked.

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

    Yes, that is a good option for missing data.

    Kevin

Sign In or Register to comment.