Screening for items that meet specific criteria

Currently, the functions are functioning properly by filtering inventory based on barcode and manufacturer. However, I am looking to enhance it to behave like default angularjs filtering. Specifically, I want it so that if I select manufacturer - LG and barcode - 112, and they do not match, nothing should be displayed.

At the moment, when I click on the filter function, it filters by barcode, and when I use the filter2 function, it filters by manufacturer separately.

$scope.filter = function(barcode) {
            var filtered = [];
            for(var i = 0; i < $scope.inventories.length; i++){
                if($scope.inventories[i].barcode == barcode){
                    filtered.push($scope.inventories[i]);
                } 
            }
            $scope.filtered_inventories = filtered;
        };

        $scope.filter2 = function(manufacturer) {
            var filtered = [];
            for(var i = 0; i < $scope.inventories.length; i++){
                if($scope.inventories[i].manufacturer == manufacturer){
                    filtered.push($scope.inventories[i]);
                } 
            }
            $scope.filtered_inventories = filtered;
        };

Answer â„–1

Give this a try


    $scope.filter = function (barcode, manufacturer) {
        var filterItems = [];
        for(var x = 0; x < $scope.inventories.length; x++){
            if($scope.inventories[x].barcode == barcode || $scope.inventories[x].manufacturer == manufacturer){
                filterItems.push($scope.inventories[x]);
            } 
        }
        $scope.filtered_items = filterItems;
    };

Answer â„–2

To enhance your filtering process, consider implementing a more versatile approach:

$scope.filterByBarcode = function(list, key, value) {
    return list.filter(function(item){
        return item[key] === value;
    });
};

When conducting the filter, you can utilize the following syntax:

$scope.filtered_items = $scope.filter($scope.filter($scope.items, 'barcode', 112), 'manufacturer', 'LG')

To streamline this operation, you may opt to create a dedicated function:

$scope.filterByBarcodeAndManufacturer = function(barcode, manufacturer) {
    $scope.filtered_items = 
        $scope.filter($scope.filter($scope.items, 'barcode', barcode), 'manufacturer', manufacturer);  
}

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

Issue encountered while attempting to parse JSON data

I've been dealing with JSON data in Django view and parsing it in JavaScript to extract the necessary information. This is a snippet from my view.py file (Django) ibms = [] for i in range(2, 5): ibm = Mapa(i, wsMapa) ibms.append(ibm.__dict__) ...

Incomplete data was retrieved from the localStorage

I am currently in the process of developing a mobile application using Phonegap version 1.4.1. I have encountered an issue on iOS (running on version 5.1) where the app fails to load all data from localStorage. Upon first use of the app, I set a flag in l ...

Vue.js routing and mixin dilemma

Calling all Vue developers! I am currently working on a vuebnb tutorial and running into an issue with routing and mixins. I have attempted to assign data before entering the router using the beforeRouteEnter guard, but it seems like my template is being r ...

What is the best way to obtain a user's ID on the server side?

I'm currently working on a node.js application using express and I am in need of retrieving the user ID. I would like to have something similar to "req.userID" so that I can use it in the following way: var counter=0; var user = new Array(); router.g ...

Tips for reconstructing a path in Raphael JS

I am encountering a unique issue with my implementation of Raphael JS. Currently, I am working on drawing a donut element and seeking advice similar to the content found in this helpful link How to achieve 'donut holes' with paths in Raphael) var ...

What is the best approach to retrieve a username from a SQL database using AJAX within a Flask application

I have been attempting to retrieve the username column information from SQL using ajax, but only the username of the first ID is being returned. However, I need all usernames to be fetched. Below is the model: class users(db.Model): id=db.Column(db.I ...

Is there a way to create a popover menu in VueJS without using JQuery

I found exactly what I need in this helpful answer. It demonstrates a menu appearing when clicking on a table row. The dilemma is that it relies on JQuery... Therefore, I am curious if achieving the same functionality without JQuery is possible. Maybe thr ...

Finding the Biggest Number in an Array

I've come up with a solution using Math.max(), but I'm curious why the following approach isn't working. array = [4, 5, 1, 3] for (var i = 0; i < array.length; i++) { var num = 0; if (array[i] > num) { num = array[i]; } } ...

Flickering issue with Chart.js chart persists upon reopening the page

Utilizing mustache.js, I am injecting a view file into a designated <div>. Within this view, I have incorporated a canvas element situated inside a specific div, showcasing a chart created with Chart.js. <div> <canvas id="gesundheitsve ...

Filtering an array within an array based on user input

I am currently facing a challenge in filtering the child elements of an array. I am puzzled on how to specifically target children elements. So far, my filter is only functioning at the top level. Array: options: [ {name: 'Ð’Ñ‹Ñ…Ð ...

Generate a compressed folder containing a collection of PNG images

While attempting to generate a zip file using JSZip, I encounter issues where the images are falsely flagged as not being instances of Blob or the final result turns out to be corrupt and cannot be extracted. The process involves an API on the back-end th ...

Default Angular2 route component for the RC5 program

My PHP website already in place, and I started integrating Angular2 components into it. I've implemented a router script to handle loading different components based on the URL. However, upon navigating away from a component page, I encounter the fol ...

Error encountered in Three.js: ShaderPass.js is unable to read the property 'prototype' of undefined

I'm currently facing an issue while trying to implement a basic GlitchPass in a Three.js demo. I keep encountering the error message Uncaught TypeError: Cannot read property 'prototype' of undefined at ShaderPass.js. For this particular dem ...

Utilizing various colors for tooltipFontColor in Chart.js

I'm trying to customize the font color for tooltip labels in Chart.js, but I want to set different colors based on certain conditions. Specifically, I want the label color to be white by default, but change to red if a condition is met. I've look ...

Running javascript within a python environment commonly leads to the occurrence of a KeyError

Struggling to implement a Python Selenium script? I need to verify if a specific element exists within a designated parent and return true if it does. Check out the code snippet below: for box in range(len(browser.find_elements(*selector))): res ...

Tips for assigning a personalized value to an MUI Switch when it is in the off position

I am currently utilizing the MUI Switch component to create an On-Off button. I have manually set the value as "on" and it is functioning correctly when the switch is in the true state. However, there doesn't seem to be an option to change the default ...

Integrate a Facebook Like-box within a customized jQuery modal window

I've been working on inserting the Facebook like-box code into my page and trying to display it within a jQuery modal dialog. Here's the code I'm using: <script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>< ...

Move the internal array pointer forward to the next record in order to apply the insertAfter function within the jquery code

As a new jQuery user, I'm attempting to develop a jQuery function using data provided by PHP. My goal is to arrange DIV elements in order of their values using insertAfter method. However, I seem to be encountering some difficulty in advancing the poi ...

Providing hosting for static files that allows for html5mode with necessary URL rewriting

Is there a way to host my static files with html5mode support without setting up a server? It seems like html5mode requires url rewriting for the virtual URLs generated by Angular. Are there any CDNs that offer this kind of support? Could Firebase Hostin ...

Parse a string and generate an array using regular expressions in JavaScript/Node.js

I'm currently working on coding in JavaScript to extract an array of elements after splitting using a regular expression. var data = "ABCXYZ88"; var regexp = "([A-Z]{3})([A-Z]{3}d{2})"; console.log(data.split(regexp)); The current output is [ &a ...