Quantcast
Channel: Recent Discussions — DataTables forums
Viewing all articles
Browse latest Browse all 82018

dynamic columns

$
0
0

I found other posts that deal with grammatically creating columns. My data seems exactly the same but I am getting an error. So I am guessing it is because my json is formatted differently.

Here is the link where someone else got it working: https://datatables.net/forums/discussion/comment/162006/#Comment_162006

Here is my code:

            var columns = []; 
            $.ajax({
                url: "api/ToGenerate",
                success: function (data) {
                    //tableData = JSON.parse(data);  //Unexpected token o in JSON at position 1
                    tableData = JSON.parse(JSON.stringify(data));    // Cannot read property '0' of undefined
                    
                    columnNames = Object.keys(tableData.data[0]);
                    for (var i in columnNames) {
                        columns.push({
                            data: columnNames[i],
                            title: columnNames[i]
                        });
                    }
                    
                    $('#ToGenerate').DataTable({
                        dom: 'frtip',
                        ajax: {
                            url: 'api/ToGenerate',
                            dataSrc: 'Table'
                        },
                        rowId: 'ImportID',
                        columns: columns
                    })
                }

            });

here is the format of my JSON

{"Table":[{"ImportID":121,"DeptName":"Ag Commissioner","FTE":48.15,"EmployeeCount":50}...

the first line of code JSON.parse(data) results in 'unexpected token...
the second line of code (from the post that I have the link to above) results in Cannot read property '0' of undefined and is talking about the line: columnNames = Object.keys(tableData.data[0]);

So I assume something is wrong with the structure of the object tableData but I can't figure it out.


Viewing all articles
Browse latest Browse all 82018

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>