加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 角度的datatable和JSON结构

发布时间:2020-12-17 07:36:33 所属栏目:安全 来源:网络整理
导读:这是我的问题: datatable插件只接受一个我相信的数组,但我的API返回一个数组的对象.我试图弄清楚我是否需??要提取这个信息(如何?),或者如果插件有一些方法为我(有一个): //the datatable plugin expects the following JSON structure [ { "ID":"a5f415a7
这是我的问题:
datatable插件只接受一个我相信的数组,但我的API返回一个数组的对象.我试图弄清楚我是否需??要提取这个信息(如何?),或者如果插件有一些方法为我(有一个):
//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","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:
    https://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;   
};
})();

使用Fiddler我可以看到所有返回的数据.另外我输出第一个数组项如下:

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


任何帮助是赞赏
谢谢

解决这个问题的办法是添加以下内容:
.fromFnPromise(function() {
     return dataservice.get('table001');
})

不知道为什么调用getAllData()不起作用.所以终于我得到了这件事情的解决.谢谢.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读