How to use Datatable Plugin (Jquery) with Node.js

Asked

Viewed 285 times

0

I am working with Node and express and need to fill a table with information that comes from an SQL Database. I run my get code from the page that way:

app.get('/Home.ejs', (req, resp) => {
    SQL_Server(Seleciona, (rec) => {
        resp.render("Home", { Objeto: rec });
        resp.end();
    });
});

It was working when I was carrying the tabel like this:

<% for(var i = 0; i < Objeto.length; i++){ %>
                    <tr>
                        <td name="Nome">
                            <%=Objeto[i].ObjetoNome %>
                        </td>
                        <td name="Nome">
                                <%=Objeto[i].ObjetoEndereco %>
                        </td>
                    </tr>
<%}%>

But now I need to fill this table using Jquery, because we are standardizing the templates of all systems. When I tried using jquery datatable, it is no longer displaying the table. Here is my code:

var TableDados = $('#MinhaTabela').DataTable({
                "serverSide": true,
                ajax: {
                    "url": "/Home.ejs",
                    "dataSrc": "Objeto"
                }, 
                "scrollX": true,
                "processing": true,
                "paging": true,
                "lengthChange": false,
                "searching": true,
                "ordering": true,
                "info": true,
                "autoWidth": true,
                "deferRender": true,
                "language":
                {
                    "url": "<% /scripts/plugins / dataTables / languagePT - BR.json' %>"
                },
                "columns": [
                    { "data": "Nome" },
                    { "data": "Endereço" },
                ],
                "order": [0, "asc"]
            });

It no longer opens the screen and displays the message:

Error: Can’t set headers after they are sent. At Serverresponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)

I researched several solutions and examples but keeps displaying this error.

1 answer

0


I found the right solution.

<script>
//Atribui a variável Lista o array de objetos que retorna do backend
var Lista= <%- JSON.stringify(lista) %>;

var TableDados = null;
$(function () {

    TableDados = $('#tblDados').DataTable({
        "serverSide": false,
        "drawCallback": function (settings) {},
        "scrollX": true,
        "processing": true,
        "paging": true,
        "lengthChange": false,
        "searching": true,
        "ordering": true,
        "info": true,
        "autoWidth": true,
        "deferRender": true,
        "language":
        {
            "url": "/scripts/plugins/dataTables/languagePT-BR.json"
        },
        "columns": [
            { "data": "Nome" },
            { "data": "Sobrenome" },
            { "data": "Numero" },
            { "data": "Idade" },
        ],
        "order": [0, "asc"]
    });
    console.log(Lista);
    TableDados.rows.add(Lista)
    TableDados.draw();
});

Browser other questions tagged

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