Switch between selected boxes in AngularJS

I am currently working on a feature where all boxes should be automatically checked when the user deselects them both.

Just to clarify, there are two items in cards as shown in the image. These cards are initially selected. When the user deselects the first one, the other card will be checked. Likewise, when the user deselects the second one, both of them should automatically be selected again.

This is what I have implemented so far:

HTML

<ion-list ng-show="transactionsCtrl.showCardBox">

<ion-item class="bg-blue p-0">

    <div class="dotted-1"></div>

    <div ng-repeat="singleCard in transactionsCtrl.cards">

        <div class="row p-tb0 m-t5" ng-click="transactionsCtrl.ToggleCardFilter(singleCard.g_id)">

            <div class="col col-20">
                <div ng-class="{'image-size-1-50 white-checked-image': singleCard.selected , 'image-size-1-50 white-unchecked-image': !singleCard.selected}"></div>
            </div>

            <div class="col col-80">
                <div class="sub-title-white oslight">{{(singleCard.cardNumberMasked).slice(-5)}} <span class="m-l10 right {{transactionsCtrl.PlaceCardImageClassForFilter(singleCard.g_productSubCategory)}}">&nbsp;</span></div>
            </div>

JS

 self.ToggleCardBox = function() {
     closeAll();
     self.showCardBox = !self.showCardBox;
 };

 self.ToggleCardFilter = function(cardId) {
     // toggle on-screen indicator
     for (var c = 0; c < self.cards.length; c++)
         if (self.cards[c].g_id == cardId)
             self.cards[c].selected = !self.cards[c].selected;

     var idx = $scope.transactionFilter.cards.indexOf(cardId);
     if (idx == -1)
         $scope.transactionFilter.cards.push(cardId);
     else
         $scope.transactionFilter.cards.splice(idx, 1);

     self.applyFilterChange();
 };

Answer №1

Keep it straightforward. Begin by adding the current checked count variable.

// Do not accept only the card id, but the full card
 self.ToggleCardFilter = function(card) {
     // toggle on-screen indicator
     //for (var c = 0; c < self.cards.length; c++)
         //if (self.cards[c].g_id == cardId)
             //self.cards[c].selected = !self.cards[c].selected;
    if($scope.transactionFilter.cards.length){
       card.selected = !card.selected;
     }else{
       for(var i = 0 ; i < self.cards.length ; i++){
          self.cards[i].selected = true;
        }
      }

     var idx = $scope.transactionFilter.cards.indexOf(card.g_id);
     if (idx == -1)
         $scope.transactionFilter.cards.push(cardId.g_id);
     else
         $scope.transactionFilter.cards.splice(idx, 1);

     self.applyFilterChange();
 };

HTML

<ion-list ng-show="transactionsCtrl.showCardBox">

<ion-item class="bg-blue p-0">

    <div class="dotted-1"></div>

    <div ng-repeat="singleCard in transactionsCtrl.cards">

        <div class="row p-tb0 m-t5" ng-click="transactionsCtrl.ToggleCardFilter(singleCard)">

            <div class="col col-20">
                <div ng-class="{'image-size-1-50 white-checked-image': singleCard.selected , 'image-size-1-50 white-unchecked-image': !singleCard.selected}"></div>
            </div>

            <div class="col col-80">
                <div class="sub-title-white oslight">{{(singleCard.cardNumberMasked).slice(-5)}} <span class="m-l10 right {{transactionsCtrl.PlaceCardImageClassForFilter(singleCard.g_productSubCategory)}}">&nbsp;</span></div>
            </div>

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

What is the best way to transfer the value of a slider from jQuery or JavaScript to a Python Flask application

Trying to implement a round slider that displays its value on the client-side webpage. Whenever the user adjusts the slider, the updated value needs to be sent to the server using Python Flask as the backend. I attempted to achieve this using jQuery and Aj ...

Clicking on the toggle button will reveal a different ID each time, extracted from JSON links

I have a JSON file containing information about movies currently playing in theaters. I want to display specific details for each movie when its title is clicked, but my code isn't functioning properly. Currently, all the movies' information is d ...

Harnessing the power of Angular JS, Jquery, and Metismenu simultaneously

Is it possible to successfully integrate Jquery, AngularJs, and metisMenu simultaneously? Initially, I added Jquery first, followed by AngularJs and then metisMenu. Unfortunately, metisMenu did not function properly. Subsequently, I rearranged the order, ...

The addition of a slash between the hashtag and the anchor name by Fullpage.js is causing conflicts with ng-include

