Revamp the table layout for mobile with a unified markup structure

I want to customize the display of my bootstrap table for mobile view. Each row entry should be shown in a separate box or card, similar to the following examples:

First: Mark
Last : Otto
Handle: @mdo

Second card-

First:Jacob
Last:Thornton
Handle:@fat

Is there a way to achieve this without creating additional HTML specifically for mobile view? I am using Bootstrap 4.

<table class="table">
  <thead>
    <tr>
      <th scope="col">#</th>
      <th scope="col">First</th>
      <th scope="col">Last</th>
      <th scope="col">Handle</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">1</th>
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
      <th scope="row">2</th>
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
      <th scope="row">3</th>
      <td>Larry</td>
      <td>the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
</table>

Answer №1

You have the ability to customize each element to your preferred look. Remember that you can only add static information or incorporate it as an attribute within an element. In this case, I have added the col attribute to each td element as an example:

document.getElementById('toggle').onclick = () => {
  const table = document.getElementById('table');
  table.classList.toggle('cards');
}
.cards thead {
  display: none;
}

.cards tr {
  display: flex;
  flex-direction: column;

  margin-bottom: 5px;
  border: 1px solid black;
}

.cards tr > th {
  display: none;
}

.cards tr > td::before {
  content: attr(col) ': ';
  font-weight: bold;
}
<button id="toggle">Toggle Style</button>
<table class="table" id="table">
  <thead>
    <tr>
      <th scope="col">#</th>
      <th scope="col">First</th>
      <th scope="col">Last</th>
      <th scope="col">Handle</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">1</th>
      <td col="First">Mark</td>
      <td col="Last">Otto</td>
      <td col="Handle">@mdo</td>
    </tr>
    <tr>
      <th scope="row">2</th>
      <td col="First">Jacob</td>
      <td col="Last">Thornton</td>
      <td col="Handle">@fat</td>
    </tr>
    <tr>
      <th scope="row">3</th>
      <td col="First">Larry</td>
      <td col="Last">the Bird</td>
      <td col="Handle">@twitter</td>
    </tr>
  </tbody>
</table>

Q: How can I adjust the space between the heading and data in the flex view?
A: You can increase the padding on the right side of the ::before element.

document.getElementById('toggle').onclick = () => {
  const table = document.getElementById('table');
  table.classList.toggle('cards');
}
.cards thead {
  display: none;
}

.cards tr {
  display: flex;
  flex-direction: column;

  margin-bottom: 5px;
  border: 1px solid black;
}

.cards tr > th {
  display: none;
}

.cards tr > td::before {
  content: attr(col) ': ';
  font-weight: bold;
  padding-right: 100px;
}
<button id="toggle">Toggle Style</button>
<table class="table" id="table">
  <thead>
    <tr>
      <th scope="col">#</th>
      <th scope="col">First</th>
      <th scope="col">Last</th>
      <th scope="col">Handle</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">1</th>
      <td col="First">Mark</td>
      <td col="Last">Otto</td>
      <td col="Handle">@mdo</td>
    </tr>
    <tr>
      <th scope="row">2</th>
      <td col="First">Jacob</td>
      <td col="Last">Thornton</td>
      <td col="Handle">@fat</td>
    </tr>
    <tr>
      <th scope="row">3</th>
      <td col="First">Larry</td>
      <td col="Last">the Bird</td>
      <td col="Handle">@twitter</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 is the mechanism behind lazy module loading in typescript?

