Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular datatable and JSON structure issue

Here is my issue: The datatable plug-in only accepts an array I believe but my API returns an object with an array. I am trying to figure out if I need to extract this info (how?) or if the plug-in has some methods that do that for me (there is one):

    //the datatable plugin expects the following JSON structure
        [
            {
                "ID":"a5f415a7-3d4f-11e5-b52f-b82a72d52c35",
                "Record":1,
                "HostName":"SRX552P"
            }
        ]

    //my PHP server returns the following JSON structure:
        {
            "status":"success",
            "message":"data retrieved",
            "data":[
                    {
                        "ID":"a5f415a7-3d4f-11e5-b52f-b82a72d52c35",
                        "Record":1,
                        "HostName":"SRX552P"
                    }
                   ]                
        }    

    var allData = null;
    //this is my angularjs service where I am grabbing all the 'data':
        function getAllData() {
            dataservice.get('table001').then(function(data){
                allData = data;
            });         
            return allData;
        }

    //it looks like my issue is exactly what this post describes:
    http://stackoverflow.com/questions/27797435/accessing-json-data-in-angularjs-datatables-using-dtoptions

    //but applying ".withDataProp('data.data')" didn't work for me:
        ...
            this.standardOptions = DTOptionsBuilder
                .fromFnPromise(getAllData())        
                .withDataProp('data.data')
                //.fromSource('api/tables/datatables.standard.json')    //static data works fine!                      
                 //Add Bootstrap compatibility
                .withDOM("<'dt-toolbar'<'col-xs-12 col-sm-6'f><'col-sm-6 col-xs-12 hidden-xs'l>r>" +
                    "t" +
                    "<'dt-toolbar-footer'<'col-sm-6 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-6'p>>")
                .withBootstrap();              

            this.standardColumns = [
                DTColumnBuilder.newColumn('ID'),
                DTColumnBuilder.newColumn('Record'),
                DTColumnBuilder.newColumn('HostName')
            ]; 
        ...

//this is the service
(function () {
    'use strict';
    angular.module('app.ipxtool').factory('dataservice', dataservice);
    dataservice.$inject = ['$http', '$q'];    

    function dataservice($http, $q) {        
        var serviceBase = 'api/v1/';
        var obj = {};        
        obj.get = function (q) {
            return $http.get(serviceBase + q).then(success).catch(fail);
        };         

        function success(results) {             
            if (results.data.status == "error") {
                logger.error(results.data.message, '', 'Error');    //$response["data"] = null for errors;
            }              

            if (results.data.status == "warning") {
                logger.warning(results.data.message, '', 'Warning');
            }                      

            if (results.data.status == "success") {
                logger.success(results.data.message, results.data.data, 'Success'); //$response["data"] = $rows;               
            }                
            return results.data; 
        }

        function fail(e) { 
                return (e); 
        }                                                
        return obj;   
};
})();

Using Fiddler I can see all the data being returned. Also I output the first array item as follows:

console.log("var allData: " + "[" + JSON.stringify(alldata.data[1]) + "]");

enter image description here enter image description here

like image 370
Max Avatar asked Feb 17 '16 20:02

Max


1 Answers

The solution to this issue was to add the following:

.fromFnPromise(function() {
     return dataservice.get('table001');
})

Not sure why calling getAllData() didn't work. So finally I got this thing resolved. Thanks.

like image 199
Max Avatar answered Sep 21 '22 23:09

Max