What is the best way to incorporate a toggleable grid or table view into my project?

Recently, I've set up a grid view for some content using an unordered list to create a visually appealing block layout. Now, I've been given the task of presenting the same data in a table format. The data also involves various actions that can be performed on it. While the visual switch is intended for shorter lists, there could be longer lists as well.

I'm considering reusing the existing structure of the unordered list and enhancing it with additional elements to simulate a table view, which doesn't seem too difficult. Another option is to redo everything in a table format and then use CSS to implement the grid view.

My main query is regarding the approaches to tackle this challenge. One thought is to duplicate my markup - implementing both views and toggling between them. However, in an AJAX-intensive application, this would mean performing double the DOM manipulations to incorporate these changes.

Is there a recommended practice for seamlessly switching between views like this? I'm eager to hear about various ideas and practical examples from those who have worked on similar projects.


To illustrate further, I've included a jsFiddle demonstrating the use of an unordered list as the foundation. Can anyone provide sample CSS code that utilizes a table as the core structure?

Answer №1

Unfortunately, no one opted for a table-centric approach, so here is the code for a ul>li centric approach:

HTML:

<div class="controls">
    <a href="#" class="list active">List</a>
    <a href="#" class="grid">Grid</a>
</div>

<div id="wrapper">
<ul class="list">
    <li class="header"><h2 class="name">Name</h2>
        <p class="date">Start Date</p>
        <p class="title">Title</p>
        <div class="image">Image</div></li>
    <li class="even"><h2 class="name">Adam Ant</h2>
        <p class="date">1995</p>
        <p class="title">Specialist</p>
        <div class="image"><img src="http://www.placehold.it/200x200" alt=""></div></li>
    <li class="odd"><h2 class="name">Brian Box</h2>
        <p class="date">2005</p>
        <p class="title">Specialist</p>
        <div class="image"><img src="http://www.placehold.it/200x200" alt=""></div></li>
    <li class="even"><h2 class="name">Clara Clock</h2>
        <p class="date">2010</p>
        <p class="title">Manager</p>
        <div class="image"><img src="http://www.placehold.it/200x200" alt=""></div></li>
    <li class="odd"><h2 class="name">Darla Dock</h2>
        <p class="date">1996</p>
        <p class="title">Editor</p>
        <div class="image"><img src="http://www.placehold.it/200x200" alt=""></div></li>
</ul>
</div>​

CSS:

body {
    font-family: sans-serif;
}
.controls {
    text-align: right;
    width: 500px;
}
.controls a {
    background-color: #000;
    color: #fff;
    display: inline-block;
    padding: 6px;
    text-decoration: none;
}
.controls a.active {
    background-color: blue;
}

#wrapper .list li { 
    width: 500px; 
    position: relative;
    height: 30px;
}
#wrapper .list li.odd * {
    background-color: #eee;
}
#wrapper .list li.header {
    background-color: #666;
    color: #fff;
}
#wrapper .list li * {
    position: absolute;
    line-height: 1;
    top: 0;
    display: block;
    height: 30px;
}
#wrapper .list li h2.name {
    left: 0;
    width: 150px;

}
#wrapper .list li p.date {
    left: 150px;
    width: 100px;
}
#wrapper .list li p.title {
    left: 250px;
    width: 100px;
}
#wrapper .list li div.image {
    left: 350px;
    width: 150px;
}
#wrapper .list li div.image img {
    height: 20px;
    width: 20px
    margin: 0 auto;
}

#wrapper .grid li { 
    display: inline-block;
    width: 200px; 
    height: 200px;
    position: relative;
    overflow: hidden;
}
#wrapper .grid li.header {
    display: none;
}
#wrapper .grid h2.name {
    background-color: rgba(0,0,0,0.5);
    color: #fff;
    position: absolute;
    left: 0;
    top: 0;
    width: 200px;
    line-height: 2;
    height: 30px;
}
#wrapper .grid p.date {
    display: none;
}
#wrapper .grid p.title {
    display: none;
}

JavaScript (using MooTools):

$$('a').addEvent('click', function(){
    $$('.controls a').toggleClass('active');
    $$('ul').toggleClass('list').toggleClass('grid');
    return false;
})

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

Tips for preventing countdown from resetting when you refresh the page

I'm currently using a countdown feature on my Shopify website that is functioning well, except for one issue - whenever the page is refreshed, the countdown resets itself. Can anyone provide guidance on how to solve this problem? Thank you in advance! ...

Modifying the color of drawings on a Javascript canvas

I am currently working on developing a drawing board using HTML and JavaScript (Node.js on the server side). One challenge I'm facing is implementing a color picker that allows users to change the paint color dynamically. While I could hard code the c ...

Here's a unique rewrite: "Learn how to manipulate the CSS class of a textarea element (specifically in NicEdit) by utilizing the addClass

I am currently validating a textarea using the NicEdit plugin. var getContent = nicEditors.findEditor("SubSliderDescription").getContent(); bValid = bValid && checkBlankTextArea(getContent, "SubSliderDescription") function checkBlankTextArea(o, ...

How can I create space in between each column underline using css?

I'm struggling to add space between the columns of my table, similar to having a gap between each underline. I've looked at some posts on this website, but haven't been able to solve it. Here's the code I've attempted: .rbcrosstab ...

Align a flex item at the center of its parent when the parent has a flex direction of column

My challenge is aligning a flex item vertically within the remaining space allocated to it. This is my current code snippet: <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82e0e ...

Constructing an interactive table from a JSON dataset using Node.js and Express

Just starting out with Node.JS here! I'm currently working on creating a page with multiple tables and information using NodeJS. However, I've hit a roadblock when trying to display the result of an SQL query in an HTML table. Right now, I'm ...

Substitute the outdated body element with the freshly loaded element

Using ajax with .load() to load page contents. The formatting of my HTML pages is as follows: For the home page: <html> <head></head> <body class="HOME"> <header></header> <div id="content"> // ...

Discovering keywords within HTML code and concealing particular content through jQuery

My HTML code snippet is shown below: <div id='p1'> <p>First Paragraph</p> <p>Abbot and Costello - Africa Screams</p> </div> <div id='p2'> <p>Second Paragraph</p> <p>Abbot and ...

Step-by-step guide on displaying a window containing text when hovering over a CSS class

As a beginner in css, html, and js, I created an html page with a table of athletes. Each row is identified by the css class "athlete-name", like this: <div id="message-hide" style="display: none"> Hello world! </div> <t ...

Having trouble making the menu stay at the top of the page in IE7

Check out the demo here: http://jsfiddle.net/auMd5/ I'm looking to have the blue menu bar stay fixed to the top of the page as you scroll past it, and then return to its original position when scrolling back up. This functionality works in all brows ...

What is the best way to loop through each word within an element and selectively enclose specific matches?

Currently, I am writing content for my personal blog using ASP.NET, and I have integrated the Gist embed plugin for displaying code snippets. I am interested in customizing the code colors to resemble how code looks in Xcode when coding in Swift. Initiall ...

What steps can be taken to identify the script responsible for preventing text highlighting or right clicking on a webpage?

Whenever I visit aliexpress using Chrome, I encounter issues where I am unable to highlight text on an item's page (although it works fine on the search results list) and cannot right-click to access the context menu on images. Both of these functions ...

Concealing a CSS class with PHP within the Wordpress Woocommerce platform

I have very limited knowledge in programming and need help with a code snippet. I am using the Wordpress Snippets Plugin to hide the Add Cart Button from a single product. add_action( 'wp', 'rudr_remove_add_to_cart_single_product' ); fu ...

How can we minimize the data contained in JSON HTML markup?

https://i.stack.imgur.com/mzuB0.png Currently, I am attempting to find a way to conceal the last 12 digits in the price shown on a button, as it is excessively long. The method I am utilizing involves a JSON api and insertAdjacentHTML markup. This snipp ...

Is it possible to set the radius of a d3.js circle using a style attribute?

Learn more about styling in d3.js with this detailed guide that explains how style attributes can be leveraged to customize the look of a circle, including fill color, stroke color, and stroke width. Is it possible to adjust the radius of a d3.js circle u ...

Loop through every list item within each unordered list using JQuery

I am currently using a loop to iterate through each li element and search for specific content within it. If the content matches certain criteria, I add a class to that li element. However, the issue I am facing is that there are multiple UL elements in th ...

Input field in a tableview

I have a ListView that features a dropdown, 4 textboxes and buttons. My goal is to only show the fourth textbox (enclosed in a span) when the dropdown value in each row of the ListView is set to 2. For instance, if there are 5 records being displayed in t ...

PHP: Invoking a function within the HTML content of a variable

I need help with adding a PHP function to a variable storing HTML code for a form. This is what the current setup looks like: <?php function example_function() { // code } $form = ' <form action" <!-- etc. --> > <input <!-- etc ...

Why does a relatively positioned element always begin at the top corner of the window?

I am really struggling to understand the concept of relative versus absolute positioning, especially when dealing with parent or grandparent elements that have position values. Can someone please help me clarify these two scenarios: Case 1: grandparent ...

textarea element enclosed by a div tag

I'm looking to add two side-by-side boxes on my page for comments. One box will display previous comments and should be disabled, while the other allows current users to leave comments. I achieved this by using two separate divs positioned next to eac ...