Exploring the concept of lazy loading in typescript, the author provides an example in this section of their book. They demonstrate it using the following piece of code: import bar = require('bar'); export function loadBar() { ...

Preventing the display of any object items that include a period character in AngularJS

There seems to be an issue with a JSON item not displaying correctly in AngularJS. error = { "name": [ "The name field is required." ], "salary.salary_id": [ "The salary.salary id field is required." ] } When attempting to ...

jQuery and Bootstrap collide

Here is my jQuery code that toggles visibility of different divs based on a click event: $(function () { $(".kyle-div, .tracey-div, .frank-div, .rosie-div").hide(); $("a").bind("click", function () { $(".conor-div, . ...

The presence of the !doctype html tag completely messes up my

I am currently working on a code snippet that is supposed to provide the screen coordinates of a given object: <!DOCTYPE HTML> <html> <head> </head> <body style="margin:0px;padding:0px;"> <div id="help" style="top:100px;r ...

Reorder an array of objects in JavaScript alphabetically, taking into account any possible null values

Encountering a problem with sorting contacts by first name, especially when some contacts are missing this information. Any suggestions on how to modify the method for such cases? Thanks! This is the current sorting function I am using: function sortAZ(o ...

Reactjs is failing to display the page

I am facing an issue where the components are not rendering in the browser even though there is no error being displayed. This makes it difficult to troubleshoot and resolve the problem. Can someone help me identify the root cause? import React, { Com ...

The Add/Remove button functionality is not functioning as expected for duplicated form fields when using jQuery

I am experiencing an issue with the functionality of the "Add another" and "Remove row" buttons in my form. I implemented code from a specific Stack Overflow answer (jquery clone form fields and increment id) to create a cloneable section, but only the ori ...

Use a swipe gesture to move through the slideshow on a touchscreen device

I am currently working on a slideshow that allows users to navigate through images by clicking either the next or previous buttons. When a user clicks on one of these buttons, the next or previous image is loaded into the img src, as shown below. In addit ...

Perform calculations for product and sum when button is clicked

I received the following task for an assignment: Develop 3 functions along with a form. The first function should execute upon button press and utilize the other 2 functions. These two functions are supposed to compute the sum of two numbers and the ...

Guide on using JavaScript to automatically scroll a HTML page to the top on any mobile browser

Can JavaScript be utilized to smoothly scroll an HTML page to the top? I am looking to achieve this with a stylish animation that functions correctly on all mobile browsers. jQuery is the library I am using on this particular page. Thank you, ...

The length function appears to be signaling an unanticipated error

Recently, I encountered an issue with the code execution. Although the code appears to be functioning correctly, it is throwing an uncaught error. Is there a reason for concern regarding this situation? var xmlhttp = new XMLHttpRequest(); xmlhttp.onread ...

Updating Content Dynamically with AJAX, JavaScript, and PHP without requiring a page reload or user action

Currently, I'm delving into the world of JS/AJAX functions that operate without the need for a page refresh or button click. However, I've encountered an issue when trying to echo the value. Oddly enough, when I change this line $email = $_POST ...

Navigation menu with submenus containing buttons

I attempted to incorporate a dropdown into my existing navigation bar, but unfortunately, the dropdown content disappeared after adding the necessary code. I am now at a loss on how to troubleshoot this issue and make the dropdown function properly. Despit ...

Encountering an error: [nsIWebProgressListener::onStatusChange] when utilizing jQuery AJAX within a click event?

Greetings! I am currently learning how to implement AJAX with jQuery to load an HTML document into a div element within another HTML document. Here is the approach I am using: function pageload() { $.ajax({ url: 'Marker.aspx', ...

An unexpected 'undefined' value is being added to a particular Web API request

I am encountering an issue in my Angular application where the word 'Undefined' is being appended to a specific WebAPI call, causing both registerUser and login requests to fail. Here are the problematic request URLs: Request URL: http://localho ...

Is it possible to trigger an event in Laravel prior to the expiration of a user's login session?

To keep track of when a user logs out, I have set up an event that is triggered after clicking the logout button. The code below demonstrates how I store the logout time: <?php namespace App\Listeners; use App\UserTrack; use Carbon\Carb ...

Creating visually appealing tables in React.js

Having trouble with table rendering in React. The buttons in the table cell are not styled properly and do not center within their div. Additionally, the table border is cut off where there are buttons or empty headers. Need help figuring out what's g ...

Transfer numerical values from PHP to JavaScript using individual files

What is the best way to pass variables from PHP to JavaScript when they are in separate files? Is it possible to achieve this without using AJAX, or is AJAX necessary for this task? If AJAX is required, how can I implement it? test.php <?php $a = 5; $ ...

How can I utilize npm with the original source code instead of minified or bundled code?

I am looking to access npm and JavaScript (or TypeScript) 3rd party libraries directly from the source code. Similar to how I can make changes in Python libraries by going into their source code, I want to have the same capability with my JavaScript depen ...

Invoking a function passed via props that utilizes react-router's features

I'm really struggling to grasp this problem. Is there anyone here who could help me out? I have a component where I pass a method called this.fetchContent as props named Filter. This method triggers an action creator that uses axios with Redux to fetc ...