Angular JS presents an exciting feature called Multiple Filters, which allows

I have a data representation application that displays information in table format with columns

id, name, price, quantity

The data is presented using ng-repeat. You can view it on this Plunker

<body ng-controller="myController">
<h1>Data</h1>

<table>
  <tr>
    <th>ID</th>
    <th>NAME</th>
    <th>PRICE</th>
    <th>QUANTITY</th>
  </tr>
  <tr ng-repeat="item in myData">
    <td>{{ item.id }}</td>
    <td>{{ item.name }}</td>
    <td>{{ item.price }}</td>
    <td>{{ item.quantity }}</td>
  </tr>
</table>

My goal is to implement multiple filters in the ng-repeat for this table

a) Filter by 'Name'

b) Filter by 'Price' OR 'Quantity'

This means that the table should be filtered by either

i) 'Name' and 'Price'

ii) OR 'Name' and 'Quantity'

The Quantity filter should be inactive when the Price filter is active, and vice versa.

I will provide 3 input fields for the filter parameters.

How do I implement these filters in the ng-repeat in HTML to achieve this?

Answer №1

To optimize your data filtering process, consider creating a personalized filter with a custom filterObject. This filterObject should contain all the necessary filter options for efficient data manipulation.

<tr ng-repeat="item in myData | myCustomFilter:filterObject">
  <td>{{ item.id }}</td>
  <td>{{ item.name }}</td>
  <td>{{ item.price }}</td>
  <td>{{ item.quantity }}</td>
</tr>

Here's an example of how your filterobject could be structured:

$scope.filterObject = {
    id: false,
    name: true,
    price: false,
    quantity: true
}

Within the implementation of myCustomFilter :

app.filter('myCustomFilter', function(items, filterObject) {
    // Implement logic to filter items based on enabled filters
});

Answer №2

This is a demo of myDatafilter function in JavaScript

myDatafilter = function () {};

<h1>Filtered Data Results</h1>
<input type="text" ng-model="myDatafilter"/>
<tr ng-repeat="item in myData | filter:myDatafilter">

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

Ways to include multiple pieces of data in a JQuery Mobile List view

Obtaining JSON data (list of available Hotels within a certain distance) and extracting the following information in JSON format: Name of Hotels, Distance of Hotel from our current location, number of rooms. There might be multiple Hotels within the specif ...

Troubleshooting the issue: Uncaught TypeError when trying to read property 'substr' of undefined while passing parameters in JavaScript

In a JavaScript code in my JS file, I have the following function: var obj, mtl; init(); animate(); var MYLIBRARY = MYLIBRARY || (function () { var _args = {}; // private return { init: function (Args) { _args = Args; ...

Ui Bootstrap (angularjs) sidebar is not functioning properly

I am encountering an issue with a sidenav that I am in the process of creating for one of my projects. The goal is to develop a side menu that shifts the content to the right when opened and returns it to the left when closed, essentially functioning as a ...

What is the best way to eliminate a vertical line from the canvas in react-chartjs-2?

Can someone please lend me a hand? I've been working on a project in React JS that involves using react-chartjs-2 to display charts. I'm trying to incorporate a range slider for the chart to manipulate values on the x-axis, as well as two vertic ...

Display specific column using ng-repeat

When attempting to filter an array of objects using ng-repeat based on a specific key or column, the key/column for filtering is determined by a selection from a dropdown menu. data-ng-model="selectedValue" // -> item.name The search term is entered i ...

Show the meta-field mp3 and place it in a lightbox container on the portfolio page

My Current Portfolio Gallery Setup: Currently, my portfolio gallery is displayed in a masonry grid format using the JIG plugin. This grid showcases images from my custom post_type. When clicking on a thumbnail, a lightbox pops up showing the full photo. T ...

What is the specific table element compatible with Polymer3?

I stumbled upon iron-data-table (), but it relies on bower which is used in Polymer2. However, I am working with npm in Polymer3. Is there a suitable table element or an alternative solution compatible with Polymer3? ...

All the details from this run are available in the npm run dev log on Ubuntu

Good day! I've been working on deploying a page built in vue.js, and after uploading all the files successfully, I encountered an issue when trying to run "npm run dev." No matter what I try, including re-installing npm dependencies and starting from ...

Using API call responses to initiate a render in React

As a newcomer to React, I'm facing an issue with passing the results of my API call to another component file in order to trigger a render. Can anyone provide a straightforward explanation of what steps I need to take? In my code, I make a call to th ...

Ways to exchange information among Vue components?

My role does not involve JavaScript development; instead, I focus on connecting the APIs I've created to front-end code written in Vue.js by a third party. Currently, I am struggling to determine the hierarchy between parent and child elements when ac ...

Submit information from an HTML form to a PHP script and then send the response back to the client using AJAX,

Looking to run a PHP script using AJAX or JavaScript from an HTML form and then receive the results on the HTML page. This is what my changepsw.php file looks like: <?php // PHP script for changing a password via the API. $system = $_POST['syst ...

Establish a connection with the ejabberd server as well as the application server

I manage an ejabberd server and am currently working on implementing a chat module within my Angular/NodeJS application. Within my setup, the Angular app directly connects to the chat server. I have a roster consisting of 100 contacts, some marked as onli ...

The jQuery script operates just one time

Recently, I encountered a small issue with my script. It only seems to work once - after that, I have to refresh the page in order to remove a favorite article (which the script is supposed to do). $("a.fav_no").on('click', function () { ...

Encountering the issue of "Cannot read properties of undefined" while attempting to pass data to a prop

Currently, I am developing a Vue application that heavily relies on charts. The issue lies in the fact that I am fetching data from the database individually for each chart component, resulting in multiple calls and a slower page load time. I am looking to ...

React encountered an error: Unable to destructure the property 'id' of '_ref' as it has been defined

After spending several hours trying to solve this issue, I am completely stuck. The console shows that the patch request successfully updates the information, but then my map function breaks, leading to a blank page rendering. Here is the problematic comp ...

Challenges with Property Decorators in Angular 6

Hello there, I've been testing out this sample decorator code and encountered an issue that I can't seem to figure out. The error message I received was "cannot read property 'firstname' of undefined". It appears that the 'this&apo ...

When you click anywhere on the page, JavaScript's method __dopostback is triggered

I'm encountering an issue in my asp.net application where I have an html table. Specifically, when a user clicks on a td element within the table, I use JavaScript to store the id of that td element in a hidden field. Afterwards, I trigger __dopostbac ...

Surprising Denials Following the Launch of Karma Using NgUpgrade

Currently in the process of implementing ngupgrade into our AngularJS application and consistently encountering unexpected rejection errors while Karma is initializing. The functionality of my app with ngupgrade is running smoothly, but the issue lies wit ...

HTML5 Boilerplate and optimizing the critical rendering path by deferring scripts and styles

Previously, I constructed my website layouts using the HTML5 Boilerplate method: incorporating styles and Modernizr in the head section, jQuery (either from Google CDN or as a hosted file) along with scripts just before the closing body tag. This is an exa ...

Trouble with Angular.js HTTP POST communicating with MVC async Task<ActionResult> method

I'm having an issue trying to invoke an Async method in the MVC controller (like Login) from an Angular client, but the calls are failing. I've also attempted it using the Google Postman tool. // // POST: /Account/Login [HttpPost] ...