Unknown Number of Column With Each Query
Unknown Number of Column With Each Query

I have a dropdown with a list of reports that can be run on a page. DataTables is using ajax to retrieve the correct number of rows, but I can't get the columns names to show up. My data is a JSON array of objects with the column name in each row.
{"action": "getReportData", "actionId": "21", "retVal": "True", "sqlCode": "0", "sqlMsg": "", "recordset": [ { "ShortCatalogName": "1949, General Statutes of Connecticut, Comley et al, 3v plus supps.", "LongID": "08024.0003.001", "CatalogID": "08024", "Downloads": 3 }, { "ShortCatalogName": "1949, General Statutes of Connecticut, Comley et al, 3v plus supps.", "LongID": "08024.0003.001", "CatalogID": "08024", "Downloads": 1 }, { "ShortCatalogName": "1875-, Georgia Attorney General Opinions", "LongID": "08027.1949.001", "CatalogID": "08027", "Downloads": 3 }, { "ShortCatalogName": "1875-, Georgia Attorney General Opinions", "LongID": "08027.1972.001", "CatalogID": "08027", "Downloads": 2 }, { "ShortCatalogName": "1888, Miller’s Annotated Code 1v+s.", "LongID": "08031.1888.001", "CatalogID": "08031", "Downloads": 2 } ] }
This question has accepted answers - jump to:
Answers
How can I create the column definition when I don't know which columns are coming back? Each report has a different number of columns and I don't know what the columns are until the AJAX call returns.
Thanks for your question - however, per the forum rules can you link to a test case showing the issue please. This will allow the issue to be debugged.
Information on how to create a test page, if you can't provide a link to your own page can be found here.
Thanks,
Allan
It's not an issue with DataTables itself, but an issue of using DataTables. My JOSN is coming from a .Net page that includes a number of other values. The examples on the site show an array, or object array, where each array item is a row from the database, and the initialization has a definition for the name and number of columns.
I'm using JsonConvert.SerializeObject in my .Net code, and am presented with an array of objects. I can get DataTables to display the JSON data, but only because I know how to define the names and number of columns when initializing the table in my test cases.
I could modify my JSON code to return an extra property with the names of the columns being returned, but was hoping there was a mechanism built into DataTables to determine the column names from the array of objects.
I WAS using AJAX loading via DataTables with a dataSrc option to get the correct number of rows, but was unable to dynamically define the column names.
I'm currently working on getting the column names from the JOSN object to build a string for use in defining the columns that came in:
The above doesn't work because
columns
needs to be an object. If I can pass the column definition as an array I can modify my JSON output to accommodate this.Yes, there is no way to do that via the DataTables Ajax options at the moment I'm afraid (FAQ).
Allan
Sorry for multiple messages - I missed this bit:
columns
has to be an array.Allan
After a bunch of trial and error I decided to mock up two rows of data:
This results in a table with two rows and four columns BUT no column names.
I'm sure I can get the JSON to look like the mocked up data, and return a Columns property, but I can't get the column names to show up.
When I change
"columns": columnInit,
to this:
"columns": ['1', '2', '3', '4'],
I get the following error:
Unhandled exception at line 534, column 2 in http://localhost:4800/scripts/jquery-3.1.0.js
0x800a138f - JavaScript runtime error: Invalid operand to 'in': Object expected
I believe I found the issue. I was using the wrong property for the columns. I should have been doing:
columnInit.push({ "title": columns[I] });
This is powerful stuff, Allan. Thanks for making DataTables
Thanks for posting back - great to hear you've got the answer!
Allan