Using CSS, I have rearranged the content of a single row (tr) in an HTML table into two separate rows

I am currently facing a challenge with rearranging my table due to width constraints. My goal is to apply Jquery functions to the table in order to extract all parameters related to a product, but I want to avoid having nested tr elements or splitting parameters across multiple rows.

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

How can this be achieved?

    <table style="width: 300px">
    <thead>
        <tr>
    <th rowspan="2">Product</th>
    <th>Category</th>
    <th>Sub Category</th>
    <th>Quantity</th>
    <th>Arrival Date</th>
    <th>Manufactured Date</th>
    <th>Expiry Date</th>
        </tr>
    </thead>
    <tbody>
        <tr id="productID_1">
            <td rowspan="2">Skimmed Milk</td>
            <td>Dairy</td>
    <td>Milk</td>
    <td>1 Ltr</td>
    <td>1st Jan 2018</td>
    <td>1st Jan 2018</td>
    <td>7th Jan 2018</td>
        </tr>
        <tr id="productID_2">
            <td rowspan="2">Cheddar</td>
            <td>Dairy</td>
    <td>Cheese</td>
    <td>200 Grms</td>
    <td>1st Jan 2018</td>
    <td>1st Jan 2018</td>
    <td>15th Jan 2018</td>
        </tr>
        <tr id="productID_3">
            <td rowspan="2">Vanilla Ice Cream</td>
            <td>Desserts</td>
    <td>Ice Creams</td>
    <td>100 Grms</td>
    <td>15th Jan 2018</td>
    <td>1st Jan 2018</td>
    <td>31st March 2018</td>
        </tr>
    </tbody>
</table>

Here is a snippet of my JQuery code:

$("table#customerSalesTable tbody tr").each(function(index){ 
  // Code to capture each parameter 
} 

-- Update

Based on @CodeIt's suggestion, opting not to split single row into two rows and capturing values using classes would entail significant re-editing of my JS code.

Answer №1

To enhance the display of each <tr>, consider using a CSS grid instead of traditional table rows. This approach allows you to position each cell individually within the grid structure. One drawback is that since each row functions as its own grid, column widths cannot adjust based on their content to ensure alignment.

#customerSalesTable th, #customerSalesTable td {
  border: 1px solid grey; /* visible borders */
  margin: 0 -1px -1px 0; /* "border-collapse" for non-tables */
  /* Content overflow strategy for cells with long text */
  overflow-wrap: break-word; /* or overflow: hidden if preferred */
}

#customerSalesTable tr {
  display: grid;
  grid-template:               /* Row height ▼ */
    "product category subcategory quantity" auto
    "product arrival  manufacture expiry  " auto
    /25%     25%      25%         25%; /* Define column widths */
}

/* Assigning grid areas to specific columns */
#customerSalesTable th:nth-child(1),
#customerSalesTable td:nth-child(1) { grid-area: product; }
...
... (similar assignments for other columns)
...

<table id="customerSalesTable" style="width: 300px">
  <thead>
    <tr>
      <th>Product</th> <!-- rowspan doesn't help now -->
      ...
      ... (header row definitions)
      ...
    </tr>
  </thead>
  <tbody>
    <tr id="productID_1">
      <td>Skimmed Milk</td>
      ...
      ... (data rows)
      ...
    </tr>
    ... (additional data rows)
    ...
  </tbody>
</table>

Answer №2

Implementing JQuery

In each <tr> tag, there are 7 child nodes. By wrapping child nodes 1-4 and 5-7 with separate <tr> tags, the cells can be rearranged.

For instance:

Initially:

<tr>
    <th>Product</th>
    <th>Category</th>
    <th>Sub Category</th>
    <th>Quantity</th>
    <th>Arrival Date</th>
    <th>Manufactured Date</th>
    <th>Expiry Date</th>
</tr>

After:

<tr>
    <th>Product</th>
    <tr>
        <th>Category</th>
        <th>Sub Category</th>
        <th>Quantity</th>
    </tr>
    <tr>
        <th>Arrival Date</th>
        <th>Manufactured Date</th>
        <th>Expiry Date</th>
    </tr>
</tr>

// code to wrap table headers
var e = $('th').slice(1, 4);
e.wrapAll("<tr/>");
var e = $('th').slice(-3);
e.wrapAll("<tr/>");

// code to wrap table rows
var e = $('td').slice(1, 4);
e.wrapAll("<tr/>");
var e = $('td').slice(4, 7);
e.wrapAll("<tr/>");
var e = $('td').slice(8, 11);
e.wrapAll("<tr/>");
var e = $('td').slice(11, 14);
e.wrapAll("<tr/>");
var e = $('td').slice(15, 18);
e.wrapAll("<tr/>");
var e = $('td').slice(18, 21);
e.wrapAll("<tr/>");

