toggle visibility of <li> elements using ng-repeat and conditional rendering

I have an array of color IDs and codes that I'm utilizing with ng-repeat in the <li> tag to showcase all colors. However, I only want to display colors where the color code is less than 10, hiding any colors where the color code exceeds 10. Additionally, there is a '+' button that, when clicked, will reveal the hidden colors and switch its icon to '-'. Clicking the button again will hide the colors with a Color Code greater than 10. Here's the code snippet:

<div class="row py-3 border_bootom_1">
    <div class="col-lg-2 align-self-center">
        <h4 class="card-title"> Color </h4>
    </div>
    <div class="col-lg-10 align-self-center">
        <ul class="ss_size_general ss_size_general_1">
            <li class="pointer" ng-repeat="color in $ctrl.parameters.colors">
                <a href="#" 
                   ng-class="{'active': $ctrl.search.colors.indexOf(color.code.toString()) >= 0, 'colorOpen-content': color.code > 10}"                                  
                   ng-click="$ctrl.setParameter('colors', color.code.toString())" title="{{color.name}}">
                    <div class="verticle_center"><span>{{color.label}}</span></div>
                </a>
            </li>
            <li>
                <a><div class="verticle_center pointer colorbtn"><i class="fa fa-plus"></i></div></a>
            </li>
        </ul>
    </div>
</div>


$('.colorbtn').click(function () {
    $('.colorOpen-content').toggle(200);

    var child = $(this).children();
    if (child.hasClass('fa fa-plus'))
        child.removeClass('fa fa-plus').addClass('fa fa-minus');
    else
        child.removeClass('fa fa-minus').addClass('fa fa-plus');

    return false;
});`

Answer №1

Give this a shot

`

<div ng-class="{'active': $ctrl.search.colors.indexOf(color.code.toString()) >= 0}"
ng-style="{{color.code}} >= 10 ? {'display': 'hide !important'} : {'display': 'block !important'}"></div>

