Populating Column names from AJAX source dynamically

Populating Column names from AJAX source dynamically

maxpetersmaxpeters Posts: 2Questions: 1Answers: 0

Currently I have tried to create my code to use Datatables and it is working for me except the part where I where I wish to populate the columns from a JSON object dynamically, I have searched countless threads and methods to build/convert/create the JSON as an object but datatables will only take if its written within the js document.

    $(document).ready(function () {
        domLoaders.EICBackup_IndexView(); // misc function

        var columns = [{ "sTitle": "ActivityID", "mDataProp": "activityID" }, { "sTitle": "ContactType", "mDataProp": "contactType" }];
        var z = [];

       var oTable = $('#tblEIC').dataTable({
            "bProcessing": false,
            "ajax": {
                "url": "RESTApi/EICBackup.json/SearchActivities?paramName=ActivityID&paramValue=11",
                "dataSrc": ""
            },
            "aoColumns": columns /* '/RESTApi/EICBackup.json/GetEICProperties' */
        });
    });

Everything else works, the closest I have gotten is this code and tried even this previous thread which to my understanding is everything I needed to make it work am I missing something the time that thread was discussed?
http://www.datatables.net/forums/discussion/3519/getting-column-names-from-ajax-source

Answers

  • maxpetersmaxpeters Posts: 2Questions: 1Answers: 0

    i got the solution for anyone looking to autopopulate data and columns properly using a json object

    $(document).ready(function () {
     $.getJSON("/RESTApi/GetEICColumns", function(data){
                var items = [];
                $.each(data, function (key, val) {
                    console.log(key + ':' + val);
                    items.push(val);
                });
                console.log('*');
                console.log(items);
                console.log('*');
    
                var oTable = $('#tblEIC').dataTable({
                    "bProcessing": false,
                    "ajax": {
                        "url": "RESTApi/EICBackup.json/SearchActivities?paramName=ActivityID&paramValue=12",
                        "dataSrc": ""
                    },
    
                    "aoColumns": items
                });
            });
    });
    
This discussion has been closed.