Skip to content

Instantly share code, notes, and snippets.

@jackiewu
Created November 18, 2015 09:02
Show Gist options
  • Save jackiewu/5b6e1beacb27623594b8 to your computer and use it in GitHub Desktop.
Save jackiewu/5b6e1beacb27623594b8 to your computer and use it in GitHub Desktop.
RWD Table Patterns
<div class="docs">
<h3>Selectively displaying data</h3>
<p>The number of columns displayed in the table below depends on the available screen space, by default; a smartphone will display 2 columns, for example, while an expanded desktop browser displays the full set. This is accomplished by assigning semantic classes to the column headings that indicate which data values take precedence (essential vs optional), in combination with media queries to display them at different screen widths (a.k.a., <a href="http://www.alistapart.com/articles/responsive-web-design/">responsive design</a>).</p>
<p>We added a bit of JavaScript logic so you can control which data is displayed by checking column names in the "Display" menu on the right. Once an option is checked, the associated data will persist and display at all screen widths until the option is unchecked.</p>
<p>You can also set a column to always persist by assigning a class in the markup, in which case it has no menu option. Here, the "Company" column is persistent.</p>
</div>
<div class="table-wrapper">
<table cellspacing="0" id="tech-companies">
<thead>
<tr>
<th class="persist essential">Company</th>
<th class="essential">Last Trade</th>
<th class="optional">Trade Time</th>
<th class="essential">Change</th>
<th class="optional">Prev Close</th>
<th class="optional">Open</th>
<th>Bid</th>
<th>Ask</th>
<th>1y Target Est</th>
</tr>
</thead>
<tbody>
<tr>
<th>GOOG <span class="co-name">Google Inc.</span></th>
<td>597.74</td>
<td>12:12PM</td>
<td>14.81 (2.54%)</td>
<td>582.93</td>
<td>597.95</td>
<td>597.73 x 100</td>
<td>597.91 x 300</td>
<td>731.10</td>
</tr>
<tr>
<th>AAPL <span class="co-name">Apple Inc.</span></th>
<td>378.94</td>
<td>12:22PM</td>
<td>5.74 (1.54%)</td>
<td>373.20</td>
<td>381.02</td>
<td>378.92 x 300</td>
<td>378.99 x 100</td>
<td>505.94</td>
</tr>
<tr>
<th>AMZN <span class="co-name">Amazon.com Inc.</span></th>
<td>191.55</td>
<td>12:23PM</td>
<td>3.16 (1.68%)</td>
<td>188.39</td>
<td>194.99</td>
<td>191.52 x 300</td>
<td>191.58 x 100</td>
<td>240.32</td>
</tr>
<tr>
<th>ORCL <span class="co-name">Oracle Corporation</span></th>
<td>31.15</td>
<td>12:44PM</td>
<td>1.41 (4.72%)</td>
<td>29.74</td>
<td>30.67</td>
<td>31.14 x 6500</td>
<td>31.15 x 3200</td>
<td>36.11</td>
</tr>
<tr>
<th>MSFT <span class="co-name">Microsoft Corporation</span></th>
<td>25.50</td>
<td>12:27PM</td>
<td>0.66 (2.67%)</td>
<td>24.84</td>
<td>25.37</td>
<td>25.50 x 71100</td>
<td>25.51 x 17800</td>
<td>31.50</td>
</tr>
<tr>
<th>CSCO <span class="co-name">Cisco Systems, Inc.</span></th>
<td>18.65</td>
<td>12:45PM</td>
<td>0.97 (5.49%)</td>
<td>17.68</td>
<td>18.23</td>
<td>18.65 x 10300</td>
<td>18.66 x 24000</td>
<td>21.12</td>
</tr>
<tr>
<th>YHOO <span class="co-name">Yahoo! Inc.</span></th>
<td>15.81</td>
<td>12:25PM</td>
<td>0.11 (0.67%)</td>
<td>15.70</td>
<td>15.94</td>
<td>15.79 x 6100</td>
<td>15.80 x 17000</td>
<td>18.16</td>
</tr>
</tbody>
</table>
</div>
/* Scripts for the tables test page
Author: Maggie Wachs, www.filamentgroup.com
Date: November 2011
Dependencies: jQuery, jQuery UI widget factory
*/
(function( $ ) {
$.widget( "filament.table", { // need to come up with a better namespace var...
options: {
idprefix: null, // specify a prefix for the id/headers values
persist: null, // specify a class assigned to column headers (th) that should always be present; the script not create a checkbox for these columns
checkContainer: null // container element where the hide/show checkboxes will be inserted; if none specified, the script creates a menu
},
// Set up the widget
_create: function() {
var self = this,
o = self.options,
table = self.element,
thead = table.find("thead"),
tbody = table.find("tbody"),
hdrCols = thead.find("th"),
bodyRows = tbody.find("tr"),
container = o.checkContainer ? $(o.checkContainer) : $('<div class="table-menu table-menu-hidden"><ul /></div>');
// add class for scoping styles - cells should be hidden only when JS is on
table.addClass("enhanced");
hdrCols.each(function(i){
var th = $(this),
id = th.attr("id"),
classes = th.attr("class");
// assign an id to each header, if none is in the markup
if (!id) {
id = ( o.idprefix ? o.idprefix : "col-" ) + i;
th.attr("id", id);
};
// assign matching "headers" attributes to the associated cells
// TEMP - needs to be edited to accommodate colspans
bodyRows.each(function(){
var cell = $(this).find("th, td").eq(i);
cell.attr("headers", id);
if (classes) { cell.addClass(classes); };
});
// create the hide/show toggles
if ( !th.is("." + o.persist) ) {
var toggle = $('<li><input type="checkbox" name="toggle-cols" id="toggle-col-'+i+'" value="'+id+'" /> <label for="toggle-col-'+i+'">'+th.text()+'</label></li>');
container.find("ul").append(toggle);
toggle.find("input")
.change(function(){
var input = $(this),
val = input.val(),
cols = $("#" + val + ", [headers="+ val +"]");
if (input.is(":checked")) { cols.show(); }
else { cols.hide(); };
})
.bind("updateCheck", function(){
if ( th.css("display") == "table-cell" || th.css("display") == "inline" ) {
$(this).attr("checked", true);
}
else {
$(this).attr("checked", false);
}
})
.trigger("updateCheck");
};
}); // end hdrCols loop
// update the inputs' checked status
$(window).bind("orientationchange resize", function(){
container.find("input").trigger("updateCheck");
});
// if no container specified for the checkboxes, create a "Display" menu
if (!o.checkContainer) {
var menuWrapper = $('<div class="table-menu-wrapper" />'),
menuBtn = $('<a href="#" class="table-menu-btn">Display</a>');
menuBtn.click(function(){
container.toggleClass("table-menu-hidden");
return false;
});
menuWrapper.append(menuBtn).append(container);
table.before(menuWrapper);
// assign click-to-close event
$(document).click(function(e){
if ( !$(e.target).is( container ) && !$(e.target).is( container.find("*") ) ) {
container.addClass("table-menu-hidden");
}
});
};
}, // end _create
disable: function() {
// TBD
},
enable: function() {
// TBD
}
});
}( jQuery ) );
$(function(){ // on DOM ready
$("#tech-companies").table({
idprefix: "co-",
persist: "persist"
});
}); // end DOM ready
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
/* Styles for the tables test page */
/* Test page styles */
.docs {
margin: 3% 5%;
font-size: 1.2em;
}
.docs h3 {
font-size: 1.2em;
font-weight: bold;
margin: .5em 0;
}
.docs p {
margin: 0 0 1em;
}
body {
font: 62.5%/1.3 Helvetica, sans-serif;
}
.a11y-only {
position: absolute;
left: -999em;
}
.table-wrapper {
position: relative;
margin: 5em 5%;
}
.table-menu-wrapper {
position: absolute;
top: -3em;
right: 0;
}
.table-menu {
position: absolute;
background-color: #fff;
padding: 10px;
border: 1px solid #ccc;
font-size: 1.2em;
width: 12em;
right: 0;
left: auto;
}
.table-menu-hidden {
left: -999em;
right: auto;
}
.table-menu-btn {
text-decoration: none;
color: #333;
font-size: 1.2em;
background: #eee url(../_img/icon-menu.png) no-repeat 5px center;
padding: .3em 10px .3em 20px;
border: 1px solid #ccc;
}
.table-menu li {
padding: .3em 0;
}
/* Table styles */
table {
width: 100%;
font-size: 1.2em;
}
thead th {
white-space: nowrap;
border-bottom: 1px solid #ccc;
color: #888;
}
th, td {
padding: .5em 1em;
background-color: #fff;
text-align: right;
}
th:first-child,
td:first-child {
text-align: left;
}
tbody th, td {
border-bottom: 1px solid #e6e6e6;
}
.co-name {
display: block;
font-size: .9em;
opacity: .4;
}
.enhanced th,
.enhanced td {
display: none;
}
.legacy-ie .enhanced th.essential,
.legacy-ie .enhanced td.essential {
display: inline;
}
.enhanced th.essential,
.enhanced td.essential {
display: table-cell;
}
@media screen and (min-width: 500px) {
.legacy-ie .enhanced th.optional,
.legacy-ie .enhanced td.optional {
display: inline;
}
.enhanced th.optional,
.enhanced td.optional {
display: table-cell;
}
}
@media screen and (min-width: 800px) {
.legacy-ie .enhanced th,
.legacy-ie .enhanced td {
display: inline;
}
.enhanced th,
.enhanced td {
display: table-cell;
}
}
<link href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment