Sorting or filtering with two elements in the option value in AngularJS

I have been struggling for a while now to find a solution to this issue. How can I display the query for the third option value, which should show the filter of DB || CSLB from a json file?

<select ng-model="search.source">
   <option value="DB">DB</option>
   <option value="CSLB">CSLB</option>
   <option value="">DB & CSLB</option>
</select>

The json data looks like this.

"validated": false,
"matches": 10,
"renewed": 2005,
"exMod": 128,
"activeYear": 2003,
"effectiveDate": "10/0/2014",
"address": {
  "zip": 7806,
  "state": "Virgin Islands",
  "city": "Delco",
  "street": "402 Lawton Street"
},
"primaryName": "Lexicondo",
"source": "DB",
"ID": 9,
"_id": "55b5d21cc1389d427f90f9f4"

Any assistance on this matter would be highly appreciated!

Answer №1

If you're looking to solve your problem, the code below might be helpful. I utilized the filter method for arrays to perform the sorting.

For more information on the filter method, you can visit: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

The callback function in the filter method checks if the source matches a value in the select box to group them. If no match is found, the entire array will be displayed (which is why the callback simply returns true).

angular.module('TestApp', [])
  .controller('SearchController', function($scope) {
    this.source = 'All';

    this.items = [{
      name: 'One',
      src: 'DB'
    }, {
      name: 'Two',
      src: 'DB'
    }, {
      name: 'Three',
      src: 'CSLB'
    }, {
      name: 'Four',
      src: 'CSLB'
    }];

    this.applyFilter = function() {
      var _this = this;
      this.filteredItems = this.items.filter(function(value) {
        if (_this.source == 'All') return true;
        return value.src == _this.source;
      });
    };

    this.applyFilter();
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>

<body ng-app="TestApp" ng-controller="SearchController as search">
  <select ng-model="search.source" ng-change="search.applyFilter()">
    <option value="All">DB & CSLB</option>
    <option value="DB">DB</option>
    <option value="CSLB">CSLB</option>
  </select>
  <p>{{search.source}}</p>
  <ul>
    <li ng-repeat="item in search.filteredItems">{{item.name}}</li>
  </ul>
</body>

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

The Controller received a JSON object that was empty

I know this question has been asked countless times, but I've tried all solutions with no success. Here's the JSON object in question: { "manufacture":"HP", "model":"testModel", "serialNumber":"testSerial", "description":"Test Descript ...

NextJS function utilizes its own references within React and automatically fetches data without the need for an import

I recently purchased this template and I'm trying to figure out which page the code snippet "{getLayout(<Component {...pageProps} />)}" is directed to during the initial load. It seems like it might be a global variable hidden somewher ...

A step-by-step guide on implementing lazy loading for a specific div section using either AJAX or Java

I'm facing an issue with a div section that contains numerous tables pulled from my database. The main problem here is that when the page loads, it takes a considerable amount of time to fully load all the content, causing a significant delay. Could ...

What is the best way to execute Jest tests concurrently using the VSCode extension?

Running the Jest tests in band is essential to prevent errors from occurring. However, I am unsure of how to resolve this issue. The tests run smoothly when executed through the command line. ...

Only the initial value is being processed in the for loop using Javascript

I am having issues trying to sum the values within a for loop sourced from my Django database using JavaScript. Currently, when I iterate through the loop, only the first value in the array is returned. I need assistance in finding a solution that allows ...

html interactive/expandable tree

I've come across this code which generates an HTML tree, but I'm facing an issue where the tree expands every time I refresh the page. What I want to achieve is to have certain branches expanded and others collapsed when the page is opened, depe ...

Best practices for organizing an array of objects in JavaScript

I have an array of objects with nested arrays inside, and I need to restructure it according to my API requirements. [{ containerId: 'c12', containerNumber: '4321dkjkfdj', goods: [{ w ...

What is the best way to extract multiple values from a JavaScript variable and transfer them to Node.js?

Script JavaScript script snippet embedded at the bottom of an HTML file: var savedValues = [] var currentId = document.getElementById("fridgeFreezer").value function handleChange() { // Logic to handle user input changes: var temp = document.ge ...

Arranging Vue numbers

Vue data contains: data: { offices: requestData, selectedFloors: [ "3", "4", "5", "10", "11", "12", ...

What is the best way to include multiple views in a main HTML template in Angular?

Is there a way to load two HTML files into a base HTML file? Essentially, I have a base HTML file with a header and content view, and I want to load different HTML files into each of them. Here is the base HTML file structure: <div class="container"> ...

The HTML iframe is displaying blank content

I'm trying to embed a webpage within another webpage using an iframe. I attempted to do so with this simple code: <iframe src="http://edition.cnn.com/" id="i_frame"></iframe> JSFIDDLE However, nothing is displaying. Any thoughts on why ...

The variable "vue" is not properly defined within the instance, yet it is being called

I'm currently working on a Vue app and encountering an issue. The onScroll function is working correctly, but when I click the button component to trigger the sayHello function, I receive an error message. The error states: "Property or method &apo ...

Struggling with this CSS is really getting to me

I am struggling to create a modal with the tools and close button appearing on the same line. It should look similar to the image below: var modal = document.getElementById('myModal'); var btn = document.getElementById("myBtn"); var span = doc ...

Unlocking the power of Angular Bootstrap UI in your controller

I have been working with the angularJS bootstrap tabset and have encountered a requirement to add a next button for customers to switch tabs. Below is an example of my code: <div ng-controller='ctr'> <tabset> <tab active=&apo ...

Setting minimum or maximum dates in jquery-simple-datetimepicker is proving to be challenging

Currently, I am utilizing the following library: In this jsfiddle example: http://jsfiddle.net/3SNEq/2/, everything works perfectly when setting minDate or MaxDate directly in the appendDtpicker method. However, when trying to use handleDtpicker like thi ...

Learn how to successfully upload an image using React and Node without having to rely on

I am currently exploring file uploading in react/node. Instead of passing files directly into the API request, I am passing them within the body of the request. Here is a snippet of my react code: import React, { Component } from 'react'; import ...

Altering the inner HTML content of a div using the ID within an Angular component function

Attempting to change the inner HTML content of a div using its id with another div in an Angular component method. Successfully calling the populateEndpointHomeContent() method and retrieving the content, but encountering issues with subsequent content. Th ...

What is the method for applying the action (hide) to every table cell that doesn't include a specific string in its ID?

I have a table with cells containing unique IDs such as "2012-01-01_841241" that include a date and a number. My goal is to filter the table to only display three specific numbers by sending a request and receiving those numbers. Is there a more efficien ...

Position an HTML canvas at the very top of the webpage

Can someone help me figure out how to align a canvas element to the very top (0, 0) of a webpage? I attempted using margin: 0; padding: 0px;, but it didn't work. There always seems to be some white space at the top that I can't eliminate. ...

Can an image be uploaded using solely jQuery without relying on PHP?

I have been attempting to upload an image using Ajax/jquery without refreshing the page and also without relying on php in the back-end. After extensive searching, all solutions I found involved php on the server side, but my requirement is to avoid using ...