Determine if the data in an HTML table should be spanned when it

I'm facing a scenario where my HTML table contains duplicate data in a particular column, as illustrated below

I am looking to automatically merge the cells of rows in the HTML table if the data in them is identical, similar to the table displayed below

While I have been successful in manually specifying the number of cells to span, I am seeking a way to achieve this dynamically

Answer №1

Impressive task, thank you for the challenge. Here is the solution I devised http://jsfiddle.net/abc123/7/. Although the code might not be the most elegant, I'll leave that part up to you to refine.

Visualize your table as a standard 2-dimensional array, which simplifies the problem to utilizing nested loops. By examining each cell in every row within the same column, we can identify duplicates and track their first and last occurrences to determine the span length later on.

Remember to preset all "rowspan" values prior to removing any duplicates.

Answer №2

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Tables</title>
  </head>
  <body>
    <table border="black">
        <caption>Table</caption>
       <tr>
        <th>Name</th>
        <th>Subject</th>
        <th>Parts</th>
       </tr>
       <tr>
        <td rowspan="5">Shiva</td>
        <td rowspan="2">Math</td>
        <td>A</td>
       </tr>
       <tr>
        <td>B</td>
       </tr>
       <tr>
        <td rowspan="3">Science</td>
        <td>A</td>
       </tr>
       <tr>
        <td>B</td>
       </tr>
       <tr>
        <td>C</td>
       </tr>
       <tr>
        <td rowspan="4">Ram</td>
        <td rowspan="2">Math</td>
        <td>A</td>
       </tr>
       <tr>
        <td>B</td>
       </tr>
       <tr>
        <td rowspan="2">Science</td>
        <td>A</td>
       </tr>
       <tr>
        <td>B</td>
       </tr>
    </table>
  </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

Implementing multiple dropdown menus with Material UI in a navigation bar

I am currently working on designing a navigation bar that consists of multiple material UI dropdown menus. However, I have encountered an issue that has proven to be quite challenging for me to resolve. Whenever I click on a navigation bar item, all the dr ...

Turn off the scroll function while loading a webpage

Here is the script for my preloader: $(window).load(function() { $("#loading").fadeOut(1000); I want to prevent scrolling while the "loading" is still visible, and then enable it again after the fade out completes. ...

Obtain the native element of a React child component passed through props, along with its dimensions including height and width

Is there a way in React to access the native HTML element or retrieve the boundaries of an element passed as a child in props? Consider the following component: class SomeComp extends Component { componentDidMount() { this.props.children.forEa ...

Choose a specific date on a Materialize datepicker and set it as the selected date

I am currently developing an application using Materialize that includes two datepickers: $(document).ready(function(){ $('#outDate').datepicker({ format: 'dd-mm-yyyy' }); }); $(document).ready(function(){ $(&apos ...

Clicking on an element deactivates its hover state

While experimenting on this JSFiddle, I noticed that after clicking one of the boxes, the hover state becomes inactive and doesn't show up again. These three boxes have their background set using css background-image. The click event is attached usin ...

Are memory leaks a common issue with Angular directives?

Using a simple html file to replicate a memory leak: <!doctype html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.6/angular.min.js"></script> <script> va ...

Refreshing the settimeout function through a click event

I'm working on a feature where I use setTimeout to dynamically change the font size of paragraphs. My goal is to have the font size reset when the user clicks on the "reset" button or initiates the timer again. However, I've encountered an i ...

What methods are typically used for testing functions that return HTTP observables?

My TypeScript project needs to be deployed as a JS NPM package, and it includes http requests using rxjs ajax functions. I now want to write tests for these methods. One of the methods in question looks like this (simplified!): getAllUsers(): Observable& ...

Creating a Striking Multi-Layered CSS Header

Can someone help me figure out how to add horizontal lines behind the words in this header? Here is what I have so far: https://i.sstatic.net/dN7s4.jpg. However, I want to achieve something similar to this design: https://i.sstatic.net/FZoSF.jpg. You can v ...

Utilizing Server-Sent Events to display a interactive navigation button

Currently, I am managing a web-based point of sale system where some buttons are hidden for specific functions. To streamline the process, I would like to allow managers to simply click on an "Enable" button in the admin panel and have it immediately refle ...

Issue with iPhone CSS displaying differently on mobile devices

The CSS design does not display correctly on iPhone devices in real-life testing, even though it appears fine on browsers with mobile view emulators. Interestingly, the design also looks great on Android phones but encounters issues specifically on iPhones ...

I need assistance improving my poor JavaScript patterns and practices. Where should I turn for help?

After focusing primarily on back-end tasks for the past few years, it's become apparent to me that JavaScript (and CoffeeScript) projects have evolved significantly in my absence. Working mainly in a rails environment, my previous JavaScript/jQuery i ...

Is it possible to precisely position multiple children in a relative manner?

I am currently developing a custom dropdown menu where I want the parent element to be relatively positioned within the page. The goal is for all the child elements of the dropdown menu (the list items) to appear as if they are part of a select element and ...

Sophisticated jQuery templates

Working with jQuery templates can be straightforward for basic data structures. <script type="jquery/x-jquery-tmpl" id="myTemplate"> <li> ${Element} </li> </script> var elements = [ { Element: "Hydrogen" }, { Element: "Oxy ...

Personalizing the height and width of a jQuery UI Slider

While adjusting the height/width of the jQuery UI Slider and handle, I've noticed that the handle sometimes slides too far to the left, going off the slider. Is there a recommended way to prevent this from happening? You can see what I mean by checki ...

Entry is automatically added to the form as soon as the page is loaded

I am trying to create a form using PHP, but I want the script to only run after the submit button is pressed. Currently, it runs every time the page loads and overloads the box on the same page. Unfortunately, my knowledge of PHP is limited! Any help wou ...

Syntax for retrieving response with jQuery AJAX

Currently, I am in the process of working on an AJAX request that triggers a URL when a specific button is clicked. The fundamental aspects are running smoothly, ensuring that the GET request is activated towards the URL upon button click. I have also cros ...

Is there a way to position the Bootstrap navbar menu items in the center specifically for mobile devices?

I have a navbar that I want to center the menu li items when it collapses. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9dbd6d6cdcacdcbd8c9f98c978b978b">[ema ...

Extremely efficient CSS utility classes for supreme performance

I've noticed the rise of CSS utility classes and I'm curious about their impact on performance. Is there a better approach? Option One - creating HTML elements with multiple utility classes like: <div class="padding flex justifyCenter align ...

What is the best way to store changing images in a Next.js application?

Is it possible to set the cache-control for images in a list of objects received from an API? Each object in the list contains a property called imageUrl, which is the link to the image. ...