Slow loading with Jquery Datatables plugin

Asked

Viewed 964 times

1

I am using Jquery Datatables in a small system and everything was fine until I came across a listing of almost 2500 records...

The point is that it takes a certain amount of time for the information to be loaded and adjusted in Datatable, which causes it to keep loading and displaying all 2500 records on the page until it loads everything and the paging goes into action.

Is there any way to avoid/mask it? I don’t currently use ajax, I do so:

In my Controller:

public function index()
{
    $funcoes = Funcao::orderBy('id')->get();
    return view('cadastros/funcao/index', compact('funcoes'));
}

In my View:

<table id="appDatatable" class="table table-bordered table-hover">
    <thead><tr><th>...</th></tr></thead>
    <tbody>
    @foreach($funcoes as $funcao)
    <tr>
        <td>{{ $funcao->cbo }}</td>
        <td>{{ $funcao->nome }}</td>
        ...
    </tr>
    @endforeach
    </tbody>
</table>

My script:

<script>
    $(function () {
        $("#appDatatable").DataTable({
            sPaginationType: "simple_numbers",
        });
    });
</script>
  • 1

    An option would be to load the user’s events/interactions with the table. https://datatables.net/examples/data_sources/server_side.html

  • 1

    Loading 2500 records at once is not wise, why not use Server-side Processing? https://datatables.net/examples/data_sources/server_side.html

  • I n use because I don’t even know rs, I’ll take a look at this link and see if I can do here

  • In case I am using Laravel5, in the part that calls the ajax I put a route that returns all the elements in JSON and it makes this thing to load only what you need?

  • The ajax call is inside the . Datatable? method or you fill in the <table> first and then use $('#table'). Datatable()?

  • Yeah, I’ll fill it in and then call

  • edits the question and puts the javascript of the ajax call and . Datatable()

  • I put what I do currently @Eduardomoreira

  • Look, this slowness is probably in the writing of the table, and not in you turning it into Datatable (in my experience this is very fast). I suggest you test without turning it into Datatable to confirm where the problem is.

  • Yes, apparently it is slow by its much same thing, also it took a long time without Datatable, I will try to do what they said with the server side.

Show 5 more comments

1 answer

1


I have already experienced this problem. I realized that slowness was caused by manually filling the table and then using $('#table'). Datatable(). I see two alternatives to solve this, but both need ajax.

The first option, easier, is to make an ajax call and deliver json to Datatables

Therefore:

function preencherTabela(jsonData) {
    $('#tabela').DataTable({
        destroy: true, //retirar essa linha caso você nunca recarregar a tabela
        sPaginationType: "simple_numbers",
        data: jsonData,
        columns: [
            { "data": "Cbo" },
            { "data": "Nome" }
        ]
    });
}

$.ajax({
    url: '/suaUrl',
    type: 'GET',
    success: function (jsonData) {
        preencherTabela (jsonData);
    }
});

The second, more elegant option would be to use "server-side Processing" support and make a real pagination. Personally I never had to do that and therefore I can’t help you much. More on the subject here

  • I tried to do this but it gave error "Uncaught Referenceerror: data is not defined" in line " Success: fillTable (jsonData)". This jsonData I have to replace with something?

  • had a little problem in Success. I edited. jsonData is just any name I gave to the variable. It is the data you will receive via ajax to the popular table (if ajax is successful, of course).

  • Ah now it worked, you can break a branch while I see how to implement this server side deal more calmly, vlw.

Browser other questions tagged

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