How to get columns to jquery datatables without define
How to get columns to jquery datatables without define
hbaltuntel
Posts: 2Questions: 1Answers: 0
I have a table in oracle which has 86 columns. I want to get all columns to my server-side datatables.
I got all columns data from table suitable for serverside.
And I create serverside datatable with jquery. I defined each column like this;
$('#server_side').DataTable({
...
"columns": [
{ "data": "ID", "name": "ID", "title": "ID", "autoWidth": true, "class":"read_only" },
{ "data": "TANIM", "name": "TANIM", "title":"TANIM", "autoWidth": true },
{ "data": "SKOD", "name": "SKOD", "title": "SKOD", "autoWidth": true },
{ "data": "BARKOD", "name": "BARKOD", "title": "BARKOD", "autoWidth": true }
],
....
It works fine but I don't want define any column one by one. Is there any simple way to get all columns
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
You would need to do something like this example:
http://live.datatables.net/qimukefe/1/edit
The ajax response will contain your columns then in the
success
callback the column info is extracted and saved in a variablecolumns
. Then the Datatable is initialized with server side .processing and the columns variable.Kevin
Thanks!
This opened my mind. Firstly I get column names in first ajax, and call server-side ones in success.
It works fine
I need to display variable columns (from a no sql db), from java script on client side
I have an object objrecord (not an array) containing the current record
firstly I put the keys as strings into a java script array mycolumns
but in this DataTable constructor it gives an error
"TypeError: Cannot use 'in' operator to search for 'length' in _id"
should I use mycolumns as an associative array instead ? to use a java script object ? thank you for any clues
@lonelyplanet ,
The
columns
option specifies it expects an array. Within the array you would have elements representing the config options for each column. The config options are contained with a Javascript object. Take a look at the code snippet in the first post above to see an example.Use console.log to output the resulting mycolumns and post here. This way we can take a look to see what you are trying to use.
Kevin
this seems to work, as a java script object
var mycolumns = {};
for (i = 0; i < Object.keys(objrecord).length; i++)
{
mycolumns[Object.keys(objrecord)[i]]=Object.keys(objrecord)[i];
}
using the example above, is this a valid way to load variable columns from a java script object
var cols = { "data": "ID", "name": "ID", "title": "ID", "autoWidth": true, "class":"read_only" };
dt1 = $('#mytable1').DataTable({
"columns": [cols]
});
thank you for any clues
Hi @lonelyplanet ,
We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.
Cheers,
Colin