0

I am trying to use server side processing for my datatable but it just doesn't work when you use the pagination. It reads processing, but does nothing. It pops up with requested unknown parameter '0' for row 0

  $('#load').DataTable({
    "processing": true,
    "serverSide": true,
    "ajax": "{{ url('api/testing')}}","columns": [
     { "data": "id" },
     { "data": "title" }
    ],
    "deferLoading": 20
  });

TABLE

 <table id="load">
        <thead>
            <tr>
                <th>Booking ID</th>
                <th>Booking Title</th>
           </tr>
         </thead>
        <tbody>
           @foreach($bookings as $booking)
              <tr>
                <td>{{ $booking->id }} </td>
                <td>{{ $booking->title }}</td>
               </tr>
           @endforeach
      </tbody>
</table>

JSON

{
    data: [
        {
        id: 1,
        title: "Title 1"
        },
        {
        id: 2,
        title: "A Meeting"
        },
        {
        id: 3,
        title: "Another title"
        }
    ]
}

1 Answer 1

1

This is how I got datatables to work in my project:

Controller:

public function index()
{
    return View::make('admin.requester.index');
}

View:

header: <link href="\css/plugins/datatables/customDT.css" rel="stylesheet">

table:

<table id="requesters" style="word-wrap: break-word;">
    <thead>
        <tr>
            <th>{{ trans('admin/user.show.id') }}</th>
            <th>{{ trans('admin/user.show.first_name') }}</th>
            <th>{{ trans('admin/user.show.last_name') }}</th>
            <th>{{ trans('admin/user.show.email') }}</th>
            <th>{{ trans('admin/user.show.created_at') }}</th>
            <th>{{ trans('admin/user.show.confirmed') }}</th>
            <th>{{ trans('admin/admin.url.actions') }}</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
    </tbody>
</table>

script: <script src="\js\plugins\dataTables\customDT.js"></script>

CSS file:

#requesters {
 margin: 0 auto;
 clear: both;
 width: 100%;
 table-layout: fixed;
 word-wrap: break-word;
}

JS file:

$(document).ready(function(){

$('#requesters').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "requesters/ajax",
    "autoWidth": false,
    "aaSorting": [[ 3, "desc" ]],
    "aoColumns": [
            { 'sWidth': '40px' },
            { 'sWidth': '80px', 'sClass': 'center' },
            { 'sWidth': '80px', 'sClass': 'center' },
            { 'sWidth': '70px', 'sClass': 'center' },
            { 'sWidth': '80px', 'sClass': 'center' },
            { 'sWidth': '35px', 'sClass': 'center' },
            { 'sWidth': '55px', 'sClass': 'center' }
        ],
        "sPaginationType": "full_numbers"
});

});

^ Ajax requests to requesters/ajax <- goes to ajax_index in controller:

Route::get('requesters/ajax', ['uses' => 'admin\RequesterController@index_ajax']);

Controller:

public function index_ajax()
{
    $requesters = User::select($this->ajax_columns)->where('user_role_id', '=', REQUESTER_ROLE_ID);

    return Datatables::of($requesters)
        ->add_column('show', '{{ HTML::actions(\'requester\', $id) }}')
        ->make();
}

private $ajax_columns = [
    'id', 
    'first_name', 
    'last_name', 
    'email', 
    'created_at', 
    'confirmed'
];

No ->get() or ->paginate(), datatables handles the rest.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.