Using AngularJS forEach to assign properties to objects within a found set in ng-repeat

I am using an ng-repeat to display items from a JSON file, and they can be filtered based on user input.

<tr ng-repeat="i in filteredItems = (iso3166 | filter: {alpha_2: isoQuery})">

Everything is working as expected. Each item in the "iso3166" group has boolean values for "restricted", "unrestricted", and "unclassified", corresponding to the columns "license", "prohibited", and "size".

Users can set these values to true or false in each row:

https://i.stack.imgur.com/m0ck4.jpg

This functionality is working fine.

Now, I want to add an "ALL" row at the top that will apply the selected value to every item in the filtered set when clicked:

https://i.stack.imgur.com/0N6an.jpg

In simple terms, if a user clicks the "restricted" button in the "license" column of the "ALL" row, all rows should toggle their "restricted" license value. However, I am unsure about how to implement this logic in the code.

The current ng-click function is:

ng-click="setAll('col1','licensed')"

The corresponding function looks like this:

$scope.setAll = function (column, value) {
    angular.forEach($scope.filteredItems, function (value, key) {
      ??
    });
};

I need help with assigning the correct value to each row within the loop.

Answer №1

Currently, the inner function is hiding the value variable. To resolve this, modify the name of the local variable.

$scope.setAll = function (column, customValue) {
  angular.forEach($scope.filteredItems, function (item, key) {
    item[column] = customValue;
  });
};

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

Positioning elements next to each other in jQuery on mouse over - while also addressing scrolling issues in a div

After tinkering with this interesting concept of mouseover combined with absolute positioning divs on a jsFiddle, I encountered some unexpected results. The code was inspired by a stackoverflow thread on positioning one element relative to another using j ...

In what way can the result of the code displayed be considered as truthful?

this.someService.findDevices() .subscribe((segments) => { this.segments = Array.from(segments.segments); this.packs.forEach((pack) => { pack.segments = Array.from(segments.segments); pack. ...

Struggling to accurately convert the string into a date object

I have an array of objects structured like this: const days = [ { _id: 12312323, date : '30/12/2021', dateStatus : 'presence' }, ... ] I am looking to convert the date property from a string to a Date object using the follo ...

Executing Statements in a Specific Order with Express and Sqlite3

I am having an issue creating a table and inserting an item into it using the node command. Despite my efforts to reorganize my script, the item is being inserted before the table is created. Interestingly, manually inputting the commands in sqlite3 works ...

Roundabout Navigation Styles with CSS and jQuery

My goal is to implement a corner circle menu, similar to the one shown in the image below: https://i.stack.imgur.com/zma5U.png This is what I have accomplished so far: $(".menu").click(function(){ $(".menu-items").fadeToggle(); }); html,body{ colo ...

Having trouble changing my array state in react?

I'm having trouble understanding why my React state isn't updating with the following code: state = { popUpMessages:[] } popUpMessage(id,name) { console.log("id ",id,"name ",name) const addUserObject = { id, name }; const new ...

Passing the v-model property from a child component to a parent in VueJS

Consider a scenario where there is a Vue component structured like the following: Vue.component('child-comp',{ props:['name', 'val'], data: function(){ return { picked: '' } }, ...

Show/Hide a row in a table with a text input based on the selected dropdown choice using Javascript

Can someone please assist me with this issue? When I choose Business/Corporate from the dropdown menu, the table row becomes visible as expected. However, when I switch back to Residential/Consumer, the row does not hide. My goal is to only display the row ...

Tips for delaying the execution of numerous ajax success callbacks?

In my JavaScript code, I am facing the following situation: call_A(success_A) call_B(success_B) function call_A(success){ // make ajax request success(result) } function call_B(success){ //make ajax request success(result) } function success_A(){ ...

The error message "TypeError: Trying to access properties of an undefined object (reading '800')" is being displayed

Every time I launch my application, I encounter the error message: "TypeError: Cannot read properties of undefined (reading '800')". import React, { useState } from 'react'; import { Menu, MenuItem, Avatar, Box, ThemeProvider} ...

Issue with Material-UI NativeSelect not correctly displaying preselected option

I have the following code snippet: <NativeSelect classes={{ icon: classes.icon }} className={classes.select} onChange={this.onVersionChange} > { Object.keys(interface_versions).map(key => { return <optio ...

Issue when trying to use both the name and value attributes in an input field simultaneously

When the attribute "name" is omitted, the value specified in "value" displays correctly. However, when I include the required "name" attribute to work with [(ngModel)], the "value" attribute stops functioning. Without using the "name" attribute, an error ...

Create a dynamic onClick event script and integrate it into Google Optimize

I need to incorporate a button element into my website using Google Optimize for an experiment. This button needs to trigger a specific script depending on the variation of the experiment. I have attempted two different methods: <button id="my-button" ...

I'm curious about the type I can set for the first parameter of setState in TypeScript. Is there a way to pass a dynamically generated state object to setState?

When trying to pass a newState object to setState and add some additional properties under certain conditions, I encountered a type error: I attempted to define the new State as Pick<ItemListState, keyof ItemListState> but received a type error ...

Having trouble with getting the second JavaScript function to function properly?

I am facing an issue with the second JavaScript function. When I click the 'Send Mail' button, it should call the second function and pass it two values. However, the href line (second last line in the first function) is not rendering correctly. ...

When adding files through drag and drop, the FormData is including a blank file field in the sent

I am currently working on a photo upload page that has drag and drop functionality enabled. Below is the form code: <form id="Upload" method="post" action="sessionapi/UserPicture/Upload" enctype="multipart/form-data"> <input class="box__file ...

The router component in "react-router-dom" is not functioning properly

My goal is to explicitly utilize history in my project. While I am familiar with BrowserRouter, I prefer to use Route and make history one of its properties. Upon running the program, I encounter this page: enter image description here Below is my AppRout ...

JavaScript - Not a Number

I am currently facing an issue while attempting to calculate the Markup of a product. I keep receiving a 'NaN' error in my console, which I understand stands for Not a Number. However, I am struggling to identify and rectify the root cause of thi ...

npm sinopia installation failed - there was an error during the installation

When attempting to globally install sinopia for electron updater, I encountered the following error: npm install -g sinopia npm WARN deprecated <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f69b9f989f9b9782959eb6c7d8c6d8c ...

A guide on implementing an asynchronous timeout error handler in feathers

Is there a way to implement an async-function timeout error handler as a hook in Feathers that is located in the service file to manage promises within hooks? This post was created at the suggestion of @Bergi on my previous question If you are interest ...