// use #productID_* to capture parameters 
var e = $('#productID_2').find('td');
console.log(e);
td,
th {
  width: 100px !important; // sets cell width
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table style="border-collapse: collapse;" border=1>
  <thead>
    <tr>
      <th>Product</th>
      <th>Category</th>
      <th>Sub Category</th>
      <th>Quantity</th>
      <th>Arrival Date</th>
      <th>Manufactured Date</th>
      <th>Expiry Date</th>
    </tr>
  </thead>
  <tbody>
    <tr id="productID_1">
      <td>Skimmed Milk</td>
      <td>Dairy</td>
      <td>Milk</td>
      <td>1 Ltr</td>
      <td>1st Jan 2018</td>
      <td>1st Jan 2018</td>
      <td>7th Jan 2018</td>
    </tr>
    <tr id="productID_2">
      <td>Cheddar</td>
      <td>Dairy</td>
      <td>Cheese</td>
      <td>200 Grms</td>
      <td>1st Jan 2018</td>
      <td>1st Jan 2018</td>
      <td>15th Jan 2018</td>
    </tr>
    <tr id="productID_3">
      <td>Vanilla Ice Cream</td>
      <td>Desserts</td>
      <td>Ice Creams</td>
      <td>100 Grms</td>
      <td>15th Jan 2018</td>
      <td>1st Jan 2018</td>
      <td>31st March 2018</td>
    </tr>
  </tbody>
</table>

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

What could be causing the 500 internal error in my AJAX code?

My code is generating an error and I need help identifying the issue: Ajax Call back Code: $(document).ready(function(){ $('#btn2').click(function(e){ e.preventDefault(); var cate=$( "#cate option:selected"). ...

The date range picker displays the previous arrow but not the next arrow

I am currently using the DateRangePicker tool and for some reason, I am not seeing the arrow that should appear on the right side. I have double-checked my configuration but can't seem to figure out what is causing this issue. In the image attached, ...

extracting the data from the URL parameter and showing it on the screen

Is there a reason why nothing displays if the name variable is missing from the URL? I've set up code to show a greeting message along with the name value extracted from the URL, but it doesn't work when the name is not present. Any ideas on what ...

Server side Meteor with reactive coffee on client

Is it possible to run 'client' Meteorite packages on the server? I want to implement reactive coffee for generating a newsletter on the server. I haven't been able to find any information on this, even though it seems like a logical choice. ...

Overriding the Ajax URL

During an AJAX request in a Rails app triggered by onchange event, I am encountering an issue with the URL being called. The function for the request is a simple PUT operation: function quantityChange(id, val) { $.ajax({ url: 'cart_items/ ...

Numerous data points contained within a single cell of a table

I am in the process of developing a website, but I am facing a challenge. I am working on a display for a DataTable using HTML and C#, which needs to be sortable, but with a specific feature in mind. My dataset consists of objects with a Name and a Value, ...

The JSON data response is not being properly displayed on the div element

I am having trouble with the success function in my ajax call. The data is processed correctly in the view and the ajax call works fine, but for some reason, the data is not getting appended to the div. Here is my jQuery: $(document).ready(function() { ...

What is the best way to use CSS to center two blocks with space in between them?

My goal is to achieve a specific design: I want two text blocks with some space in between them aligned around the midline of the page (refer to the image). I have experimented with the float property, as well as used margin and padding to create the gap ...

How can I delete a CSS class from an element?

Is there a way to remove the "ui-widget-content" class from the code below? It appears in multiple places throughout my code. Here is an example snippet: <pre> <form id="clientForm"> <div id="clientData"> <div id="gbox_grid_1" class=" ...

Having trouble with Bootstrap dropdowns not opening when clicking with jQuery?

I am in the process of developing a table with multiple rows, each containing an "Options" button to display a dropdown context menu. To streamline the code, I am utilizing a single div to serve as a common markup for the context menu. The technologies I ...

SASS - centering timeline content vertically

I am a beginner in frontend development and I am currently using sass. I have created a custom timeline, but I need assistance in properly aligning the location with the year and timeline marker. Additionally, I would like to position the image description ...

Step-by-step guide for implementing tooltips using jQuery

I've been attempting to implement a tooltip using jQuery UI. However, when I use var_dump I am not getting any output. Here is the code snippet: <a href="#"><span id='11111_22222'>text_here</span></a> And this is ...

Include a hyperlink in an email using PHP

I am currently using an HTML form textarea along with PHP to send emails through my website. My question is, how can I successfully insert a link into the email message body? I attempted to do it like this: <a href="http://www.xxxxx.com/">Visit the ...

Instructions on setting div opacity when Overflow is set to visibleExplanation on setting opacity for div with Overflow

In my HTML code, I have a div element and an image tag stacked one on top of the other. The div is smaller than the image. I have set the overflow property to visible for the div. My query is, when I add an image to the image tag, the content that overflow ...

HTML/CSS Inactive: Troubleshooting and Solutions

There seems to be an issue with the :active state, if anyone can spot the problem and provide assistance, it would be greatly appreciated. Here is the HTML code: http://pastebin.com/4wCi3L2Z And here is the CSS code: http://pastebin.com/jZvgdDaA Thank y ...

JavaScript/jQuery: What is the best way to assign an element as the context ('this') of a function?

Is there a way to pass something to a function and have it act as if it's calling the function itself? Consider this function: function ShowId() { alert($(this).attr('id')); } and this block of HTML: <div id='div1'> & ...

Should the potential benefits of implementing Responsive Design in IE8 be taken into account in 2013?

It seems that there are still questions lingering about how to make responsive design compatible with outdated browsers like IE8 or even the dreaded IE7. Personally, I question the need for implementing responsive design specifically for IE8, considering ...

Tips for inserting the final array into the designated div

While working on creating a navigation menu with dropdown and multi-menu functions, I encountered some issues that needed to be addressed. After making several improvements, there is still one area that confuses me. Perhaps I can find the solution here. ...

Vue.js - Including new information in an array and passing it to the props (assistance needed)

I am facing an issue where I want my component to be able to add a new sidebar menu item every time the user clicks an add button. Essentially, my component should appear when the user is defining their own sidebar menu items. Here is the Vue.js code that ...

Having trouble loading external CSS in HTML?

I have been attempting to load an external CSS file into my current HTML file without success. I have provided images showcasing the code: View the code here See the output here Currently, I am working on Windows and using Aptana Studio 3 as my IDE. ...