Angularjs 不同表列上的不同筛选器

Angularjs 不同表列上的不同筛选器,angularjs,angularjs-filter,Angularjs,Angularjs Filter,我想创建一个表,根据不同的过滤器对列的数据进行格式化 假设我有一个数组对象,如下所示: $scope.components = [ {lastName:'Bob', firstName:'John',netWorth:1000}, {lastName:'Foo', firstName:'Bar',netWorth:2000} ]; app.filter('custom',

我想创建一个表,根据不同的过滤器对列的数据进行格式化

假设我有一个数组对象,如下所示:

$scope.components = [
                     {lastName:'Bob', firstName:'John',netWorth:1000},
                     {lastName:'Foo', firstName:'Bar',netWorth:2000}
                   ];
app.filter('custom', ['$filter', function ($filter) {
  return function (value, type) {
    return $filter(type)(value);
  }
}]);
  $scope.filterArray = {
    'lastName': 'uppercase',
    'firstName': 'lowercase',
    'netWorth': 'currency'
  };
<tr ng-repeat="component in components">
  <td ng-repeat="column in tableColumns">{{component[column] | custom : filterArray[column]}}</td>
</tr>
现在我想使用
ng repeat
将数据填充到表中。我是这样做的:

            <tr ng-repeat="component in components">
              <td ng-repeat="column in tableColumns">
                {{component[column]}}
              </td>
            </tr>
其中,
$scope.filterArray
是我想要的过滤器字符串数组,如
'uppercase'
'currency'
,等等。这个字符串数组也应该与tableColumn数组中的元素一致。例如,要获得上面提到的所需格式,我必须声明
$scope.arrayFilter
如下:
$scope.arrayFilter=['uppercase'、'lowercase'、'currency']


有什么想法吗?

你可以这样做:

<td ng-repeat="column in tableColumn>
  <label ng-if="column === 1">
     {{component[column] | uppercase}}
  </label>
  <label ng-if="column === 2">
     {{component[column] | lowercase}}
  </label>

...

</td>

我认为您可以为将使用
$filter
服务的内容编写自己的过滤器

因此,您的过滤器将如下所示:

$scope.components = [
                     {lastName:'Bob', firstName:'John',netWorth:1000},
                     {lastName:'Foo', firstName:'Bar',netWorth:2000}
                   ];
app.filter('custom', ['$filter', function ($filter) {
  return function (value, type) {
    return $filter(type)(value);
  }
}]);
  $scope.filterArray = {
    'lastName': 'uppercase',
    'firstName': 'lowercase',
    'netWorth': 'currency'
  };
<tr ng-repeat="component in components">
  <td ng-repeat="column in tableColumns">{{component[column] | custom : filterArray[column]}}</td>
</tr>
现在,您可以通过下一种方式使用它:
{{value | custom:filterName}
,例如:

{{component[column] | custom : 'uppercase'}}
现在您只需要创建一个对象,该对象将包含列名称及其过滤器名称,如下所示:

$scope.components = [
                     {lastName:'Bob', firstName:'John',netWorth:1000},
                     {lastName:'Foo', firstName:'Bar',netWorth:2000}
                   ];
app.filter('custom', ['$filter', function ($filter) {
  return function (value, type) {
    return $filter(type)(value);
  }
}]);
  $scope.filterArray = {
    'lastName': 'uppercase',
    'firstName': 'lowercase',
    'netWorth': 'currency'
  };
<tr ng-repeat="component in components">
  <td ng-repeat="column in tableColumns">{{component[column] | custom : filterArray[column]}}</td>
</tr>
现在,您的HTML代码将如下所示:

$scope.components = [
                     {lastName:'Bob', firstName:'John',netWorth:1000},
                     {lastName:'Foo', firstName:'Bar',netWorth:2000}
                   ];
app.filter('custom', ['$filter', function ($filter) {
  return function (value, type) {
    return $filter(type)(value);
  }
}]);
  $scope.filterArray = {
    'lastName': 'uppercase',
    'firstName': 'lowercase',
    'netWorth': 'currency'
  };
<tr ng-repeat="component in components">
  <td ng-repeat="column in tableColumns">{{component[column] | custom : filterArray[column]}}</td>
</tr>

{{组件[列]|自定义:filterArray[列]}
一切似乎都在演示中运行。希望这对你有帮助

演示: