Enhancing Filtering Capabilities with Multiple ng-Model in AngularJS

I am facing an issue with filtering a form based on user input in a text box or selection from a dropdown list. The text box filter works fine individually, but when I try to combine it with the dropdown list selection, neither filter seems to work. Below is the HTML code:

 <div class="row">
            <table class="table" id="results">
                <thead style="background-color:#003A5D; color:white">
                    <tr>
                        <td>Company Name</td>
                        <td>City</td>
                        <td>State</td>
                        <td>Company Type</td>
                        <td>System ID</td>
                        <td>Releast Status</td>
                        <td>Training Tracker</td>
                        <td>SSQ Complete</td>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="contractor in contractors | filter: search ">
                        <td id="companyname">{{contractor.vchCompanyName}}</td>
                        <td id="city">{{contractor.vchOprCity}}</td>
                        <td id="state">{{contractor.vchOprStateID}}</td>
                        <td id="companytype">{{contractor.CompanyType}}</td>
                        <td id="companyid">{{contractor.CompanyID}}</td>
                        <td id="status">{{contractor.ReleaseStatus}}</td>
                        <td>
                            <div ng-switch="contractor.TrainingTracker">
                                <div ng-switch-when="true">
                                    <span style="color:green" ng-bind-html="contractor.TrainingTracker | applyMarks | trustedhtml"></span>
                                </div>
                                <div ng-switch-when="false"><span style="color:red" ng-bind-html="contractor.TrainingTracker | applyMarks | trustedhtml"></span></div>
                            </div>
                        </td>
                        <td>
                            <div ng-switch="contractor.SSQComplete">
                                <div ng-switch-when="true">
                                    <span style="color:green" ng-bind-html="contractor.SSQComplete | applyMarks | trustedhtml"></span>
                                </div>
                                <div ng-switch-when="false"><span style="color:red" ng-bind-html="contractor.SSQComplete | applyMarks | trustedhtml"></span></div>
                            </div>
                        </td>
                    </tr>

                </tbody>

            </table>
        </div>

Here is the Angular Controller filter code:

 $scope.search = function (row) {
        return (angular.lowercase(row.vchCompanyName).indexOf($scope.query || '') !== -1 || angular.lowercase(row.CompanyID).indexOf($scope.query || '') !== -1 || row.CompanyType.indexOf($scope.query2 || '') !== -1);
    };

To address this issue, I attempted to create a custom filter as follows:

app.filter('searchArrayFilter', [function () {
    return function (rows, query, query2) { // your filter take an array, and two query as parameters
        return rows.filter(function (row) {
            return (angular.lowercase(row.vchCompanyName).indexOf(query || '') !== -1 || angular.lowercase(row.CompanyID).indexOf(query || '') !== -1 || row.CompanyType.indexOf(query2 || '') !== -1);
        });
    }
}])

I modified my HTML code to include the custom filter like this:

                  <tr ng-repeat="contractor in contractors |  searchArrayFilter:query:query2 ">

However, now it requires selecting an option from the dropdown and entering text in the textbox simultaneously for the filter to work. This behavior is unexpected as I am using '||' OR operator, but it behaves like '&&' AND.

Any help or guidance on this matter would be highly appreciated!

Answer №1

To resolve the issue, I implemented a customized filter. The initial problem stemmed from a logic error in the filter's code. By replacing '||' with '&&', the filter started functioning correctly. Here is the updated version of the filter:

app.filter('searchArrayFilter', [function () {
    return function (rows, query, query2) { // The filter takes an array and two queries as input parameters
        return rows.filter(function (row) {
            return ((angular.lowercase(row.vchCompanyName).indexOf(query || '') !== -1 || angular.lowercase(row.CompanyID).indexOf(query || '') !== -1) && row.CompanyType.indexOf(query2 || '') !== -1);
        });
    }
}])

I hope this solution proves helpful to others facing a similar challenge. Wishing you a fantastic day ahead!

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

Encountering difficulties in storing array data into MongoDB collection

I am facing an issue with connecting to two different MongoDB instances using different URLs. One URL points to a remote connection string while the other one is for a local MongoDB instance. Initially, I establish a connection to MongoDB using MongoClient ...

Seeking guidance on capturing the correct error message when using JSON stringify?

Imagine I have an object structured as follows var obj = { "name": "arun" age } After attempting JSON.stringify(obj), it results in an error due to the improper structure of the obj. I am interested in capturing this error displayed in the console and pr ...

There are three checkboxes, one of which is independent of the others and requires jQuery to not consider it part of the collection