`

Answer №2

If you're looking to filter data in AngularJS, you can utilize the filter function. Here is an example of how you can achieve this:

var app = angular.module("app", []);

app.controller("MainCtrl", function($scope) {
  $scope.lt10 = true;
  $scope.colors = [{
      code: 1,
      name: 'red',
      label: 'Red'
    },
    {
      code: 2,
      name: 'blue',
      label: 'Blue'
    },
    {
      code: 30,
      name: 'green',
      label: 'Green'
    },
    {
      code: 40,
      name: 'yellow',
      label: 'Yellow'
    }
  ];


  $scope.toggleShow = function() {
    $scope.lt10 = !$scope.lt10;
  };

  $scope.setParameter = function() {

  };

  $scope.myFilter = function(itm) {
    if ($scope.lt10 && itm.code < 10) return true;
    if (!$scope.lt10 && itm.code >= 10) return true;
    return false;
  };

});
<html ng-app="app">

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
</head>

<body ng-controller="MainCtrl">
  <div class="row py-3 border_bootom_1">
    <div class="col-lg-2 align-self-center">
      <h4 class="card-title"> Color </h4>
    </div>
    <div class="col-lg-10 align-self-center">
      <ul class="ss_size_general ss_size_general_1">
        <li class="pointer" ng-repeat="color in colors|filter: myFilter">
          <a href="#" ng-class="{'active': $ctrl.search.colors.indexOf(color.code.toString()) >= 0, 'colorOpen-content': color.code > 10}" ng-click="setParameter('colors', color.code.toString())" title="{{color.name}}">
            <div class="verticle_center"><span>{{color.label}}</span></div>
          </a>
        </li>
        <li>
          <a>
            <div ng-click="toggleShow()" ng-show="lt10" class="verticle_center pointer colorbtn">+ PLUS +</div>
            <div ng-click="toggleShow()" ng-show="!lt10" class="verticle_center pointer colorbtn">- MINUS -</div>
          </a>
        </li>
      </ul>
    </div>
  </div>

</body>

</html>

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

Using CSS Clip Path to conceal elements

My current design uses clip-path for a gradient effect at the top of the page. Ideally, I want the two angles to meet seamlessly without any visual issues. I initially positioned the top so that they touch perfectly and nothing interferes with the angled ...

Issue with child div exceeding 100% height within CSS Flexbox causing inconsistency in Firefox and Chrome browsers

I am facing a challenge trying to make a div resize to match its parent's height using flexbox. This issue seems to occur only in Chrome (48), as it displays correctly in Firefox (44). You can view the example on Fiddle: https://jsfiddle.net/jvilla/k ...

JavaScript opacity adjustments not achieving desired outcome

My attempt to make this sub menu fade in and out upon button press is not working as expected. It should fully fade in shortly after clicking 'help' and completely fade out when clicking 'back'. Is the problem with the method I'm u ...

Tips for fading out two elements after completing a drag and drop action

Visit this Codepen for more examples - Codepen I have been developing a drag and drop feature in my application. Once the item is dropped, it transitions from red to green and fades out smoothly. The droppable element behind the draggable should also fad ...

Having trouble getting a div to slide to the left on hover and collapse on mouseout using Jquery?

Hey there! I need some assistance in completing this code snippet. I have a div with overflow:hidden and a margin-left of 82px. Inside it, there is a share link and a ul list containing three external links. When I hover over the share link, everything w ...

AngularJS - Not binding $scope to the DOM

Recently starting out with Angular, I decided to practice by creating a simple website. One of the features I want to include is displaying the number of times a button has been clicked through data binding. Here's the controller code I've writte ...

Enhance Your Website with GatsbyJS Custom Scrollbars

I'm struggling to customize the default scrollbar on my Gatsby website with an overlay scrollbar. I want to avoid the page 'shifting' slightly to the left when switching between pages that have scrollbars and those that do not. My attempt i ...

Initialization of Angular provider $get is missing

Within my 'app.js' file, I have the following code that is used in my config to set up $navigationProvider.doSomething(). When running this code, Test1 and Test3 are alerted correctly, but I'm having trouble with getting my this.$get method ...

Using jQuery functions like closest(), find(), and children(), it is possible to target and retrieve the sibling of a specific parent

Having trouble determining the URL of a using .closest, .find and potentially other jQuery methods. The webpage structure is as follows: ul ul ul.good li ... li li <a href="/findme">findme</a> ul . ...

How to apply custom styling to a specific element within an Angular Material 2 component using CSS based on its Id or

My EntryComponent features a material button menu where I attempted to customize the default style using ::ng-deep. However, the changes affected all button components in the parent Component as well. <style> ::ng-deep .mat-button{ max-width: 50 ...

Moving pictures using css3 transitions

Trying to create a straightforward image slider. Below is the provided JavaScript code: $(document).ready(function() { $('#slide1_images').on('click', 'img', function(){ $("#slide1_images").css("transform","translateX ...

Is there a way to bypass cells within an html table?

Creating an online board game and looking to have the cells go around the board. Wondering if anyone knows a way to skip the cells in the middle? Attempted using   but it doesn't seem to be effective. Here is a snippet of the code: #board { ...

Angular App successfully submits authentication calls to MobileFirst Adapter multiple times

Setup: MFPF v7.0 Eclipse: Luna SR.2 (4.4.2) Windows 7 An unusual problem has arisen for me. I am currently utilizing adapter based authentication in a specific Angular project. The application successfully authenticates, but it keeps repetitively calli ...

What is the best way to determine font size based on the width of the

In order to ensure that certain elements on my page do not exceed specific widths, it is crucial for me to use a monospaced font. This will limit the horizontal "stride" between characters to stay within a specified threshold. However, simply setting the ...

Renew Firebase Token

Currently, my email and password authentication flow in web Firebase JavaScript involves generating a token that I then verify on my node.js backend using firebase-admin. To make things easier, I store this generated token in the browser's local/sessi ...

What are the steps to restrict the scope of a <style> declaration in HTML?

Currently, I am in the process of creating a user interface for a webmail. Whenever I receive an email, I extract its content and place it within a <div> element for display purposes. However, one challenge that I am facing is that each email contain ...

Learn how to dynamically change a class name with JavaScript to alter the color of a navbar icon

I have little experience with javascript, but I want to make a change to my navbar icon. Currently, my navbar has a black background with a white navbar icon. As I scroll the page, the navbar background changes to white and the font color changes to black. ...

Protractor unable to locate elements using by.repeater

What is the best method for targeting this repeater with Protractor? <a ng-repeat="item in filteredItems = (items | filter:'abc')">{{item}}</a> ...

ways to change the font color style of a header element using css

Below is the HTML code that I need to work with: <h5 class="post-title">Welcome To The Site</h5> I am attempting to change the font color to red using this CSS: .post-title{ color:red ! important; } However, my attempt was unsuccessful ...

Tips for Keeping Footer Aligned with Content:

When checking out this website, you may notice that the footer appears to be affixed to the left side. The CSS code responsible for this is as follows: #footer { width: 800px; clear:both; float:right; position:relative; left:-50%;} I would appreciate it ...