Designing tables and image galleries using HTML, CSS, and jQuery: A guide

How can I easily transform tabular data into thumbnails with the click of a button? I need a 2-view system that switches between table and thumbnail views without including image thumbnails.

I welcome any creative suggestions! :)

Examples:

Answer №1

If you want to display a standard table first and then use jQuery to create a thumbnail view, follow this example:

HTML Table Structure:

<a href="#" id="changeview">Change view</a>
<table id="theTable">
    <thead>
        <tr>
            <th>Name</th>
            <th>Description</th>
        <tr>
    </thead>
    <tbody>
        <tr>
            <td>Item 1</td>
            <td>Description 1</td>
        </tr>
        <tr>
            <td>Item 2</td>
            <td>Description 2</td>
        </tr>
        ...
    </tbody>
</table>​

JQuery Code:

var thumbnails = $('<ul></ul>').attr('id', 'theThumbnails').insertAfter('#theTable').hide();
$('#theTable tbody tr').each(function() {
    var cells = $(this).find('td');
    var thumbnail = $('<li></li>').addClass('thumbnail');
    $('<h3></h3>').text($(cells[0]).text()).appendTo(thumbnail);
    $('<p></p>').text($(cells[1]).text()).appendTo(thumbnail);
    thumbnail.appendTo(thumbnails);
});

$('#changeview').live('click', function() {
    $('#theTable, #theThumbnails').toggle();
});

You can actually make the jQuery code shorter by chaining some statements.

Check out this working demo: http://jsfiddle.net/2PCnL/1/

Answer №2

One way to achieve this would be using a client-side templating mechanism.

You can create two templates - one for the 'list' view and another for the 'thumbnail' view. The data merged with both templates would remain the same.

I suggest utilizing Resig's templating library, a jQuery plugin available at http://github.com/jquery/jquery-tmpl

It is straightforward but effective in meeting your requirements.

For instance:

Thumbnail template:

{{each}}
  <div style="float: left;">
    <img src="${{thumbnailUrl}}" alt="${{description}}" />
  </div>
{{/each}}

List template:

<ul>
  {{each}}
    <li>${{description}}</li>
  {{/each}}
</ul>

The data to merge could be formatted as follows:

arrData = 
[ { thumnailUrl: "/image.gif", description: "Some image" },
  { thumbnailUrl: "another.gif", description: "Another image" }
]

To apply the template to a container div on your page with the ID: 'divContainer':

$("#divContainer").append( templateContents, arrData );

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

How to position one div next to another using CSS and HTML

Hey there, I'm facing an issue with creating a sidenav next to a div containing paragraphs and images. I can't seem to make both the sidenav and the other div appear inline. If anyone has any insights on how to solve this problem or spot what I&a ...

Managing the height of a hero section with TailwindCSS and React.js

Embarking on my first website project using React and Tailwind has been an exciting learning experience. My vision is to showcase a Hero Section prominently at the top, with a Nav bar situated elegantly at the bottom of the screen. To bring this concept ...

Access exclusive RSS feeds from Twitter

I am facing a challenge in showcasing the most recent 3 tweets from Twitter on my client's website. The tweets are marked as private, making it difficult to access them. How can I pass the username and password to retrieve these latest tweets? Is it p ...

Unable to determine why node.js express path is not working

