Uncaught TypeError: Cannot use 'in' operator to search for 'length' in Id

Uncaught TypeError: Cannot use 'in' operator to search for 'length' in Id

itmalanitmalan Posts: 3Questions: 1Answers: 0
edited March 2023 in Free community support

Hello All,
I am trying dynamic column header in DataTables. The below code is working in the old environment. Can any help me in fixing the above mentioned issue.

Thanks in advance.

My cade is below,

function get_details(){
 
var payload = {start:0,draw:1,length:25 };  
    $.ajax({
        url: "<?php echo base_url('employee/get_users_list')?>",
        type: 'POST',
        dataType: "JSON",
        data: payload,
        "success": function(json) {
            var tableHeaders;
            $.each(json.columns, function(i, val){
                tableHeaders += "<th>" + val + "</th>";
                headers_count++;
            });
                     
            $("#div_data_table").empty();
            $("#div_data_table").append('<table id="data_table" class="table table-bordered table-striped" ><thead><tr>' + tableHeaders + '</tr></thead></table>');
            
            $('#data_table').DataTable(json);  **//Getting Error Here**
            
        },
     
    });

)

Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,341Questions: 26Answers: 4,954

    //Getting Error Here

    Can you post an example of the json variable?

    Better is to post a link to your page or a test case replicating the issue so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    As Kevin said, it's going to be the JSON. It looks like it'll contain the user list, the data that going into the data. If so, you'll need to change:

    $('#data_table').DataTable(json);
    

    to be something like:

    $('#data_table').DataTable({data: json});
    

    Colin

  • itmalanitmalan Posts: 3Questions: 1Answers: 0
    edited March 2023

    Hi kthorngren and colin,
    Please have a look at the response data from server.
    Json output data:

    {
        "draw": "1",
        "recordsTotal": 1000,
        "recordsFiltered": 100,
        "data": [
            [
                "1",
                "GOVINDAN.S",
                "Male",
                "Driver Level - I"
            ],
            [
                "2",
                "MEENAKSHISUNDARAM. V",
                "Male",
                "Lab Attender (Electronic Media)"
            ],
            [
                "1795",
                "GULAM RASOOL BHAT",
                null,
                "ASSISTANT PROFESSOR"
            ]
        ],
        "columns": [
            "Id",
            "Emp Id",
            "Pen Id",
            "Name"
        ]
    }
    
    
  • kthorngrenkthorngren Posts: 21,341Questions: 26Answers: 4,954
    Answer ✓

    I copied your code here:
    https://live.datatables.net/vavivita/1/edit

    Looks like you need to remove the columns object from the JSON data. This:

        "columns": [
            "Id",
            "Emp Id",
            "Pen Id",
            "Name"
        ]
    

    is not a supported format for Datatables. Comment out delete json.columns; and you will see the same error.

    Kevin

  • itmalanitmalan Posts: 3Questions: 1Answers: 0

    I changed the name of the object columns. It is working now. Problem solved. Thanks a lot.

Sign In or Register to comment.