mmadrigal
mmadrigal

Reputation: 95

DataTables Server Side Processesing Without Columns Property

I need to know, if is possible initialize a DataTable from JQuery, with server side processesing without indicate the property "columns", or if not, how to indicate it dynamically.

When I initialize the DataTable just like that

var InicialiceLaTablaDeUsuarios = function () {
        var laTabla = $('#TablaDeUsuarios').DataTable({
            "processing": true,
            "serverSide": true,
            "ajax": {
                "url": "/Usuarios/ConsulteLosUsuarios",
                "type": "POST",
                "dataType": "JSON"
            },
            "deferRender": true,
            "serverSide": true,
            "searchDelay": 800,
            "autoWidth": true,
            "stateSave": true,
            "columns": [
                        { "data": "Apellido1" },
                        { "data": "Apellido2" },
                        { "data": "Clave" },
                        { "data": "CorreoElectronico" },
                        { "data": "Estado" },
                        { "data": "Id" },
                        { "data": "Nombre" }
            ]
        });
        return laTabla;
    }

it works perfectly, but if I try to initialize DataTable just like that

var InicialiceLaTablaDeUsuarios = function () {
        //
        var laTabla = $('#TablaDeUsuarios').DataTable({
            "processing": true,
            "serverSide": true,
            "ajax": {
                "url": "/Usuarios/ConsulteLosUsuarios",
                "type": "POST",
                "dataType": "JSON"
            },
            "deferRender": true,
            "serverSide": true,
            "searchDelay": 800,
            "autoWidth": true,
            "stateSave": true
        });
        return laTabla;
    }

it doesn't work and throw the next error.

enter image description here

Thanks for your time.

Greetings.

Upvotes: 0

Views: 1331

Answers (2)

philantrovert
philantrovert

Reputation: 10092

I was in a similar situation where I had to initialize the columns for my datatable dynamically. I solved it by making a seperate ajax call to fetch the column description from the backend.

Something along the lines of this:

$.ajax({
        "async": false,
        "url": "..//cgi-bin/<file with column description>",
        "type": "GET",
        "success": function(res){
            oTable = $('#example').DataTable({
                "serverSide": true,
                "ajax": {
                    "url": "../cgi-bin/<server-side output file>",
                    "type": "POST",
                    "data" : { <table names> }
                    },
                "dataSrc": "data",
                "language": {
                    "searchPlaceholder": "Search..."
                },
                "columns": res
      });

The output of the file with column description was a JSON that looked like:

[{
    "title": "ID"
}, {
    "title": "NAME"
}, {
    "title": "AGE"
}]

This would define 3 columns named TITLE, NAME and AGE.

Upvotes: 1

Nikita Dhiman
Nikita Dhiman

Reputation: 182

I was trying jquery datatables few days back and I encountered the same problem. Though I saw examples were the server side functionality was retained without "columns" field within script but while trying the same I was not able to retain the functionality. The primary reasons are following:

  1. How you obtain json data, that is how you json string is being made from your array of elements.

  2. Refer this example, though they have not used "columns" in the script part but they are defining those columns in controller and hence no need to do so later. Something similar can be done in your case.

Without columns our json data won't know which data is the part of which column and hence defination is needed somewhere. Hope this is helpful! Thanks.

Upvotes: 0

Related Questions