const express = require("express"); const app = express(); app.use(express.static("public")); var dirname = __dirname; app.get("/:lang/:app",function(req,res){ console.log(req.params.lang + " " + req.params.app); ...

Representation of a family tree in CSS showcasing multiple sub-parents (both many to one and one to many relationships)

I've come across various family tree presentations. I'm curious if it's possible to display a one-to-many and then many-to-one structure? Currently, the flow is linear stop after stop, but I'd like to incorporate multiple steps that con ...

Transfer a data element to a PHP webpage and launch the said webpage in a fresh browser tab

Here's an example of some code I'm working with: $(document).on('click', '#button', function(){ var my_info = 'Example Example Exam'; $.ajax({ type: "POST", url: "view.php", data: "my_info ...

How come @keyframes animations do not seem to cooperate with Vue.js?

I'm attempting to create a flashing link upon site load, but I'm encountering issues with the animation not working in Vue. Interestingly, when testing the same code without Vue in a separate file, it functions perfectly fine. Below is the spec ...

The functionality of Bootstrap Tabs is compromised when used within a jQuery-UI dialog window

My goal is to develop a user interface similar to MDI for my application. To achieve this, I am utilizing the dialog feature of the jQuery UI library. To dynamically create a dialog window on demand, I have coded a helper function as shown below: functio ...

Initially, the 'display none' CSS command may not take effect the first time it is used

I am currently working on a slideshow application using jquery.transit. My goal is to hide a photo after its display animation by setting the properties of display, transform, and translate to 'none' value. Although the slideshow application work ...

Issues with FullCalendar functionality in CakePHP 1.2.5 are arising when using jQuery version 1.4.1

Currently, I am encountering an issue with fetching events data through a URL that returns JSON data. Surprisingly, the code works flawlessly with jQuery 1.3.2, however, it throws errors when using jQuery 1.4.1. The specific error message displayed in the ...

What is the best way to recreate this grid-like design using CSS?

By utilizing clever techniques such as margins and outlines, I was able to design the following. If you'd like to take a look at the live website, it can be found at The CSS code that I used is as follows: body { margin: auto; max-width: 736px } s ...

Frame Tweet Integration

Attempting to create an iframe modal with a Twitter share source. <iframe src="https://twitter.com/share" class="tweetFrame"></iframe> The current setup is not functioning properly, resulting in a blank page. Is there a workaround to ensure t ...

Stubborn boolean logic that refuses to work together

Seeking guidance on resolving a persistent issue with my website that has been causing me headaches for the past few weeks. This website was created as my capstone project during a recent graduate program, where I unfortunately did not achieve all the desi ...

Reduce the length of the text and display it upon hovering with the mouse

I am trying to figure out how to truncate text and have it expand on mouseover in a paragraph element. I attempted to truncate the content using a specific code, but I encountered an issue. While my current code expands the content when clicking on the el ...

Ajax mode in Mvcpaging is malfunctioning

Having some trouble with getting MvcPaging to work in Ajax mode. It seems that it's not behaving as expected - instead of making an Ajax call, it's doing a full GET request. When checking the controller code, I noticed that Request.IsAjaxRequest( ...

Choosing a specific page number - kendo grid

Currently, I am working on creating a grid using kendo-telrik. Let's say I have 100 data items, but in the JSON format, I only have 10 data items (assuming each page contains 10 data items for pagination). However, I want to display all the pages (10 ...

"Successfully implementing AJAX POST functionality, but encountering issues where callbacks are not triggering

I have gone through numerous posts addressing this issue and attempted various solutions, however, none seem to work for me. My AJAX POST function correctly adds the email to my mailing list, but the success and error callbacks are not consistently firing, ...

What is the method for retrieving the name of a canceled file?

When a user clicks on the "Cancel" button during an upload, I am attempting to retrieve the filename of the file that was canceled. However, it seems like the current method I am using (var image_file_name) is not successfully retrieving the name of the ca ...

Using Python Flask: Passing a Python list of data elements to Jquery's "Lists": [{ [] }]

Seeking assistance in passing a Python List Result to a Jquery List. Can anyone provide the necessary code to integrate jinja templates into Jquery for iterating through the List values? Python Flask Code:- @app.route('/mapcolumns_link',methods ...

Unable to populate a calendar with JSON events for the entire schedule

I have been experimenting with the following code for integrating fullcalendar:- <doctype! html> <html lang="urf-8"> <head> <title>Full calendar</title> <link rel="stylesheet" href="fullcalendar.css"/> ...