Searching for multiple values using ng-model in AngularJS is a powerful feature that allows

I've looked everywhere for a solution to my problem but I haven't found an exact match. Can anyone lend a hand? Here's the ng-repeat filter list I'm working with:

<ul>   
  <li ng-repeat="f in filterOptions">
  <p>{{f.group}}</p>   
    <ul>
      <li ng-repeat="o in f.options">
        <input id="{{$parent.f.filtername}}{{$index}}" type="checkbox" ng-model="Filter.category[o.title]" ng-true-value="{{o.title}}" ng-false-value=""  />
      </li>
    </ul>
  </li>
</ul>

This is how my JSON looks:

[
  {
    "group": "Product Categories",
    "filtername" : "category",
    "options": [
        {
            "title": "Category A",
            "alt_titles": ["Category B, Category C"]
        },
        {
            "title": "Category 1",
            "alt_titles": ["Category 2, Category 3"]
        }
    ]
  }
]

Currently, I can successfully search for items that fall under o.titles. Now, I need some guidance on how to also search for items belonging to alt_titles.

For example: When I click on Category A, I want to display items from Category B and Category C as well. The same goes for Category 1, which should show items from Category 2 and 3.

I hope my explanation was clear. Any help would be greatly appreciated. Thank you!


Take a look at a working sample HERE

Answer №1

If you're faced with a scenario like this, creating a custom filter is the solution. You can find detailed documentation on how to do this here: https://docs.angularjs.org/tutorial/step_09 For a practical example, check out my implementation here: http://plnkr.co/edit/lzyo5cTMd6FUzYw9JSq8

app.filter('MultiValueFilter',function(){
return function(list, queries){
  var result = [];
  var queries_regex = [];
  angular.forEach(queries, function(query){
    queries_regex.push(new RegExp('^' + escapeRegExp(query),'i'));
  });

  angular.forEach(list, function(item){
    for(var i = 0; i < queries_regex.length; ++i){
      if(queries_regex[i].test(item)){
        result.push(item);
        break;
      }
    }
  });
  return result;
  };
});

In essence, this filter function iterates through each item in the list, tests it against a set of regular expressions, and adds it to the result if it matches any of the expressions.

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

Achieve a Smooth Transition: Utilizing Bootstrap 3 to Create an Alert Box that Fades In upon Click and Fades Out

Incorporating AngularJS and Bootstrap 3 into my web app, there is an "Update" button that saves user changes. Upon clicking the "Update" button, I aim to trigger and display bootstrap's alert box with the message "Information has been saved," followed ...

`Is there a way to choose several radio buttons with varying names using just one label?`

Is there a way to choose multiple radio buttons with different names using just one label? Here's an example of what I'm attempting to accomplish... <input id="1A" type="radio" name="first" value="A">firstA<br> <input id="1B" typ ...

Unable to retrieve data from the database within PHP code

I have successfully built a shopping cart website utilizing SQL, HTML, and PHP. Below is the code snippet for the 'Add to Cart' button: <form method="post" action="cart.php" class="form-inline"> <input type="hidden" value="&apos ...

When an image is loaded, use Jquery to automatically open a new tab, instead of

I need to implement a function where, on loading an image on a page, a new tab opens and starts downloading a file from a specific URL while keeping the original page open. This task is part of a WordPress site with a jQuery section in the backend. I' ...

"Enhance your website with Ajax code that enables automatic refreshing of a PHP file, keeping its values up-to-date

Before diving in, I want to make it clear that my understanding of ajax is limited. I'm currently working on a small application where I have a load.php file that echoes 3 variables: $loadout[0]; $loadout[1]; $loadout[2]; The PHP file is functioning p ...

Tips for hiding a div element until its visibility is toggled:- Set the display property of

Looking for some guidance on this jQuery code I'm working with to create a toggle menu. The goal is to have the menu hidden when the page loads, and then revealed when a button is clicked. However, currently the menu starts off being visible instead o ...

JavaScript-powered server polling

What are some effective strategies for continuously refreshing data from a server using JavaScript in an application with high refresh rate requirements? The front-end is developed using jQuery, while the backend utilizes Java Spring Framework. One exampl ...

What is the method for specifying a JSON object within a DOM element's `data` attribute?

In an attempt to store JSON data within a dataset attribute, I encountered the following HTML structure: The appearance of the HTML is as follows <div data-switch="true" data-json="{'key1': 'value 1'}, {'key2': 'valu ...

Whenever I change the value of a textbox using plain JavaScript, the text becomes hidden from view

I'm struggling to understand why this function is making the textbox value invisible. I attempted changing the hidden field to a visible field, but that didn't solve the issue. Would appreciate some guidance. Here's the complete page code, p ...

No response from jQuery's $.getJSON() function

I'm currently experimenting with jQuery by using a script that I wrote. As a beginner in learning jQuery, I am trying to read data from a .json file and display it in a div. function jQuerytest() { $.getJSON( "books/testbook/pageIndex.json", func ...

Disabling form submission when pressing the enter key

Is there a way to prevent a submit action from occurring when the enter key is pressed within an ASP:TextBox element that triggers an asyncpostback upon text change? Instead, I would like it to click on another button. The Javascript function I am using wo ...

Unable to reset the input value to an empty string

I have created a table with a search bar feature that filters the data when the search button is clicked and resets the filter to show unfiltered data when the clear button is clicked. However, the current input value is not clearing from the display even ...

Firefox not responding to input select dropdown selection

When creating a jQuery submenu box in Firefox, I encountered an issue with the input select dropdown. While it functions properly in Google Chrome, the select box behaves erratically in Firefox. When attempting to choose an option from the select box, such ...

Setting up node.js for angular - serving static files with connect.static and running unit tests

Currently, I am in the process of setting up a basic node webserver by following a tutorial outlined in Pro AngularJS published by Apress. Node.js along with both the connect and karma modules have been successfully installed on my system. During the ins ...

What is the best approach for testing functions in AngularJS using unit tests?

Delving into the world of Angular unit testing and intrigued by the process of testing functions in controllers. I've come across some examples of how to unit test using Karma, but they all seem to define functions like this: $scope.someFunction= fun ...

Tracking and managing user clicks on external links within a vue.js application

I am currently working on a web application that retrieves data from a CMS. Utilizing the Vue-Router in 'history' mode, I need to address content fetched from the API which may include links. My goal is to manage specific links using the router w ...

Is it possible to showcase content when double curly brackets are nested within another set of double curly brackets

Having this specific case, I am interested in showcasing information from the model. Is it possible to utilize double curly braces within another set of double curly braces? ...

I am attempting to utilize a ref in React to access an element within a MUI modal, but I am encountering issues as the reference to the

I've been encountering an issue while trying to access the component did mount lifecycle method, as it's showing null. Interestingly, when I remove the modal, everything works as expected. However, inside the modal, the ref isn't functioning ...

After using browserify, when attempting to call the function in the browser, an Uncaught ReferenceError occurs

I am currently in the process of creating a compact NPM package. Here is a basic prototype: function bar() { return 'bar'; } module.exports = bar; This package is meant to be compatible with web browsers as well. To achieve this, I have inst ...

When I click on .toggle-menu, I want the data-target to have a toggled class and the other divs to expand

Looking to achieve a functionality where clicking on .toggle-menu will toggle a class on the specified data-target element and expand other divs accordingly. jQuery(document).ready(function() { var windowWidth = jQuery(window).width(); if (win ...