Is it time to reconsider using multiple Angular filters in ng-repeat loops?

I am in the process of developing a car rental website using Angular framework.

In the section where users select cars, they should be able to filter based on various attributes like 4x4 capability, automatic or manual transmission, and different categories such as compact, premium, sports cars, etc. Additionally, there is a need for detailed ordering of the cars.

Implementing this was relatively simple with standard filters and a small directive for each filter button. However, I anticipate that the ng-repeat attribute will become quite lengthy with around 12 filters. While not necessarily a problem, it might get cumbersome.

I wanted to consult with you all to see if there might be a more efficient solution available.

This current setup is likely to become unwieldy in the long run.

This is how the code looks currently:

HTML:

<div filter-btn="4x4" ng-model="filters" class="btn">4x4</div>
<div filter-btn="manual" ng-model="filters" class="btn">manual</div>
<input type="text" ng-model="filters.searchCar">
<div class="car-cont">
    <div ng-repeat="car in filteredCars = (cars | filter:filters.4x4 | filter:filters.manual | filter:filters.searchCar)" class="car">{{car.model}}</div>
    <div ng-show="!filteredCars.length">No cars</div>
</div>

JS:

angular.module('mabi').directive('filterBtn',[ function () {
var linkFunction = function(scope, elem, attr){
    var activeFilter = attr.filterBtn;

    var clickFunction = function(){
        scope.$apply(function(){
            if (scope.model[activeFilter] != activeFilter){
                scope.model[activeFilter] = activeFilter;
            } else {
                scope.model[activeFilter] = "";
            }
        });
        console.log(scope.model);
    }

    elem.bind('click', clickFunction);
}

return {
    restrict: "A",
    require: 'ngModel',
    link: linkFunction,
    scope: {
        model: "=ngModel"
    }
}
}]);

Answer №1

Your concern is valid, but my suggestion would be to test it first. Create a trial with a large amount of data and experiment with all the filters on a slow machine using a less efficient browser (cough! IE8... cough!).

If the performance is still not up to par in those scenarios, you may want to consider developing a personalized filter that accepts parameters instead of chaining multiple filters together. While I can't guarantee it will be faster, it could reduce the number of function calls and allow for optimized code customization.

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

tips for generating a random number for direct display

Can anyone help me figure out how to automatically display the total of numbers from this script on my blog post without having to click anything? Additionally, I need it to update if the browser is refreshed. ` http://jsfiddle.net/BenedictLewis/xmPgR/ ...

The Angular JS controller does not function correctly when it is wrapped within $(function () {});

After enclosing the controller code within the $(function () {}); method, it ceased to function properly. Below is an example of the code: Contacts.cshtml @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewpor ...

Performing an XMLHttpRequest in the same JSP file using Javascript

I am working on a JSP file that contains three dropdown boxes labeled "countries", "regions", and "cities". My goal is to populate the regions based on the selected country, and the cities based on the selected region. I have managed to achieve this using ...

Using Jquery to Insert Numbers Inside Brackets to Selection

I am struggling to calculate the sum of numbers in jQuery for selection fields. Currently, my code looks like this. $(function() { $("select").change(function() { updateTotal(); }); updateTotal(); }); function updateTotal() { ...

AngularJS: Utilizing Multiple HTTP Requests with the $http Service

Here is the code snippet I am working with: var updateScreen = function() { $http.get("http://localhost:5000").then(function(response){ $scope.numSerie = response.data.refDetail.Num_Serie; $http.get("data/tempsgammes ...

Change numbers into a comma-separated style containing two decimal points using javascript

I have been working on a JavaScript function to convert numbers into a comma-separated format with two decimal places: Here is my current code snippet: Number(parseFloat(n).toFixed(2)).toLocaleString('en'); The issue with this code is that it ...

When a radiobutton is clicked, a jQuery call to a PHP function triggers an AJAX request which results in a JavaScript function becoming unrefer

Currently, I have a situation where two radio buttons are representing different products. When one of them is clicked, the goal is to update the price displayed on the website based on the selected product. Everything seems to be working fine when using t ...

Changes made in React are not reflected in the DOM

import React, { Component } from "react"; import ReactDOM from "react-dom"; import "./index.css"; class App extends Component { constructor(props) { super(props); this.state = { text: "", listItem: [] } this.onChangeInpu ...

Error code 2769 in Typescript occurs when attempting to transfer process information to the data state in order to utilize it within a modal

I'm encountering an issue while trying to pass a process to a setData state from a .map function in order to display it on a modal. The error message I'm receiving is: "No overload matches this call. Overload 1 of 2, '(props: { compone ...

Create a new storefront JavaScript plugin for Shopware 6 to replace the existing one

I am attempting to replace an existing JavaScript plugin in Shopware 6. However, the code within the plugin file does not seem to execute. Here is my main.js: import MyCookiePermissionPlugin from './plugin/my-cookie-permission/my-cookie-permission.pl ...

Why does a Vue component throw errors prior to being rendered?

In the Home view, there are two components: Filter and Results. The Results component relies heavily on data from the vuex store, which is influenced by the Filter component. Once the filters are applied and the user interacts with Filter, the necessary da ...

There seems to be a problem with the Bootstrap Container and Container Fluid

I am currently utilizing Bootstrap 3.3.7 and have incorporated buttons to enhance the content on my webpage. Upon using container-fluid, I noticed that it does not expand the content across the entire page but rather confines it to the center area of the ...

Populating a clickable list and form with a complex JavaScript object

I have a code snippet that takes an unstructured String and parses it into a JavaScript object. My next step is to integrate it into a web form. You can check out the demo here. The demo displays the structured object hierarchy and showcases an example of ...

Triggering a button click event in a Jquery UI autocomplete combobox

There seems to be a strange issue happening with the jquery ui autocomplete feature when used to create a combobox. Whenever I scroll through the list of results using the scrollbar and then try to close the results by clicking on the combobox button, the ...

Arrows on the button are unresponsive and there seems to be an incorrect number of

I've been working on a project by combining various examples I found online. I'm puzzled as to why it's displaying all the buttons at once instead of just one per page, and why the number of visible buttons decreases by one with each right c ...

Using Node.js for HTML redirections involves creating routes and setting

I am currently attempting to connect my Node.js API with my HTML pages. For the most part, everything is functioning correctly, but I have encountered some confusion along the way. Is there a more efficient method for redirecting an HTML page? Typically, ...

Ways to showcase the refined object array upon clicking a button in Angular

I have been working on a project to create a task management system. The project consists of two main components: itemList and item. The itemList component takes input values for tasks and displays them in the item component. https://i.sstatic.net/SaRNMm. ...

Divergent functionality of regular expressions in Internet Explorer and Chrome when handling white spaces

Here is a function that validates input by checking for numbers and no spaces in between: checkInputValidity: function() { var isValid = true; var idNumber = this.getView().byId("iDNumber"); var regex = /^[0-9]+$/; if (idN ...

Using if statements in $scope.watchCollection causes the removal of array data

I'm facing some confusion with the watchCollection statement. As I monitor changes in my array of objects, I've noticed that using an if statement within the $watch function causes one element to be removed from the array. I find it puzzling that ...

What is the best way to pass an Id to JavaScript's getElementById when the Id from my input tag is produced by the output of PHP/MySQL?

Thank you in advance for your assistance. I am attempting to search through all the IDs listed below to determine if the user has selected any data. <input id=\"".$row['childName']."\" type=\"checkbox\" name=\"foodDa ...