简体   繁体   English

AngularJS ngtable无法正确显示数据

[英]AngularJS ngtable not displaying data correctly

I have an ngTable that is loaded with data that proceeds from a "Get" call to a webapi. 我有一个ngTable,其中加载了从“获取”调用到webapi的数据。 Then I reload the table, but the data it is not being displayed. 然后,我重新加载该表,但是没有显示该数据。

This is the *.js file 这是* .js文件

rdapp.controller('scoringTableCtrl', ['$location', '$scope', '$http', 'ngTableParams', '$filter',
function($location, $scope, $http, ngTableParams, $filter) {
    $scope.teamList = [];
    $http({
        method: 'GET',
        url: 'http://localhost:34592/api/scoringTable',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        }
    }).then(function(success) {
        $scope.teamList = success.data;
        addFieldsForSorting();
        $scope.dataTable.reload();
    }, function(error) {
        console.log(error);
    });
    $scope.resolveHTML = function(position) {
        if (position == 1 || position == 2 || position == 3) return 'champions';
        if (position == 4) return 'champions-prev';
        if (position == 5 || position == 6) return 'europa-league';
        if (position == 18 || position == 19 || position == 20) return 'decline';
        return '';
    }

    function addFieldsForSorting() {
        // Add named properties for every stat value in recipients array, used for sorting the grid.
        // Take value from vitalStats array, use alertIds to match between alpha keys and numeric keys.
        $scope.teamList.forEach(function(team) {
            for (var property in team) {
                if (!team.hasOwnProperty(property)) {
                    continue;
                }
                if (property == 'name') {
                    continue;
                }
                if (property == 'position') {
                    continue;
                }
                var prop = 'sort_' + property;
                team[prop] = -(team[property]);
            }
            team['sort_name'] = team.name;
            team['sort_position'] = team.position;
        });
    }
    $scope.dataTable = new ngTableParams({
        page: 1, // show first page
        count: 20, // count per page
        sorting: {
            sort_position: 'asc' // initial sorting
        }
    }, {
        counts: [],
        total: $scope.teamList.length, // length of data
        getData: function($defer, params) {
            // use build-in angular filter
            var requestData = $scope.teamList;
            var orderedData = params.sorting() ? $filter('orderBy')(requestData, params.orderBy()) : requestData;
            params.total(orderedData.length); // set total for recalc pagination
            $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
        }
    });
}
 ]);

This is my html: 这是我的html:

<div class="position-view" style="position:relative; top:100px;">
<table ng-table="dataTable" class="table table-bordered table-border-important" ng-class="rec_spinner">
    <tbody class="text-center">{{$data.length}}
        <tr ng-repeat="team in $data">
            <td class="{{resolveHTML(team.position)}}" data-title="''" sortable="'sort_position'">
                {{team.position}}
            </td>
            <td data-title="'Clasificación'" sortable="'sort_name'">
                {{team.name}}
            </td>

            <!--Total Stats-->
            <td data-title="'PJ'" sortable="'sort_pj'">
                {{team.pj}}
            </td>
            <td data-title="'PG'" sortable="'sort_pg'" >
                {{team.pg}}
            </td>
            <td data-title="'PE'" sortable="'sort_pe'" >
                {{team.pe}}
            </td>
            <td data-title="'PP'" sortable="'sort_pp'" >
                {{team.pp}}
            </td>
            <td data-title="'GF'" sortable="'sort_gf'">
                {{team.gf}}
            </td>
            <td data-title="'GC'" sortable="'sort_gc'">
                {{team.gc}}
            </td>
            <td data-title="'PT'" sortable="'sort_pt'">
                {{team.pt}}
            </td>


        </tr>
    </tbody>
</table>

$data has no any data, but if I try {{dataTable}} I have all the data correctly loaded. $ data没有任何数据,但是如果我尝试{{dataTable}},我将正确加载所有数据。 Can anybody help me with this?, maybe there is something obvious that I am missing, but the point is that the table is creating the amount of rows and columns anyway but empty, it is very weird. 有人可以帮我这个忙吗?也许也许我遗漏了一些明显的东西,但问题是表无论如何都在创建行和列的数量,但是却是空的,这很奇怪。

I think you are not storing $data anywhere. 我认为您不在任何地方存储$ data。 I cannot find $scope.data in your js file. 我在您的js文件中找不到$ scope.data。

After long search I figured out the problem was pretty easy, was about the CamelCase notation. 经过长时间的搜索,我发现问题很简单,与CamelCase表示法有关。 The problem is when you send info from a web api, if you don´t set custom notation for the parameters, they will be sent with the first letter capitalized. 问题是,当您从Web API发送信息时,如果您未设置参数的自定义表示法,则参数将以首字母大写的形式发送。 So, we have two chooses here, establish custom notation or just use in our HTML the first letter capitalized. 因此,我们在这里有两种选择,建立自定义符号或仅在HTML中使用首字母大写。 I hope this will help somebody in the future! 我希望这会在将来对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM