Switching between grid and list views using Bootstrap 4's Card layout feature

Looking to implement a grid/list view using bootstrap 4 cards.

<div class="well well-sm">
    <strong>Display:</strong>
    <div class="btn-group">
        <a href="#" id="list" class="btn btn-default btn-sm"><span class="fas fa-list-ul"></span>List</a> 
        <a href="#" id="grid" class="btn btn-default btn-sm"><span class="fas fa-th-large"></span>Grid</a>
    </div>
</div>

Jquery implementation:

$('#list').click(function(event){
event.preventDefault();
$('.card-deck').addClass('list-group-item');
$('.card-deck').removeClass('grid-group-item');
$(this).addClass('active');
$('#grid').removeClass('active');
});

$('#grid').click(function(event){
event.preventDefault();
$('.card-deck').removeClass('list-group-item');
$('#list').removeClass('active');
$(this).addClass('active');
$('.card-deck').addClass('grid-group-item');

});

Referenced code from another post: Bootstrap 4 card/panel with image left of header and title

<div class="card">
    <div class="row no-gutters">
        <div class="col-auto">
            <img src="//placehold.it/200" class="img-fluid" alt="">
        </div>
        <div class="col">
            <div class="card-block px-2">
                <h4 class="card-title">Title</h4>
                <p class="card-text">Description</p>
                <a href="#" class="btn btn-primary">BUTTON</a>
            </div>
        </div>
    </div>
    <div class="card-footer w-100 text-muted">

    </div>
</div>

Requirement is to transform the view into stacked card blocks when 'grid' is clicked:

<div class="card-deck>
    <div class="card" style="width: 18rem;">
        <img class="card-img-top" src="..." alt="Card image cap">
        <div class="card-body">
            <h5 class="card-title">Card title</h5>
            <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
        </div>
    </div>
</div>

Seeking guidance on achieving this functionality in bootstrap 4.

Thank you for your assistance.

Nabi

Answer №1

To start, I suggest assigning an id to each container such as 'grid-view' and 'list-view':

<div class="card" id="grid-view">
    <div class="row no-gutters">
        <div class="col-auto">
            <img src="//placehold.it/200" class="img-fluid" alt="">
        </div>
        <div class="col">
            <div class="card-block px-2">
                <h4 class="card-title">Title</h4>
                <p class="card-text">Description</p>
                <a href="#" class="btn btn-primary">BUTTON</a>
            </div>
        </div>
    </div>
    <div class="card-footer w-100 text-muted">

    </div>
</div>

In addition,

<div class="card-deck" id="list-view">
    <div class="card" style="width: 18rem;">
        <img class="card-img-top" src="..." alt="Card image cap">
        <div class="card-body">
            <h5 class="card-title">Card title</h5>
            <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
        </div>
    </div>
</div>

Next, you can utilize the hide() and show() functions from the Js file:

$('#list').click(function(event){
    ...
    $('#grid-view').hide();
    $('#list-view').show();
});

$('#grid').click(function(event){
   ...
    $('#grid-view').show();
    $('#list-view').hide();
});

Ensure that one view is hidden when the page loads initially.

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

Combining all CSS files into one and consolidating all JavaScript files into a single unified file

Whenever I need to add a new CSS or JS file, I always place it in the header section like this Add CSS files <link rel="stylesheet" href="<?php echo URL; ?>public/css/header.css" /> <link rel="stylesheet" href="<?php echo URL; ?> ...

How can I use jQuery mobile to request page content by simply clicking on a div?

I'm attempting to retrieve data from a callback page (php) and inject it into an html div using jQuery mobile when a user clicks on another div. Here's what I have: $.('#home-button').bind('vclick', function( e ) { $.get ...

Employing jQuery's offset() method for scripting

Issue: The problem I'm facing is that after clicking on the label where the date should be entered, the calendar is not appearing as expected. I tried using a jQuery script to darken the background, but then the calendar is not displaying in the corre ...

pass an array from the controller to a view by utilizing JSON within the MVC framework

I am encountering an issue with the following code: var mapcity = new Array([]); $.ajax({ type: 'GET', url: '/home/DrawMap', dataType: 'json', succe ...

What are some alternative ways to begin a photo album besides always using the first image

I have a gallery with 5 thumbnail images previewed, but there are more photos in the gallery. Instead of cluttering my code and hiding them with CSS, I am seeking an alternative solution. Below is my current HTML code: <div id="start_slides2"> ...

Move the div containing an <audio></audio> tag

Is it possible to drag a div with a width of 200px and an element into a droppable area, and once the div is dropped, have its size change according to the sound duration (e.g. 1px per second)? Check out this example on jsFiddle. Below is the code snipp ...

What could be causing this image to disregard the designated width value?

https://i.stack.imgur.com/bPH4t.png While working on my project with Next.js, I encountered an issue with the IconText component provided by Next.js. Even though I specified a width value for the image, it seems to be ignoring it. Below is the structure o ...

Need help with styling for disabled autocomplete? Check out the attached image

I'm currently working with material-ui and react to create a basic form. Everything is functioning properly except for a small UI issue that I can't seem to figure out how to resolve. When I utilize the browser's auto-complete feature, the i ...

Achieving Full Row Height in Twitter Bootstrap using CSS Grid

Before jumping to conclusions and marking this question as a duplicate, I want to clarify that I have already explored all suggestions provided on Stack Overflow, including the use of display: table. Unfortunately, none of them seem to work smoothly with B ...

The functionality for Excel, CSV, PDF, and Copy in Datatable Tools appears to be malfunctioning

After downloading DataTables-1.9.4 and attempting to run it, I encountered issues with the Excel, CSV, PDF, and Copy functionalities not working correctly. Only the print option seems to be functioning. Despite following all the steps mentioned in the do ...

What is the most effective method for transferring data from Python (specifically Flask) to JavaScript?

Greetings and a happy new year to all! I am facing a practical issue: In my GUI, I prompt the user to enter a new document name using the prompt method when they click on the "Save As New" button. $("#btnSaveNew").on( "click", function() { va ...

Choosing an element in JQuery based on the value of its style property

I have three divs with the same class but different styles. I need to select only the third one using JQuery. <div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-draggable" tabindex="-1" role="dialog" aria-labelledby="ui-dialog-title-div ...

What is the process for importing Gwt-Bootstrap?

I decided to incorporate Gwt-Bootstrap into my UiBinder files. I made sure to import the following: xmlns:b="urn:import:com.github.gwtbootstrap.client.ui"> in my UiBinder file and also added: <script src="http://html5shim.googlecode.c ...

``To position two text boxes to the right side of a container using flexbox, follow these steps

I need help aligning two input text boxes to the right in the same line within a container. Here is my code snippet: I am working with el-form, el-row, and el-col. .search__app { display: flex; margin-left:auto; font-size: 6px; border-radiu ...

Issue with loading the main.css file

Getting Started Managing two domains can be a challenge, especially when trying to make them appear as one seamless website. In this case, I have ownership of and . Goal My goal is to merge the content of https://mauricevandorst.com/personal-page/index ...

Javascript collapsible panel that is initially expanded

I found this code example here and decided to implement a collapsible panel using css/html/javascript: function toggleCollapsibleSectionWithAnimation() { this.classList.toggle("collapsible-active"); var buttonId = this.id; var sectionId = buttonId ...

What is the best way to eliminate the underline text decoration from a link element?

Take a look at this JS Bin. I want to change the link Project name in there. By default, it is underlined when hovered over. How can I remove that effect? I attempted using a class project-name: a:hover, a:focus .project-name { text-decoration: non ...

Retrieve the chosen item from the autocomplete feature

Unfortunately, I have encountered an issue with this solution as I was unable to get it to work correctly in my code. The objective is to retrieve the selected item and then call another function to utilize it. The script is as follows: <script type=" ...

Activate the dialog box exclusively after data has been submitted via the submit button on a form

My goal is to create a data saving functionality with a next button. After filling out the form, clicking on the next button triggers a popup dialog asking, "Do you want to submit your data?" To achieve this, I included the following code in my submit but ...

Interactive Radial Menu Using Raphael JS

Greetings and thank you for taking the time to consider my predicament. I am currently working on creating an SVG menu using raphael, but unfortunately, geometry has never been my strong suit. Below is a visual representation of what I have managed to cre ...