W
W
Worddoc2016-08-18 19:08:40
JavaScript
Worddoc, 2016-08-18 19:08:40

Filter question in Angular js?

Hello. I started learning Angular through the kudvenkat channel. And one of his videos raised a couple of questions for which there is no one to answer.
The search(item) function has an item argument, which we do not specify anywhere in the html markup, only in js. Question: How does item get any value if we do not specify what should go there?
The search(item) function returns a boolean value. Those. in HTML it turns out 'ng-repeat="elem in obj | filter: TRUE/FALSE">'. If you insert true or false manually, and not through a function, then nothing works.
Question: Why? And why there MAY be a boolean value instead of a string?
Thank you! There is no one to answer these questions, so I ask you!

SEARCH : <input type="text" ng-model="searchText" placeholder="search">
  <table>
    <thead>
      <tr>
        <th ng-click="sortData('name')">Name<div ng-class="getClass('name')"></div></th>
        <th ng-click="sortData('age')">Age<div ng-class="getClass('age')"></div></th>
        <th ng-click="sortData('country')">Country<div ng-class="getClass('country')"></div></th>
        <th ng-click="sortData('salary')">Salary<div ng-class="getClass('salary')"></div></th>
        <th ng-click="sortData('dateOfBirth')">DateOfBirth<div ng-class="getClass('dateOfBirth')"></div></th>
      </tr>
    </thead>
    <tbody>
      <tr ng-repeat="elem in obj | filter:search">
        <td>{{elem.name }}</td>
        <td>{{elem.age}}</td>
        <td>{{elem.country }}</td>
        <td>{{elem.salary | currency: $ : 0}}</td>
        <td>{{elem.dateOfBirth | date:"dd/MM/yy"}}</td>
      </tr>
    </tbody>
  </table>

var obj = [
    {name: 'John',age: 15,country: 'Usa',salary: 33000, dateOfBirth: new Date ('September 09,1996')},
    {name: 'Mary', age: 28, country: 'Russia', salary: 21000, dateOfBirth: new Date ('September 09,1996')},
    {name: 'Alex', age: 21, country: 'Romania', salary: 53300, dateOfBirth: new Date ('September 09,1996')},
    {name: 'Zondo', age: 33, country: 'Italy', salary: 22660, dateOfBirth: new Date ('September 09,1996')}
  ];

  $scope.obj = obj;

$scope.search = function(item) {
    if($scope.searchText == undefined) {
      return true;
    }
    else {
      if(item.name.toLowerCase().indexOf($scope.searchText.toLowerCase()) != -1 || item.country.toLowerCase().indexOf($scope.searchText.toLowerCase()) != -1) {
        return true;
      }
    }
    return false;
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
_
_ _, 2016-08-18
@AMar4enko

My opinion is that they should have thrown out the filter altogether in version 1.3
. It introduces an additional unnecessary abstraction, with it you need to clearly understand that this is not magic. The experience of using it is applicable only to Angular.
There is no problem to make additional state to store the filtered collection and output it already. Update it when some events occur (such as changing form data, etc.)
In my practice (and it is quite extensive), I used filter only at the very beginning.
You have a chance to avoid this and spend time learning really fundamental things.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question