My experience with using fullpage.js on a simple site alongside angular directives has been met with an issue. When including a .phtml file, the anchor functionality of fullpage.js stops working as it adds a slash before and after the anchor name. UPDATE ...

Transform a string into a boolean value for a checkbox

When using v-model to show checked or unchecked checkboxes, the following code is being utilized: <template v-for="(item, index) in myFields"> <v-checkbox v-model="myArray[item.code]" :label="item.name" ...

Tips for adding a CSS class to an HTML element on-the-fly

As a newcomer to JavaScript and AngularJS, my question may be considered naive by some. I am currently working on a tutorial project involving AngularJS. Here is the HTML code snippet I am dealing with: <link href="http://netdna.bootstrapcdn.com/boo ...

What could be the reason for my dropdown not submitting the form?

Check out my code below: <div class="center"> <p> Choose an item: </p> <div class="dropdown"> @using (Html.BeginForm("Start", "Home", FormMethod.Post, new { name = "form", id = "form" })) { @Html ...

What is the best way to customize the border color of a disabled Material UI TextField?

I'm struggling to override the default style for disabled Mui TextField components, particularly in changing the border color. Although I successfully altered the label color for disabled fields, I can't seem to get the border color to change. T ...

Refresh the Document Object Model (DOM) and transmit the present time

I am having an issue with sending the actual current time when a button is clicked. Instead of getting the current time, I am receiving the time when the page initially loaded. This button is used to submit a form on Google Sheets using an API. This is th ...

Is there a way to determine when a Telegram web app specifically designed for bots has been shut down?

I have created a Telegram bot that opens a web app, whether through an inline button, keyboard, or link. Once the user completes the tasks in the web app, they can close it using the "close" button instead of any button within the web app that I can track. ...

Encountering this issue: Unable to access the property 'length' of an undefined variable

I'm currently developing an app using nuxt, vuetify 2, and typescript. Within the app, I have radio buttons (referred to as b1 and b2) and text fields (referred to as t1, t2, t3). When a user clicks on b1, it displays t1 and t3. On the other hand, w ...

Using a for loop in JavaScript to dynamically display TextBoxFor(model=> model[i].prop) in an MVC application

I need some help getting this code to function correctly: $('#ddl').change(function () { $('#cont').html(''); var count = getCount($('#ddl').val()) for (var i = 0; i < count ; ...

The latest version of Bootstrap 5 [beta] has removed jQuery, causing issues with the hamburger menu functionality not opening or collapsing properly

Recently, I decided to experiment with the latest Bootstrap version 5 beta after successfully implementing a hamburger menu in v4.6. However, upon testing v5, I encountered two immediate changes: The removal of jQuery Replacing ml-auto with ms-auto Despi ...

Press the button that says "start" using Javascript

Is it possible to modify the button click in this way? $('input[name="submit.one-click-premium-de.x"]').click().first(); Can I use a wildcard for the button name instead of specifying all words? $('input[name="submit.one-click*"]').c ...

The function yields a resolved promise rather than returning data

I'm trying to use this function: const fetchAndMapData = (): Promise<Data> => { const data = fetch('https://jsonplaceholder.typicode.com/posts') .then((response) => response.json()) .then((items) => items.map((item: ...

Tips for transferring data between pages in VUE js using paths

I currently have two pages - an add page and an edit page. I am looking to transfer data from the edit page to the add page. When the save button is clicked in the edit page, it should redirect the user back to the add page with a URL of /test/admin/testin ...

The title of the homepage is showing up behind the AppBar. Any suggestions on how to correct this?

I encountered an issue with my appBar where the homepage was appearing behind it instead of below it. Here is a snapshot of the problem: https://i.stack.imgur.com/27LjB.png Below are the codes for the AppBar: const Header = () => { const [value, setV ...

Designing personalized buttons on React Google Maps

Currently tackling a personal project that involves react-google-maps, but I'm struggling to display custom buttons on the map. My goal is to create a menu button that hovers over the map, similar to the search bar in the top left corner of Google Map ...

NodeJS module loading issue

Currently, I am attempting to utilize the resemblejs library () in order to compare two images. Despite creating a directory and adding resemblejs as a dependency, when running nodejs test.js, an error occurs: var api = resemble(fileData).onComplete(funct ...

Manipulate data within multidimensional array by adding or requesting values

I have created a multidimensional array in the following way: var languageArray = {}; $('#language li a img').each(function(index) { var helperArray = []; helperArray.push(langi); helperArray.push(getnumber); languageArray.push(h ...