排序和过滤器不起作用 - Ngtable

时间:2017-04-08 15:31:47

标签: javascript asp.net angularjs ngtable

我开始使用ngtables时遇到了一些困难。现在,分页是正确的,但我无法进行排序和过滤。既定的初始排序顺序也不起作用任何建议? 感谢

脚本

var app = angular.module('ruyapp', ['ngTable']);

app.controller('equipasCtrl', function ($scope, $filter, NgTableParams) {

var tamanho = $("#Tamanho").val();

$scope.init = function (equipasCT) {
    $scope.equipas = equipasCT;
};

$scope.equipasTable = new NgTableParams({
    page: 1,
    sorting:{Nome: 'asc'},
    count: $("#Npagina").val()
}, {
    total: tamanho,
    getData: function ($defer, params) {
        $scope.data = params.sorting() ? $filter('orderBy')($scope.equipas, params.orderBy()) : $scope.equipas;
        $scope.data = params.filter() ? $filter('filter')($scope.data, params.filter()) : $scope.data;
        $scope.data = $scope.equipas.slice((params.page() - 1) * params.count(), params.page() * params.count());
        $defer.resolve($scope.data);
    }
});
});

查看

<div ng-app="ruyapp" ng-controller="equipasCtrl" ng-init="init(@Newtonsoft.Json.JsonConvert.SerializeObject(Model))">

<table class="table" ng-table="equipasTable" show-filter="true">
            <tbody>
                <tr ng-repeat="row in $data">

                    <td data-title="'Nome'" sortable="'Nome'" filter="{ 'Nome': 'text'}">
                        {{row.Nome}}
                    </td>
                    <td data-title="'Country'" sortable="'Country'" filter="{ 'Country': 'text'}">
                        {{row.Country}}
                    </td>

                    <td data-title="'Abreviatura'" sortable="'Abreviatura'" filter="{ 'Abreviatura': 'text'}">
                        {{row.Abreviatura}}
                    </td>
                    <td>
                        <a href="Equipas/Edit/{{row.EquipaID}}"> Editar</a> |
                        <a href="Equipas/Details/{{row.EquipaID}}"> Detalhes</a> |
                        <a href="Equipas/Delete/{{row.EquipaID}}"> Eliminar</a>
                    </td>
                </tr>

            </tbody>
    </table>


</div>

View

1 个答案:

答案 0 :(得分:3)

这是一个工作示例。 http://codepen.io/mkl/pen/KWjbKV 这就是我改变getData周围代码以使排序和过滤工作的方式。

{
    //total: $scope.data.length,
    getData: function(params) {
      var data = $scope.equipas;
      data = params.filter() ? $filter('filter')(data, params.filter()) : data;
      data = params.orderBy() ? $filter('orderBy')(data, params.orderBy()) : data;
      params.total(data.length);
      data = data.slice((params.page() - 1) * params.count(), params.page() * params.count());
      return data;
    }