Personalized pop-up experience with AngularJS

I am currently working on creating a filter in AngularJS without relying on jQuery. To achieve this, I am using a custom directive to generate a popup that includes a checkbox. However, I have encountered a couple of issues.

1. I have created two popups using the ng-repeat directive and passing arguments to the directive. When I pass a simple string as shown below, everything works fine:

      <input checkbox-all="0-isSelected-id1"  class="clsInput" />
      app.directive('checkboxAll', function () {
      return function(scope, iElement, iAttrs) {

         // We can split this string 
         var parts = iAttrs.checkboxAll.split('-');
        });

However, when I try to pass the index like this:

      <input checkbox-all="{{$index}}-isSelected-id{{$index}}"  class="clsInput" />
      app.directive('checkboxAll', function () {
      return function(scope, iElement, iAttrs) {

         // It will return undefined
         var parts = iAttrs.checkboxAll.split('-');
        });

I need to pass the index to the directive since all of this code is within an ng-repeat loop.

2. These two popups have the same class but different IDs. I want to display these popups when clicking on two separate div elements. I would like to achieve this without using jQuery and apply two styles to position the popup (left and top) every time it is clicked.

Requirements:

https://i.sstatic.net/K61yA.png

https://i.sstatic.net/lpNF6.png

Below is the code I have been working on, although it is not perfect:

Code

Answer №1

Don't miss checking out this jsFiddle example

If you need to pass the index to a directive, simply utilize the already defined $index within the scope. Access it in the checkboxAll directive by using scope.$index.

app.directive('checkboxAll', function () {
return function(scope, iElement, iAttrs) {
var arrayContent=scope.mainList[scope.$index].list;

iElement.attr('type','checkbox');
iElement.attr('value','All');
//iElement.attr('id','All'+parts[2]);//check this id in design
iElement.bind('change', function (evt) {
var selectedval=evt.currentTarget.value;
  scope.$apply(function () {
    var setValue = iElement.prop('checked');
    angular.forEach(arrayContent,function(v){
        v.isSelected=setValue;
    });
  });
});

For the popup behavior, I introduced div.subpart1 as a child of div.sub1. div.subpart1 has position:absolute, allowing you to set the margin-left property for a popup effect.

To manage the visibility of the popups, I included an array $scope.IsVisible=[false,false], which tracks the visibility states of all popups.

The HTML structure is as follows:

<div class="sub1" >
<div class="subpart1" ng-repeat="val in mainList" ng-click="click($event,$index)" >
  <div id={{val.name}} class="mainPopup" ng-show="IsVisible[$index]" >
    <div class="rowSep">
  <input checkbox-all class="clsInput" /> 
       <div class="sub" ng-click="testa($event,val.list)" parent="Allid{{$index}}"> All</div>
    </div>
    <div ng-repeat="elem in val.list" class="rowSep">
      <input type="checkbox" ng-model="elem.isSelected" class="clsInput" /> 
      <div class="sub" ng-click="testa($event,val.list)"> {{elem.desc}}</div>
    </div>
</div>
</div>
</div>

Answer №2

Feel free to use this handy jsfiddle link for checking everything,

HTML

<div>
<ul ng-controller="checkboxController">
    <li>
        <input type="checkbox" ng-model="selectedAll" ng-click="checkAll()" /> Check All
    </li>
    <li ng-repeat="item in Items">
        <label>
          <input type="checkbox" ng-model="item.Selected" /> {{item.Name}}
        </label>
    </li>
</ul>

JS

angular.module("CheckAllModule", [])
.controller("checkboxController", function checkboxController($scope) {


$scope.Items = [{
    Name: "Item one"
}, {
    Name: "Item two"
}, {
    Name: "Item three"
}];
$scope.checkAll = function () {
    if ($scope.selectedAll) {
        $scope.selectedAll = true;
    } else {
        $scope.selectedAll = false;
    }
    angular.forEach($scope.Items, function (item) {
        item.Selected = $scope.selectedAll;
    });

};

});

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 adjusting column positions in a table while maintaining a fixed header and utilizing both horizontal and vertical scrolling

Is there a way to display a table that scrolls both horizontally and vertically, with the header moving horizontally but not vertically while the body moves in both directions? I have been able to shift the position of the columns, however, I am struggling ...

What is the best way to pass parameters in jQuery using .on()?

Hello there, I have a slight dilemma :) Could you please advise me on how to pass a parameter to an external JavaScript function using the .on method? Here is the code snippet: <script> var attachedPo = 0; $this.ready(function(){ $ ...

Error: Issue with hook function call detected. Struggling to locate exact source in React JS

As I dive into React for the first time, I'm working on creating a sign-up form with multiple steps. Despite reading through the material-ui documentation and learning about ReactJS, I'm struggling to pinpoint where I've gone wrong. Here&ap ...

Transform the query results into clickable links

Is there a way to automatically turn specific results fetched from a MySQL $row into hyperlinks? For example, if I fetch the website Google.com in a search query, is there a method for making it an active hyperlink that goes directly to Google.com when cli ...

Issue encountered while attempting to include a background image in React JS

I encountered an issue when attempting to include a background image in react js. ./src/index.css (./node_modules/css-loader/dist/cjs.js??ref--5-oneOf-4-1!./node_modules/postcss-loader/src??postcss!./src/index.css) Unable to locate module: An attem ...

Can TypeScript modules be designed to function in this way?

Seeking to create a versatile function / module / class that can be called in various ways: const myvar = MyModule('a parameter').methodA().methodB().methodC(); //and also this option should work const myvar = MyModule('a parameter') ...

What is the best way to retrieve the inner HTML or text content of a tag?

How can I retrieve the text value of an anchor tag in HTML using only golang.org/x/net/html, without external libraries? In my code sample below, I am able to extract the values of href and title with html.ElementNode. However, I need a way to specifically ...

Tips for handling race conditions in webdriver

My app uses a combination of angular and asp.net, but I'm facing an issue with the home page redirection. Initially, all routing was handled by the angular app itself, but due to certain requirements, we had to change it so that the home page redirect ...

Displaying variables in JavaScript HTML

<script type ="text/javascript"> var current = 0; </script> <h3 style={{marginTop: '10', textAlign: 'center'}}><b>Current Status: <script type="text/javascript">document.write(cur ...

Displaying a subset of categories based on the user's selection

I have been trying to find a solution to automatically display a subcategory select drop-down only when a user selects a category. If no category is selected, the subcategory drop-down should remain hidden. I have searched online tutorials and videos for ...

The shading of the box isn't displaying the correct color in Microsoft Edge and IE 11

I have a unique pattern consisting mainly of white and grey hues. To add a touch of color, I am using the box shadow property in CSS to apply a blue filter effect. This technique successfully displays the desired result in Firefox and Chrome browsers. Howe ...

Service injection results in an error

I am encountering an issue while trying to inject a service into a component, and the error message I receive is as follows: EXCEPTION: TypeError: Cannot read property 'getDemoString' of undefined It appears that the service is not being prop ...

Calculating the Vertical Size of a Component within Django Template Design

Within a div element with a fixed width, I have a p element with the following CSS properties: height:100px; overflow:hidden; I am looking to implement a functionality where a MORE button is displayed if the text within the paragraph overflows and is hid ...

Different approach for searching and modifying nested arrays within objects

Here is an example of the object I am working with: { userId: 111, notes: [ { name: 'Collection1', categories: [ { name: 'Category1', notes: [ {data: 'This is the first note& ...

Uploading and previewing files in Angular

Using the ng-file-upload library, I am able to successfully post files to my backend Web Api. The files are saved in the folder: "~/App_Data/Tmp/FileUploads/" The file path is also stored in my database. When I enter edit mode, I want to display a previ ...

Show a plethora of images using the express framework

I have two closely related questions that I am hoping to ask together. Is there a way for express (such as nodejs express) to handle all requests in the same manner, similar to how http treats requests with code like this: pathname = url.parse(request.url ...

Can anyone suggest methods for displaying query data from a JSON file using HTML?

After countless hours of searching through various forums, I have attempted numerous methods to display query data from a JSON file onto an HTML page. My initial attempt was using XmlHttpRequest, which yielded no results. I then tried utilizing jQuery, sp ...

Modify session variable upon link click

Here is an extension of the question posed on Stack Overflow regarding creating a new page for different PHP ORDER BY statements: Create a new page for different php ORDER BY statement? The task at hand requires changing a session variable and refreshing ...

Definition of a class in Typescript when used as a property of an object

Currently working on a brief .d.ts for this library, but encountered an issue with the following: class Issuer { constructor(metadata) { // ... const self = this; Object.defineProperty(this, 'Client', { va ...

Transferring information from Child to Parent using pure Javascript in VueJS

I am familiar with using $emit to pass data from child components to parent components in VueJS, but I am trying to retrieve that value in a JavaScript function. Here is my situation: Parent Component created () { this.$on('getValue', func ...