What are the steps to add border styles to the top and bottom rows in a tanstack expandable react table?

In my project, I am utilizing the combination of Material UI and React Table. Recently, I was asked to add an expandable row, and I successfully implemented that feature. Once a user expands the row, we display additional table rows based on the data.

If you would like to see a working demo of this implementation, you can visit this link: https://codesandbox.io/s/tanstack-table-expansion-sub-level-goe-191pip?file=/src/styles.css:0-8

To further clarify the requirements, please refer to the attached screenshots (https://i.sstatic.net/UfbnL.jpg and https://i.sstatic.net/TntOx.jpg). The task now is to add border-top and border-bottom styles to the first and last row respectively in the list of expanded rows. Each expanded row has a className of depth-1, with subsequent deeper expansions labeled as depth-2.

While I have been able to differentiate the background colors of the expanded rows, applying only the desired border styles proved challenging. I attempted using nth-of-type, as well as first-child and last-child selectors, but encountered difficulties.

I am seeking advice on the feasibility of achieving this task. Is my current approach correct?

Following the guidance provided:

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

Answer №1

When using only CSS, adding a top border is straightforward. However, applying a bottom border can be more challenging. As far as I know, selecting the "last consecutive element" of a specific type or class is not possible.

Below is the CSS code to add a top border to the tr element with a depth of 1.

.depth-0 + .depth-1 {
  border-top: solid 3px red;
}

If you need a bottom border, one alternative is to use JQuery.

$(document).ready(function() {
    //$('.depth-0').prev('.depth-1').addClass('borderBottomRed');
});
.depth-0 {
  box-sizing: border-box;
    background-color: yellow;
    width: 100%;
    padding: 5px;
}

.depth-1 {
  box-sizing: border-box;
    background-color: green;
    width: 100%;
    padding: 5px 15px;
}

.borderBottomRed {
    border-bottom: solid 3px red;
}


.depth-0 + .depth-1 {
  border-top: solid 3px red;
}

.depth-1 + .depth-0 {
  border-top: solid 3px red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="depth-0">main</div>
<div class="depth-1">child</div>
<div class="depth-1">child</div>
<div class="depth-1">child</div>
<div class="depth-0">main</div>
<div class="depth-1">child</div>
<div class="depth-1">child</div>
<div class="depth-0">main</div>
<div class="depth-1">child</div>
<div class="depth-0">main</div>
<div class="depth-0">main</div>

Edit

As proposed by the author of the question, here is a potential solution for adding a bottom border in css:

.depth-1 + .depth-0 {
  border-top: solid 3px red;
}

.depth-1:last-child {
  border-bottom: solid 3px red;
}

Full code snippet:

.depth-0 {
  box-sizing: border-box;
    background-color: yellow;
    width: 100%;
    padding: 5px;
}

.depth-1 {
  box-sizing: border-box;
    background-color: green;
    width: 100%;
    padding: 5px 15px;
}

.borderBottomRed {
    border-bottom: solid 3px red;
}


.depth-0 + .depth-1 {
  border-top: solid 3px red;
}

.depth-1 + .depth-0 {
  border-top: solid 3px red;
}
.depth-1:last-child {
  border-bottom: solid 3px red;
}
<div>
  <div class="depth-0">main</div>
  <div class="depth-1">child</div>
  <div class="depth-1">child</div>
  <div class="depth-1">child</div>
  <div class="depth-0">main</div>
  <div class="depth-0">main</div>
  <div class="depth-1">child</div>
  <div class="depth-1">child</div>
  <div class="depth-0">main</div>
  <div class="depth-1">child</div>
  <div class="depth-0">main</div>
  <div class="depth-0">main</div>
  <div class="depth-1">child</div>
  <div class="depth-1">child</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

Endless loop occurs with post and put requests while attempting to refresh authentication token

Every time the token expires in my application with NEXTjs and I attempt to make a post request, it gets stuck in a loop. The POST request returns 401, then the refresh-token returns 200 after attempting the post again, it returns 401, and the cycle repeat ...

Tips for separating provider and input components with React Hook Form

Currently, I am working on a project with Next 13.5 using react-hook-form and shadcn-ui. The issue that I have encountered is that creating a form involves too much redundant code. To address this, I abstracted the FormProvider and Input component. The pr ...

An easy way to place text along the border of an input field in a React JS application using CSS

I am struggling to figure out how to create an input box with text on the border like the one shown in the image below using CSS. I have searched extensively but have not been able to find any solutions to achieve this effect. I attempted using <input&g ...

adjust the value of an attribute in a dynamic stylesheet using jquery

I'm trying to create a dynamic stylesheet using a combination of jquery, css, php, and html. My approach involves making an ajax request (using jquery) to pass the width of my screen to a php css file. However, I am facing an issue where the php css f ...

Has jQuery UI Accordion taken over the design of unordered lists?

I'm having trouble with my website's navigation styling getting messed up by the jQuery accordion. The unordered list inside the .accordion div is overflowing and losing its CSS styling. I've tried adding clearStyle true and autoHeight false ...

Unleashing the power of React: Integrating raw HTML <a href> tags with JavaScript

I am currently working on a small web app that mimics browsing WikiPedia by fetching raw HTML content of WikiPedia articles through its API. I then display this HTML in my app using "dangerouslySetInnerHTML". I am faced with the challenge of enabling a us ...

Distribute divs of equal width evenly within a grid

I'm facing a unique challenge at the moment. I'm attempting to create a grid system where all elements have a fixed width of 200px each. What I envision is a clever grid setup using only CSS, where each "row" will strive to accommodate as many el ...

The upcoming construction of 'pages/404' page will not permit the use of getInitialProps or getServerSideProps, however, these methods are not already implemented in my code

Despite my efforts to search for a solution, I have not found anyone facing the same issue as me. When I execute next build, an error occurs stating that I cannot use getInitalProps/getServerSideProps, even though these methods are not used in my 404.tsx f ...

The function buf.writeBigUInt64BE is not recognized as a valid function and returns an undefined error

I'm attempting to implement the code below on my React Native iOS device: import { Buffer } from "buffer"; const buf = Buffer.alloc(8) buf.writeBigUInt64BE(BigInt(123), 0) const value = buf.readBigUInt64BE(0) console.log(value) However, I&a ...

The <SelectField> component in material-ui is not displaying items properly in ReactJS

I am facing an issue with Material-UI where the items are not showing up. I am trying to display categories of products in a SelectField but it doesn't seem to be working. When I click on the SelectField, no items are displayed. Below is the code for ...

How can I incorporate dashed borders between images using CSS?

New to the world of HTML and CSS, I'm trying to replicate a design I came across online for some practice. The website layout includes images separated by borders, and I'm unsure if this particular design can be achieved using regular CSS alone o ...

Encountering issues with SignInWithRedirect feature's functionality

Whenever I attempt to SignInWithRedirect in my React project, I am redirected to a Google site where I only see a blue progress bar at the top before quickly being redirected back to my website. My intention is to be shown my Google sign-in options, but it ...

Pagination CSS may fail to function properly in Chrome after an AJAX call is returned

Having an issue with CSS not getting applied correctly after making an AJAX call with pagination. The problem seems to be specific to Chrome, as it works fine in Firefox. When inspecting the element, the CSS is present but not being applied properly until ...

How to insert a span element within a link tag in Next.js/React.js

Looking to create a breadcrumb using microdata in a Next.js and React.js project. Initially, I tried the following: <li itemProp="itemListElement" itemScope itemType="https://schema.org/ListItem"> <Link href={'/index&a ...

Is it possible to use Primefaces tooltip to show at the bottom of the page

When I hover over the PrimeFaces tooltip, it displays at the bottom of the page. Here is my code and image link: <!DOCTYPE html> <h:html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com ...

How to transform the Material UI functional component Mini variant drawer into a class component in ReactJS

I have been working with the mini variant drawer component from the official material-ui website and I am encountering some issues when trying to convert it into a class component. The errors seem to be related to hooks and are occurring within the node ...

React Context: issue with rendering the component

Within my translation app in React 17.0.1, I encounter an issue with a page consisting of two sections - one for input and the other for translations of that input. The challenge arises when attempting to update the UI after receiving results from a remote ...

What is the optimal method for allowing users to modify the website name and various frontend settings in a ReactJS environment?

Developing a blog web application using nodejs, reactjs, and mongodb. Implementing a feature where admin users can customize their blog's name, color scheme, header image, sidebar image, and content without developer intervention. To start with, focus ...

Issue found: React-Redux action is not being dispatched

I'm currently working on setting up Google authentication in my React Next.js application. The process involves sending the access token to my backend, where it is validated before a new token is returned in the header for accessing protected resource ...

Tips for creating horizontal scrolling in the div element

I'm working with a div element that looks like this: <div id="tl" style="float:right;width: 400px; height:100px; background-color:Green; overflow-x: scroll;overflow-y: hidden;"> <div id='demo' style="float:left;height ...