Having difficulty implementing a pseudo-selector with a custom directive within an ng-repeat loop

I have created a directive for a table with collapsible rows that allows only one row to be open at a time. Here is how it looks:

HTML:

<div class="my-table">
    <div class="table-header">
        ... table headers ...
    </div>
    <my-table-row ng-repeat="itm in itms" itm="itm"></my-table-row>
</div>

JS Directive:

app.directive('myTable', function() {
return {
    restrict: 'E',
    scope: { 
        itms: '='
    },
    controller: 'TableController',
    templateUrl: '/views/directives/my-table.html'
};
});

JS Controller:

app.controller('TableController', ['$scope', function($scope) {
$scope.rows = [];

$scope.toggleRow = function(row) {
    row.open = !row.open;
};

this.addRow = function addRow(row) {
    $scope.rows.push(row);
};

this.toggleOpen = function toggleOpen(selectedRow) {
    angular.forEach($scope.rows, function(row) {
        if (row === selectedRow) {
            $scope.toggleRow(selectedRow);
        } else {
            row.open = false;
        }
    });
};
}]);

and the rows like this:

HTML:

<div class="table-row" ng-class="{ 'open': open }" ng-click="toggleOpen(this)">
    ... row contents code ...
</div>

JS Directive:

app.directive('myTableRow', function() {
return {
    require: '^myTable',
    restrict: 'E',
    scope: { 
        itm: '='
    },
    link: function(scope, element, attrs, tableCtrl) {
        scope.open = false;
        scope.toggleOpen = tableCtrl.toggleOpen;
        tableCtrl.addRow(scope);
    },
    templateUrl: '/views/directives/my-table-row.html'
};
});

used in template like this:

<my-table itms="itms"></my-table>

Everything functions as intended, but I have encountered an issue with CSS styling. Specifically, I have a pseudo element applying rounded corners to the last row of the table like this:

.table .table-row:last-child {
    border-radius: 0 0 4px 4px;
}

Unfortunately, due to ng-repeat wrapping tags around the table rows, the pseudo selector sees them all as the last child. I have attempted various solutions, such as restructuring the layout, using $last, creating a specific class for the last row, and rearranging elements, but so far nothing has worked. Any suggestions on how to resolve this?

Answer №1

In my understanding, the CSS class table-row is found within the myTableRow directive, which does not have the property replace: true. This means that the table-row CSS class is encompassed by the my-table-row directive attribute. To target the last row, your CSS rule should look like this:

.table my-table-row:last-child .table-row {
  border-radius: 0 0 4px 4px;
}

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

Elements that are fixed are not visible on the browser's screen

Currently, I am facing an issue with two divs that have a position: fixed; property. Everything functions properly until I zoom in on the page. Typically, when you zoom in on a page, two sliders appear to allow you to view content beyond your screen. Howev ...

When using the onclick function, an unexpected behavior occurs where instead of displaying content, the page

Below is a summarized version of my code: HTML <a onclick="showHideDiv('bio')" target='_self'> bio <div id="bio" class="shadowScreen" style="display: none;"> <p class="showBio"> Bio! </p> &l ...

Utilizing external JavaScript plugins in Scalajs and Angularjs: A guide

