Angularjs 使用简单的复选框编辑筛选器

Angularjs 使用简单的复选框编辑筛选器,angularjs,html-table,Angularjs,Html Table,我想通过选中一个按钮来选择显示非活动用户 以下是用户数组: $scope.users = [ {firstname: 'Paul', inactive: true}, {firstname: 'Mark', inactive: false}, {firstname: 'Maggie', inactive: false}, {firstname: 'Lucy', inactive: true} ]; 以及要显示它的表: <

我想通过选中一个按钮来选择显示非活动用户

以下是
用户
数组:

$scope.users = [
    {firstname: 'Paul', inactive: true},
    {firstname: 'Mark', inactive: false},
    {firstname: 'Maggie', inactive: false},                   
    {firstname: 'Lucy', inactive: true}
];
以及要显示它的表:

<table>
    <thead>
        <th>Firstname</th>
        <th>Activity</th>
    </thead>
    <tbody>
        <tr ng-repeat="user in users | filter: ??">
            <td>{{user.firstname}}</td>
            <td>{{user.inactive}}</td>
        </tr>
    </body>
</table>

<input type="checkbox" ng-click="showInact()">Show inactives

名字
活动
{{user.firstname}
{{user.inactive}
不作为
我正在学习AngularJS,我没有找到一个有趣的方法。你能帮我找到解决办法吗


谢谢:)

最简单的方法!!您可以在单击事件时设置过滤器的值

<tr ng-repeat="user in users | filter:filters">
    <td>{{user.firstname}}</td>
    <td>{{user.inactive}}</td>
</tr>

{{user.firstname}
{{user.inactive}
单击“过滤器设置为”复选框

<input type="checkbox" ng-click="filters.inactive = true">

首先,您需要将控制器名instade设置为“myCtrl.js”,无论您的控制器名是什么。
最简单的方法!!您可以在单击事件时设置过滤器的值

<tr ng-repeat="user in users | filter:filters">
    <td>{{user.firstname}}</td>
    <td>{{user.inactive}}</td>
</tr>

{{user.firstname}
{{user.inactive}
单击“过滤器设置为”复选框

<input type="checkbox" ng-click="filters.inactive = true">

首先,您需要将控制器名instade设置为“myCtrl.js”,无论您的控制器名是什么。
最简单的方法!!您可以在单击事件时设置过滤器的值

<tr ng-repeat="user in users | filter:filters">
    <td>{{user.firstname}}</td>
    <td>{{user.inactive}}</td>
</tr>

{{user.firstname}
{{user.inactive}
单击“过滤器设置为”复选框

<input type="checkbox" ng-click="filters.inactive = true">

首先,您需要将控制器名instade设置为“myCtrl.js”,无论您的控制器名是什么。
最简单的方法!!您可以在单击事件时设置过滤器的值

<tr ng-repeat="user in users | filter:filters">
    <td>{{user.firstname}}</td>
    <td>{{user.inactive}}</td>
</tr>

{{user.firstname}
{{user.inactive}
单击“过滤器设置为”复选框

<input type="checkbox" ng-click="filters.inactive = true">

首先,您需要将控制器名instade设置为“myCtrl.js”,无论您的控制器名是什么。

只需这样做:

1)在您的控制器上:

$scope.showInactive = false;

$scope.filterInact = function(item)
{
    return item.inactive === $scope.showInactive;
};

$scope.showInact = function() {
   $scope.showInactive = !$scope.showInactive;
} 
 <tr ng-repeat="user in users | filter:filterInact">
    <td>{{user.firstname}}</td>
    <td>{{user.inactive}}</td>
  </tr>
2)设置过滤器:

$scope.showInactive = false;

$scope.filterInact = function(item)
{
    return item.inactive === $scope.showInactive;
};

$scope.showInact = function() {
   $scope.showInactive = !$scope.showInactive;
} 
 <tr ng-repeat="user in users | filter:filterInact">
    <td>{{user.firstname}}</td>
    <td>{{user.inactive}}</td>
  </tr>

{{user.firstname}
{{user.inactive}
只需这样做:

1)在您的控制器上:

$scope.showInactive = false;

$scope.filterInact = function(item)
{
    return item.inactive === $scope.showInactive;
};

$scope.showInact = function() {
   $scope.showInactive = !$scope.showInactive;
} 
 <tr ng-repeat="user in users | filter:filterInact">
    <td>{{user.firstname}}</td>
    <td>{{user.inactive}}</td>
  </tr>
2)设置过滤器:

$scope.showInactive = false;

$scope.filterInact = function(item)
{
    return item.inactive === $scope.showInactive;
};

$scope.showInact = function() {
   $scope.showInactive = !$scope.showInactive;
} 
 <tr ng-repeat="user in users | filter:filterInact">
    <td>{{user.firstname}}</td>
    <td>{{user.inactive}}</td>
  </tr>

{{user.firstname}
{{user.inactive}
只需这样做:

1)在您的控制器上:

$scope.showInactive = false;

$scope.filterInact = function(item)
{
    return item.inactive === $scope.showInactive;
};

$scope.showInact = function() {
   $scope.showInactive = !$scope.showInactive;
} 
 <tr ng-repeat="user in users | filter:filterInact">
    <td>{{user.firstname}}</td>
    <td>{{user.inactive}}</td>
  </tr>
2)设置过滤器:

$scope.showInactive = false;

$scope.filterInact = function(item)
{
    return item.inactive === $scope.showInactive;
};

$scope.showInact = function() {
   $scope.showInactive = !$scope.showInactive;
} 
 <tr ng-repeat="user in users | filter:filterInact">
    <td>{{user.firstname}}</td>
    <td>{{user.inactive}}</td>
  </tr>

{{user.firstname}
{{user.inactive}
只需这样做:

1)在您的控制器上:

$scope.showInactive = false;

$scope.filterInact = function(item)
{
    return item.inactive === $scope.showInactive;
};

$scope.showInact = function() {
   $scope.showInactive = !$scope.showInactive;
} 
 <tr ng-repeat="user in users | filter:filterInact">
    <td>{{user.firstname}}</td>
    <td>{{user.inactive}}</td>
  </tr>
2)设置过滤器:

$scope.showInactive = false;

$scope.filterInact = function(item)
{
    return item.inactive === $scope.showInactive;
};

$scope.showInact = function() {
   $scope.showInactive = !$scope.showInactive;
} 
 <tr ng-repeat="user in users | filter:filterInact">
    <td>{{user.firstname}}</td>
    <td>{{user.inactive}}</td>
  </tr>

{{user.firstname}
{{user.inactive}

我个人喜欢使用过滤功能过滤数据。这种方法非常灵活,可以轻松地与其他表单变量交互。从中,筛选器表达式可以是:

函数(值、索引)
:谓词函数可用于编写 任意过滤器。为数组的每个元素调用该函数。 最后的结果是谓词所包含的元素的数组 为返回true

因此,示例筛选函数可能如下所示:

$scope.filterFunc = function (user) {
  //if the checkbox is checked show everyone, else only those that aren't inactive
  return $scope.showInactives  || !user.inactive;
};
HTML将被更改以适应过滤器功能。具体来说,我将复选框绑定到
filterFunc
函数在其逻辑中使用的
$scope
变量(
$scope.showInactives
)。当然,
ng repeat
正在使用名为
filterFunc
的函数

<input type="checkbox" ng-model="showInactives"/>Show inactive users
<br/>
<table>
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Activity</th>
      </tr>
    </thead>
    <tbody>
      <tr ng-repeat="user in users | filter:filterFunc">
        <td>{{user.firstname}}</td>
        <td>{{user.inactive}}</td>
      </tr>
    </tbody>
</table>
显示非活动用户

名字 活动 {{user.firstname} {{user.inactive}
如果你需要任何其他复杂的逻辑,过滤函数可以让你有很大的自由度。它只需要返回布尔值(true或false)


与筛选无关,我还必须通过将
放入表行

来修复表的HTML。我个人喜欢使用筛选函数来筛选数据。这种方法非常灵活,可以轻松地与其他表单变量交互。从中,筛选器表达式可以是:

函数(值、索引)
:谓词函数可用于编写 任意过滤器。为数组的每个元素调用该函数。 最后的结果是谓词所包含的元素的数组 为返回true

因此,示例筛选函数可能如下所示:

$scope.filterFunc = function (user) {
  //if the checkbox is checked show everyone, else only those that aren't inactive
  return $scope.showInactives  || !user.inactive;
};
HTML将被更改以适应过滤器功能。具体来说,我将复选框绑定到
filterFunc
函数在其逻辑中使用的
$scope
变量(
$scope.showInactives
)。当然,
ng repeat
正在使用名为
filterFunc
的函数

<input type="checkbox" ng-model="showInactives"/>Show inactive users
<br/>
<table>
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Activity</th>
      </tr>
    </thead>
    <tbody>
      <tr ng-repeat="user in users | filter:filterFunc">
        <td>{{user.firstname}}</td>
        <td>{{user.inactive}}</td>
      </tr>
    </tbody>
</table>
显示非活动用户

名字 活动 {{user.firstname} {{user.inactive}
如果你需要任何其他复杂的逻辑,过滤函数可以让你有很大的自由度。它只需要返回布尔值(true或false)


与筛选无关,我还必须通过将
放入表行

来修复表的HTML。我个人喜欢使用筛选函数来筛选数据。这种方法非常灵活,可以轻松地与其他表单变量交互。从中,筛选器表达式可以是:

函数(值、索引)
:谓词函数可用于编写 任意过滤器。为数组的每个元素调用该函数。 最后的结果是谓词所包含的元素的数组 为返回true

因此,示例筛选函数可能如下所示:

$scope.filterFunc = function (user) {
  //if the checkbox is checked show everyone, else only those that aren't inactive
  return $scope.showInactives  || !user.inactive;
};
HTML将被更改以适应过滤器功能。具体来说,我将复选框绑定到
filterFunc
函数在其逻辑中使用的
$scope
变量(
$scope.showInactives
)。当然,
ng repeat
正在使用名为
filterFunc
的函数

<input type="checkbox" ng-model="showInactives"/>Show inactive users
<br/>
<table>
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Activity</th>
      </tr>
    </thead>
    <tbody>
      <tr ng-repeat="user in users | filter:filterFunc">
        <td>{{user.firstname}}</td>
        <td>{{user.inactive}}</td>
      </tr>
    </tbody>
</table>
显示非活动用户

名字 活动 {{user.firstname} {{user.inactive}
如果