How to get columns to jquery datatables without define

How to get columns to jquery datatables without define

hbaltuntelhbaltuntel 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

Answers

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

    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 variable columns. Then the Datatable is initialized with server side .processing and the columns variable.

    Kevin

  • hbaltuntelhbaltuntel Posts: 2Questions: 1Answers: 0

    Thanks!
    This opened my mind. Firstly I get column names in first ajax, and call server-side ones in success.
    It works fine

  • lonelyplanetlonelyplanet Posts: 3Questions: 0Answers: 0

    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

        var mycolumns = [];
        for (i = 0; i < Object.keys(objrecord).length; i++) 
        {
            mycolumns.push(Object.keys(objrecord)[i]);
        }
    

    but in this DataTable constructor it gives an error

    "TypeError: Cannot use 'in' operator to search for 'length' in _id"

     dt1 = $('#mytable1').DataTable({
            columns: mycolumns
        });
    
    var rowNode = dt1
        .row.add(objrecord)
        .draw(false)
        .node();
    $(rowNode)
        .css({backgroundColor: 'green'})
        .animate({backgroundColor: 'white'}, 2500);
    

    should I use mycolumns as an associative array instead ? to use a java script object ? thank you for any clues

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

    @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

  • lonelyplanetlonelyplanet Posts: 3Questions: 0Answers: 0

    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];
    }

  • lonelyplanetlonelyplanet Posts: 3Questions: 0Answers: 0

    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

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

    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

This discussion has been closed.