I'm attempting to integrate an external JavaScript plugin into my ScalaJs Angular directive, but I'm unsure of the best approach. Currently, I have added a function to the window. The JavaScript plugin has the following structure: (function(){ ...

Is it possible to count the number of days in between and then apply a specific class to each of those days

Here is the code snippet that I am working with: <div class="row"> <div class="test">02/12/2013</div> <div class="test">03/12/2013</div> <div class="test">04/12/2013</div> <div class="test"> ...

How to set the default theme color for the mat-sidenav background in Angular 6 and 7?

Is there a way to make the background of a mat-sidenav match the theme color of my mat-toolbar? In the file src\styles.scss, I have the following: @import '~@angular/material/prebuilt-themes/indigo-pink.css'; The template / HTML file incl ...

Angular components are not properly adhering to the specified height and width dimensions for child elements

I seem to be having trouble applying height/width to a child component in angular. Can someone take a look and point out where I may have gone wrong? app.component.html <app-child [width]="10" [height]="10"></app-child> .child.ts import { C ...

The distance separating the top navigation bar from the sub-navigation menu

I have designed a navigation menu with a subnavigation section, view it on JSFiddle with subnavigation. My challenge is to create a 1px solid white separation between the top navigation (with yellow background) and the subnavigation (with red-colored backg ...

Altering a Common Variable Across Several AngularJS Directives

Check out this jsfiddle I created: http://jsfiddle.net/noahgoodrich/CDwfL/1/ I've developed a collection of directives to control and manipulate navigation tabs. Strangely, when I try to close a tab, it initially removes the correct array element, b ...

Circular CSS menu

What I'm Trying to Achieve: I am interested in developing a unique radial menu that includes interactive elements, such as the central image and the surrounding sections. It is imperative that the solution is compatible across all browsers. The examp ...

Difficulty encountered while implementing a carousel using HTML, CSS, and JavaScript

I'm currently working on creating a carousel using only HTML, CSS, and JS. While it is functional, it does not perform as smoothly as I had anticipated. After completing one full round of images, there is an approximate 8-second delay before it star ...

Efficiently aligning divs within a container using Bootstrap 5 without altering the overall layout

I've been trying to figure out a solution to this issue, but so far nothing seems to work. I'm working on creating some general components for a system using Bootstrap 5. I've defined a button like this: <div class="col buttonDiv&quo ...

Alter an ng-model-options attribute dynamically

I am currently facing a challenge where I need to display a time input field based on a timezone offset determined by the user-profile chosen on the form. To achieve this, I have created a function called getTzOffset in the scope of a link function. I am ...

How can I use Bootstrap and PHP to style future posts in separate rows on a website?

I am attempting to design a categories navigation menu using Bootstrap 5. It consists of two columns, the first displaying featured posts and the second showing 5 recent posts. Here is an example of what I have tried, but it didn't work as expected b ...

Issue with Angular JS: ng-model does not update when input value is changed using jQuery

As a newcomer to AngularJS, I am currently working on creating a directive that consists of a list of input fields. The goal is for the "done" function to be triggered when the comma key (which corresponds to the number 188 in the plunker) is pressed, resu ...

Controller unable to access form inside ng-repeat loop in AngularJS

Looking to access a form object within my controller. The form is located inside an ng-repeat block with a dynamic name, and I should be able to access it since version 1.3. JavaScript: var app = angular.module('app', []); app.controller(' ...

Creating a comparison display using ng-repeat

I have a JSON file that contains revision and history information about a modified entity, including its old and new values. The diff is generated using the jsondiffpatch library and I have parsed it further to create the final JSON format for rendering. ...

Looking to establish a minimum height for a webpage

I am working on creating a webpage with three distinct sections: A fixed height header A fluid main section A fixed height footer My goal is to ensure that the page has a minimum height so that the bottom of the footer aligns with the bottom of the wind ...

Guide on retrieving inline style that has been overridden by CSS using JavaScript

<div style="background: url(http://www.example.com/image.jpg) center center/cover;"> This specific background image defined inline is being replaced by a rule in an external stylesheet: div { background-image: none !important; } Here's my q ...

The alignment of the container (heading) does not match that of the row (form) in Bootstrap 5

As mentioned in the title, I am looking to ensure that the rows have the same spacing as the container. I am currently using Bootstrap 5, so when you preview the HTML on Stack Overflow, it may not appear exactly as intended. To provide a clearer understand ...

What is the best way to find out the size of a Google font file?

Google fonts are being utilized in my project, specifically the Roboto font for light and regular font styles. Unfortunately, I am unsure of the file size of this particular font. ...