Initially, I crafted a query that perfectly met the business requirement until there was a change of heart. Uncheck checkbox if checked with jquery The implementation of this code was flawless $('input[type="checkbox"]').on('change' ...

Is it possible to manipulate content using jQuery?

I need help figuring out how to update the content within a span tag when a user clicks on a button. The specific span tag I am trying to target has a class called fa_button_id_counter and an id that corresponds to the post id. Despite my efforts, I haven& ...

When attempting to select an option from the dropdown menu, I encounter an issue where the

My code is not behaving as expected. Whenever I click on the child elements, the dropdown changes to display: none. I am encountering an error when clicking on the input frame and displaying:none. How can I resolve this issue? I would like to be able to ...

Cannot Properly Import JSX Elements in TailwindCSS and React

I am facing an issue with importing a JSX Element into my main file, App.js. The element I want to load is in a separate file called element.js and contains the following code: const element = () => { return ( <div className="text-gr ...

Creating a single Vuetify expansion panel: A step-by-step guide

Is there a way to modify the Vuetify expansion panel so that only one panel can be open at a time? Currently, all panels can be closed which is causing issues. I would like the last opened panel to remain open. I also want to prevent closing the currently ...

Is it possible to convert a string of elements in JavaScript into JSON format?

Within my JavaScript code, a variable holds the following information: url=http://localhost quality=100 tag="4.4, 5.5" I am interested in converting this data to JSON format using JavaScript, like this: "result": { "url": "http://localhost", "qu ...

Exploring the distinction in dependency injection syntax within AngularJS

When it comes to dependency injection, AngularJS offers two distinct syntaxes for support. Syntax 1 myModule.controller('myCtrl', function($scope, $http, myService) { ... ... }); Syntax 2 myModule.controller('myCtrl', [&apos ...

SmartCollection in Meteor generating unpredictable outcomes

When executing News.insert({name: 'Test'}) in the browser JS console, it caused {{count}} to increase from 0 to 1. Checking in mongo console using mrt mongo, db.news.find().count() also returns 1. However, after adding a record through the mongo ...

Designing an interactive 3D space using JavaScript

I'm currently developing an app that allows users to visualize different wallpapers in a 3D room setting. The concept involves placing the user inside a virtual space with four walls, where they can drag and look around, as well as change wallpapers v ...

Guidelines for choosing and uploading a file using Ionic

Is there a way to upload a PDF file to a server using the $cordovaFileTransfer plugin? I currently have an input field like this: <input type="file" onchange="angular.element(this).scope().fileNameChanged(this)"> How can I retrieve the pathForFile ...

Interactive web page with dynamic JQuery functionality and navigable page links

I am working on the project/index.html page <html> <head> <title>Index</title> <scripts...> </head> <body> Hello <div id="content"></div> <button id="page1" value="page1"/> <but ...

Creating a Dojo HTML template involves incorporating repetitive sections of HTML code within the template structure

I am working with a custom Dojo widget that is based on a template and has an HTML template stored in a separate .html file. Here is the Dojo Widget code snippet: define("dojow/SomeWidgetName",[ "dojo/_base/declare", "dijit/_WidgetBase", "dijit/_Templat ...

Limit how API call costs are set in a function by throttling based on an argument

I am currently implementing express-throttle to restrict the number of API calls per IP address each day. I would like to dynamically set the 'cost' parameter in the 'options' array based on a value from the API request (refer to commen ...

Issues with accessing view variables within a directive query are persisting

I am struggling with a specific directive: @Directive({ selector: '[myDirective]' }) export class MyDirective implements AfterViewInit { @ViewChild('wrapper') wrapper; @ViewChild('list') list; ngAfterViewInit() { ...

"Utilize Meteor to transmit emails to internal email addresses within the intran

I have successfully developed a Meteor App to manage requests. However, I am facing an issue where I need emails with default text to be sent whenever a request is created or updated. The challenge lies in the fact that I am operating within an intranet e ...

Positioning the comments box on Facebook platform allows users to

Need assistance, I recently integrated the Facebook comments box into my Arabic website, but I am facing an issue where the position of the box keeps moving to the left. Here is an example of my website: Could someone please suggest a solution to fix the ...

Leveraging props to set the initial value of component data in Vue 3 Composition API

Currently, I am in the process of developing a search page in Vue 3 using the composition API. One of my components is responsible for displaying a snippet of data that includes specific keywords provided by the parent component. To achieve this, I need to ...

Accessing process.env in Nest.js controllers

Is there a way for me to access process.env.SOME_FIELD in nest.js? app.module.ts ... modules: [ ... ConfigModule.forRoot({ envFilePath: '.env.' + process.env.APP_CODE }), CatModule ... ] ... CatController.ts ...