3

I have a table with rows which are displayed with ng-repeat. Also I have a <select>. My table rows filtered by property, selected in <select>. Here's code:

Controller:

$scope.transactionTypes = [
  "All",
  "Bonus received",
  "Invoice payment",
  "Taxes",
  "Credit transfers",
  "Withdrawals",
  "Cancelled transactions"
];

$scope.tableFilter = {};

$scope.transactions = [
   {
      "transactionType" : "Taxes"
   },
   {
      "transactionType" : "Bonus received"
   }
]

Html:

<select ng-model="tableFilter.transactionType"
        ng-options="transactionType for transactionType in transactionTypes">
</select>

<table>
    <tr ng-repeat="item in (transactions | filter:{transactionType:tableFilter.transactionType)">
       <td>{{item.transactionType}}</td>
    </tr>
</table>

I was simplified my code for you as much as I can. I hope that I described my situation clearly enough.

My question is: what is the easiest way to show all transactions (with any transactionType) when I choose 'All' property in <select>? How to clear this filter by selecting 'All' in <select>?

Thanks.

5 Answers 5

2

Set value of transactionType to '' if transactionType is 'All' and no filter will be applied.

<select ng-model="tableFilter.transactionType" ng-options="'All' == transactionType ? '' : transactionType as transactionType for transactionType in transactionTypes"></select>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I've found this the easiest way to fix the problem
1
 <tr ng-repeat="item in (transactions | filter:tableFilter.transactionType:checkForAll">
       <td>{{item.transactionType}}</td>
 </tr>


$scope.checkForAll = function(option, currentValue)
{
 if(currentValue == $scope.transactionTypes[0])
    return true;
 else
    return option == currentValue;    
}

try that.

Comments

1

You can write a custom filter that can take a variable bound to "Select All".

More information here: https://docs.angularjs.org/guide/filter

I can provide an example too, if this doesn't solve your problem.

Comments

1

I got it to work using the below. There might be a better solution though...

index.html:

<!doctype html>
<html lang="en" ng-app="app">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body ng-controller="ctrl">


    <select ng-model="tableFilter.transactionType"
        ng-options="transactionType for transactionType in transactionTypes">
    </select>

    <table>
        <tr ng-repeat="item in (transactions | filter:myFilter)">
           <td>{{item.transactionType}}</td>
        </tr>

    </table>

    <script src= "angular.js"></script>
    <script src= "app.js"></script>
</body>
</html>

app.js:

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

app.controller('ctrl', function($scope) {
    $scope.transactionTypes = [
      "All",
      "Bonus received",
      "Invoice payment",
      "Taxes",
      "Credit transfers",
      "Withdrawals",
      "Cancelled transactions"
    ];

    $scope.useFilter = false;

    $scope.tableFilter = {};

    $scope.transactions = [
       {
          "transactionType" : "Taxes"
       },
       {
          "transactionType" : "Bonus received"
       }
    ];

    $scope.myFilter = function(tran) {
        //debugger;
        if ($scope.tableFilter.transactionType == "All" || !$scope.tableFilter.transactionType)
            return true;
        else if($scope.tableFilter.transactionType == tran.transactionType)
            return true;
        else
            return false;
    }
});

Comments

0

You can set the fitter for all to be empty string ''. When all will be selected the filter will be set to empty string and this will make the entire transactions visible.

Your custom filter looks fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.