Filter ng-repeat using OR condition for property

I'm trying to filter a list based on a model value entered in a text box.

For example:

 var person={};
 person.Id=1;
 person.Name="Test 1";
 person.PetName="Rest 1"

 var persons=[];
 persons.push(person);

 person.Id=2;
 person.Name="Test ds";
 person.PetName="Rest sd";
 persons.push(person);

The Persons array contains multiple individuals. On my HTML page, I have a search box.

<input type="text" ng-model="searchText" placeholder="search by Name and pet name"/>

<div ng-repeat="person in persons | filter : {$:searchText}">
<div>{{person.Name}}</div>
</div>

Scenario 1:

When I enter 2 in the text box, one result appears. However, I only want to filter by name and pet name.

Is there a way to achieve this without using a custom filter? Does the filter directive allow for OR conditions on properties?

Any help would be appreciated. Thank you.

Answer №1

Personally, I find that utilizing a custom "OR" filter is the best approach. However, an alternative method would be to employ the built-in filter twice (for each OR condition) and then combine the results using UNION:

angular.module('app', []).controller('ctrl', function($scope){
  $scope.persons = [
    {Id:95, Name:"Tom", PetName: "Pet1"},
    {Id:96, Name:"Sam", PetName: "Pet2"},
    {Id:97, Name:"Max", PetName: "Pet3"},
    {Id:98, Name:"Henry", PetName: "Pet4"}
  ]
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app='app' ng-controller='ctrl'>
  <input type='text' ng-model='search'/>
  <div ng-repeat='person in temp = (persons | filter : {Name: search})'>
    {{person | json}}
  </div>
  <div ng-repeat='person in persons | filter : {PetName: search}' ng-if='temp.indexOf(person) == -1'>
    {{person | json}}
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing the Power of GrapesJs in Vue3

Recently, I attempted to integrate the GrapesJS editor into my Vue.js project, but encountered some difficulties. The editor was not visible in the browser, and the designated tag for the editor appeared empty. Here is my editor configuration: <template ...

Connect the <div> element to the Angular controller's element

We are faced with a rather intricate model within an AngularJS controller: function controller($scope) { $scope.model = { childmodel1: { childmodel2: { property1: 'abc', property2: 3, ...

Displaying spinner until the entire section has finished loading

In order to display a loading spinner until all images in a specific section of the page have fully loaded, I am utilizing Django, jQuery, and Ajax technologies. The HTML structure consists of two main divs: On the left side, there is a table, while on t ...

Navigate array in vue-chart.js

I've been utilizing Vue-chartjs with Laravel 5.7 for my project. The goal: I aim to pass an array to Vue so that I can dynamically generate a chart by looping through specific values. My approach so far: Here's the array I'm working with ...

Struggling with Implementing jQuery for Triggering Multiple Functions on a Single Change Event?

I am currently developing jQuery functions that create dependencies between the css classes of different inputs in a web form. For example, when a specific input has a certain value, the "hide" class is removed from another input. One working example of t ...

What steps should I take to ensure that the login page functions properly?

Below is the HTML code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Login Pag ...

Caution: The React Hook useEffect is missing a required dependency

What is the best way to eliminate the warning "React Hook useEffect has a missing dependency" while developing my code? Here is a snippet of the code that triggers the warning: useEffect(() => { if(inactive){ document.querySelect ...

Utilizing Vuejs and ElementUi to enhance slot filtering capabilities

I am facing an issue with a table where one of the columns is using slot-scope, and I am struggling to include that column data into the filters option. Code Component filter input <el-input v-model="filters[0].value" placeholder="Type t ...

Error: Attempting to call vector.subSelf method, which does not exist

Currently, I am experimenting with creating a tank game inspired by Isaac Sukin's "Nemesis" game for AngelHack. The specific change I want to make involves using a model of a tank ("Tank.obj") instead of the default cube used as an enemy. However, I ...

Dynamically alter routing in Express by retrieving route paths from a JSON document

My goal is to dynamically update my route in Express using a JSON file that stores the specific link. The JSON data resides in articles.js and appears as follows: title: 'title1', link: 'title2', creator: 'user1', crea ...

Is there a way to utilize a single function on two separate div elements?

Looking for a way to optimize my code that contains the addRow() and deleteRow() functions. Currently, I have duplicated these functions for both radio buttons and checkboxes. Is there a more efficient way to achieve this? function addRow(tableID) { ...

"Learn the trick to concealing a modal and unveiling a different one with the power of jquery

Whenever I try to open a modal, then click on a div within the modal in order to close it and open another one, I encounter an issue. The problem is that upon closing the first modal and attempting to display the second one, only the background of the seco ...

Ajax polling ceases the polling process when an internet connection is lost

My current setup involves a continuous ajax polling cycle where messages are pulled, processed, a wait period occurs, and then the process is repeated. Everything runs smoothly until a user disconnects from the internet, whether it be due to a simple actio ...

Contrasting bracket notation property access with Pick utility in TypeScript

I have a layout similar to this export type CameraProps = Omit<React.HTMLProps<HTMLVideoElement>, "ref"> & { audio?: boolean; audioConstraints?: MediaStreamConstraints["audio"]; mirrored?: boolean; screenshotFormat?: "i ...

Trouble displaying REACT.jsx in browser

I'm currently learning React and struggling to get it running smoothly. HTML function Person(){ return ( <div class="person"> <h1>Max</h1> <p>Your Age: 28</p> </div> ); } ...

The animation is not updating quickly enough

I'm working on a navigation bar that sticks to the top of the page. As the user scrolls down, I want the bar to shrink, and when they scroll back up, it should return to its original size. Issue: The problem I'm facing is that when the user quic ...

Tips on using the ref attribute in Vue.js to focus on an input element

In my Vue.js component for 2FA, I have implemented the following structure: <template> <div :class="onwhite ? 'on-white' : ''"> <input :id="`n1`" ref="n1" v-model=&quo ...

What is the best way to manage the "open link in a new tab" action?

I am currently working on a project that involves displaying a series of resources on a web page. Each resource is stored as a record in a database with two fields: "url" and "visited." One issue I have encountered is that when a user clicks on a resource ...

I am looking to change the state of only the element that is being moused over in a React Hooks useState function, rather than changing the

Currently, I have a component from line 61-67 that controls the state of an editIcon. The issue is that it changes the state values for all items in the column, rather than just the specific item or row it should apply to. When hovering over a span element ...

What is the best way to deliver client/index.html using server/app.js?

Here is my current file structure: - simulated-selves - client - index.html - server - app.js The goal is to serve the user index.html when they visit the / route. // server/app.js app.get('/', function(req, res) { res.sendFile( ...