Angular UI-grid过滤器代码不起作用

时间:2015-07-29 14:03:31

标签: javascript angularjs angular-ui-grid

以下代码与教程Google中的代码类似,但它不显示数据网格,我不知道为什么

我检查了plunk并且找不到任何错误,下面是我的代码

var app = angular.module('app', ['ngTouch', 'ui.grid'])

app.controller('MainCtrl', function($scope) {

    $scope.gridOptions = {
        enableFiltering: false,
        onRegisterApi: function(gridApi) {
            $scope.gridApi = gridApi;
            $scope.gridApi.grid.registerRowsProcessor($scope.singleFilter, 200);
        },
        columnDefs: [
            {
                field: 'name'
            }, {
                field: 'last'
            }, {
                field: 'address'
            }, {
                field: 'name1'
            }, {
                field: 'last1'
            }, {
                field: 'address1'
            }, {
                field: 'last45'
            }
        ],
        data: [
            {
                "name": "test",
                "last": "test2",
                "address": "test3",
                "name1": "test",
                "last1": "test2",
                "address1": "test3",
                "last45": "op"
            }, {
                "name": "test",
                "last11": "test2",
                "address": "test2"
            }, {
                "name": "test",
                "last": "test2",
                "address": "test3"
            }, {
                "name": "test",
                "last": "test2",
                "address": "test3"
            }, {
                "name": "test",
                "last": "test2",
                "address": "test3"
            }, {
                "name": "test",
                "last": "test2",
                "address": "test3"
            }, {
                "name": "test",
                "last": "test2",
                "address": "test3"
            }
        ]
    };


    $scope.filter = function() {
        $scope.gridApi.grid.refresh();
    };

    $scope.singleFilter = function(renderableRows) {
        var matcher = new RegExp($scope.filterValue);
        renderableRows.forEach(function(row) {
            var match = false;
            ['name', 'last', 'address'].forEach(function(field) {
                if (row.entity[field].match(matcher)) {
                    match = true;
                }
            });

            if (!match) {
                row.visible = false;
            }
        });
        return renderableRows;
    };
})

1 个答案:

答案 0 :(得分:0)

您的问题似乎是因为数据。由于您的数据没有每行的所有列。您必须修复singleFilter方法以检查该情况。

$scope.singleFilter = function(renderableRows) {
var matcher = new RegExp($scope.filterValue);
renderableRows.forEach(function(row) {
  var match = false;
  ['name', 'last', 'address'].forEach(function(field) {

    if (row.entity[field] && row.entity[field].match(matcher)) {
      match = true;
    }
  });
  if (!match) {
    row.visible = false;
  }
});
return renderableRows;
};

检查此plnk是否有工作版本http://plnkr.co/edit/l7Uc3H0CnoyRkaHdNZ9P?p=preview