0

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.

2
  • You need to define your columns in the initialization or you can define them under <thead> tag in your table. Commented Dec 5, 2016 at 6:27
  • Hi @philantrovert, thanks for your time. I would think I'm already doing it puu.sh/sEJSD/5c6c94c0a5.png Commented Dec 5, 2016 at 6:44

2 Answers 2

1

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.

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

2 Comments

Thanks @philantrovert it would be a nice solution. Thanks for your time and answers.
@mmadrigal Happy to help.
0

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.

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.