views:

31

answers:

2

Is there a quick/easy way to have tabular data and have a button that turns them to thumbnails (kind of a 2-view system, table/thumb)--I'm not looking for image thumbnails...

Any ideas are appreciated! :)

Examples:

alt text

alt text

A: 

I would use a client-side templating mechanism to accomplish this.

You would define two templates - one that would be the 'list' view and another that would be the 'thumbnail' view. The data merged with the two templates would be the same.

I would recommend using Resig's templating library (jQuery plugin) - http://github.com/jquery/jquery-tmpl

Very simple but will accomplish what you need.

Example:

Thumbnail template:

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

List template:

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

Where the data you would merge with might look like:

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 id: 'divContainer':

$("#divContainer").append( templateContents, arrData );
Adam
Would it be easier to just have two divs containing the separate layouts with PHP, and have a quasi-tab interface, thus eliminating the need for a plugin?
Kevin Brown
+1  A: 

If you'd like to render a standard table and then use jQuery to build the thumbnail view, then here's an example:

Rendered HTML:

<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:

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();
});

Actually you can shorten the jQuery code a bit by chaining some of the statements.

Here's a working example: http://jsfiddle.net/2PCnL/1/

Dave
That's nice--very slick and simple.
Kevin Brown