lib-jquery-learning-bookstore-scripts-tables.js / js
// Example: Row Grouping Tags // document.ready(function() { // $('table.sortable tbody tr:odd').addClass('odd'); // $('table.sortable tbody tr:even').addClass('even'); // }); // Example: Basic Alphabetical Sorting // Step 1 // document.ready(function() { // $('table.sortable').each(function() { // var // $('th', table).each(function(column) { // var // if (header.is('.sort-alpha')) { // // header.addClass('hover'); // }, function() { // // }).click(function() { // var rows = table.find('tbody > tr').get(); // rows.sort(function(a, b) { // var keyA = a.children('td').eq(column).text() // .toUpperCase(); // var keyB = b.children('td').eq(column).text() // .toUpperCase(); // if (keyA < keyB) return -1; // if (keyA > keyB) return 1; // return 0; // }); // // table.children('tbody').append(row); // }); // }); // } // }); // }); // }); // Step 2 // document.ready(function() { // var alternateRowColors = function( // $('tbody tr:odd', table) // .removeClass('even').addClass('odd'); // $('tbody tr:even', // .removeClass('odd').addClass('even'); // }; // // $('table.sortable').each(function() { // var table = this; // alternateRowColors( // $('th', table).each(function(column) { // var // if (header.is('.sort-alpha')) { // // header.addClass('hover'); // }, function() { // // }).click(function() { // var rows = table.find('tbody > tr').get(); // rows.sort(function(a, b) { // var keyA = a.children('td').eq(column).text() // .toUpperCase(); // var keyB = b.children('td').eq(column).text() // .toUpperCase(); // if (keyA < keyB) return -1; // if (keyA > keyB) return 1; // return 0; // }); // // table.children('tbody').append(row); // }); // alternateRowColors( // }); // } // }); // }); // }); // Example: Refactoring the row striping code as a plug-in method // jQuery.fn.alternateRowColors = function() { // $('tbody tr:odd', this) // .removeClass('even').addClass('odd'); // $('tbody tr:even', this) // .removeClass('odd').addClass('even'); // return this; // }; // // document.ready(function() { // $('table.sortable').each(function() { // var table = this; // // $('th', table).each(function(column) { // var // if (header.is('.sort-alpha')) { // // header.addClass('hover'); // }, function() { // // }).click(function() { // var rows = table.find('tbody > tr').get(); // rows.sort(function(a, b) { // var keyA = a.children('td').eq(column).text() // .toUpperCase(); // var keyB = b.children('td').eq(column).text() // .toUpperCase(); // if (keyA < keyB) return -1; // if (keyA > keyB) return 1; // return 0; // }); // // table.children('tbody').append(row); // }); // // }); // } // }); // }); // }); // Example: Improving sort performance using expando properties // jQuery.fn.alternateRowColors = function() { // $('tbody tr:odd', this) // .removeClass('even').addClass('odd'); // $('tbody tr:even', this) // .removeClass('odd').addClass('even'); // return this; // }; // // document.ready(function() { // $('table.sortable').each(function() { // var table = this; // // $('th', table).each(function(column) { // var // if (header.is('.sort-alpha')) { // // header.addClass('hover'); // }, function() { // // }).click(function() { // var rows = table.find('tbody > tr').get(); // // row.sortKey = row.children('td').eq(column) // .text().toUpperCase(); // }); // rows.sort(function(a, b) { // if (a.sortKey < b.sortKey) return -1; // if (a.sortKey > b.sortKey) return 1; // return 0; // }); // .each(rows, function(index, row) { // // row.sortKey = null; // }); // table.alternateRowColors(); // }); // } // }); // }); // }); // Example: Explicitly marking sort keys // jQuery.fn.alternateRowColors = function() { // $('tbody tr:odd', this) // .removeClass('even').addClass('odd'); // $('tbody tr:even', this) // .removeClass('odd').addClass('even'); // return this; // }; // // document.ready(function() { // $('table.sortable').each(function() { // var // table.alternateRowColors(); // $('th', // var header = this; // if ( // header.addClass('clickable').hover(function() { // // }, function() { // header.removeClass('hover'); // }).click(function() { // var rows = // .each(rows, function(index, row) { // var // row.sortKey = cell.find('.sort-key').text() // .toUpperCase() + ' ' // + // }); // rows.sort(function(a, b) { // if (a.sortKey < b.sortKey) return -1; // if (a.sortKey > b.sortKey) return 1; // return 0; // }); // .each(rows, function(index, row) { // // row.sortKey = null; // }); // table.alternateRowColors(); // }); // } // }); // }); // }); // Example: Sorting by other types of data // jQuery.fn.alternateRowColors = function() { // $('tbody tr:odd', this) // .removeClass('even').addClass('odd'); // $('tbody tr:even', this) // .removeClass('odd').addClass('even'); // return this; // }; // // document.ready(function() { // $('table.sortable').each(function() { // var // table.alternateRowColors(); // $('th', // var header = this; // var findSortKey; // if ( // findSortKey = function(cell) { // return // + ' ' + cell.text().toUpperCase(); // }; // } // else if ( // findSortKey = function(cell) { // var key = , ''); // key = parseFloat(key); // return isNaN(key) ? 0 : key; // }; // } // else if (header.is('.sort-date')) { // findSortKey = function( // return Date.parse('1 ' + cell.text()); // }; // } // // if (findSortKey) { // // header.addClass('hover'); // }, function() { // // }).click(function() { // var rows = table.find('tbody > tr').get(); // // var cell = row.children('td').eq(column); // row.sortKey = findSortKey( // }); // rows.sort(function(a, b) { // if (a.sortKey < b.sortKey) return -1; // if (a.sortKey > b.sortKey) return 1; // return 0; // }); // .each(rows, function(index, row) { // // row.sortKey = null; // }); // table.alternateRowColors(); // }); // } // }); // }); // }); // Example: Column highlighting // jQuery.fn.alternateRowColors = function() { // $('tbody tr:odd', this) // .removeClass('even').addClass('odd'); // $('tbody tr:even', this) // .removeClass('odd').addClass('even'); // return this; // }; // // document.ready(function() { // $('table.sortable').each(function() { // var // table.alternateRowColors(); // $('th', // var header = this; // var findSortKey; // if ( // findSortKey = function(cell) { // return // + ' ' + cell.text().toUpperCase(); // }; // } // else if ( // findSortKey = function(cell) { // var key = , ''); // key = parseFloat(key); // return isNaN(key) ? 0 : key; // }; // } // else if (header.is('.sort-date')) { // findSortKey = function( // return Date.parse('1 ' + cell.text()); // }; // } // // if (findSortKey) { // // header.addClass('hover'); // }, function() { // // }).click(function() { // var rows = table.find('tbody > tr').get(); // // var cell = row.children('td').eq(column); // row.sortKey = findSortKey( // }); // rows.sort(function(a, b) { // if (a.sortKey < b.sortKey) return -1; // if (a.sortKey > b.sortKey) return 1; // return 0; // }); // .each(rows, function(index, row) { // // row.sortKey = null; // }); // table.find('td').removeClass('sorted') // .filter(':nth-child(' + (column + 1) + ')') // .addClass('sorted'); // // }); // } // }); // }); // }); // Example: Alternating sort directions // jQuery.fn.alternateRowColors = function() { // $('tbody tr:odd', this) // .removeClass('even').addClass('odd'); // $('tbody tr:even', this) // .removeClass('odd').addClass('even'); // return this; // }; // // document.ready(function() { // $('table.sortable').each(function() { // var table = this; // // $('th', table).each(function(column) { // var // var findSortKey; // if (header.is('.sort-alpha')) { // findSortKey = function( // return cell.find('.sort-key').text().toUpperCase() // + ' ' + // }; // } // else if (header.is('.sort-numeric')) { // findSortKey = function( // var key = cell.text().replace(/^[^\d.]*/, ''); // key = parseFloat(key); // return isNaN(key) ? 0 : key; // }; // } // else if ( // findSortKey = function(cell) { // return Date.parse('1 ' + // }; // } // // if (findSortKey) { // header.addClass('clickable').hover(function() { // // }, function() { // header.removeClass('hover'); // }).click(function() { // var sortDirection = 1; // if ( // sortDirection = -1; // } // var rows = table.find('tbody > tr').get(); // // var cell = row.children('td').eq(column); // row.sortKey = findSortKey( // }); // rows.sort(function(a, b) { // if (a.sortKey < b.sortKey) return -sortDirection; // if (a.sortKey > b.sortKey) return sortDirection; // return 0; // }); // .each(rows, function(index, row) { // // row.sortKey = null; // }); // table.find('th').removeClass('sorted-asc') // .removeClass('sorted-desc'); // if (sortDirection == 1) { // // } // else { // header.addClass('sorted-desc'); // } // // .filter(':nth-child(' + (column + 1) + ')') // .addClass('sorted'); // table.alternateRowColors(); // }); // } // }); // }); // }); // Example: JavaScript pagination // Step 1 // document.ready(function() { // $('table.paginated').each(function() { // var currentPage = 0; // var numPerPage = 10; // var // // table.find('tbody tr').hide() // .slice(currentPage * numPerPage, // (currentPage + 1) * numPerPage) // .show(); // }); // }); // Step 2 // document.ready(function() { // $('table.paginated').each(function() { // var currentPage = 0; // var numPerPage = 10; // var // // var numRows = table.find('tbody tr').length; // var numPages = Math.ceil(numRows / numPerPage); // // var <div class="pager"></div>'); // for (var page = 0; page < numPages; page++) { // $('<span class="page-number">' + (page + 1) + '</span>') // .appendTo(pager).addClass('clickable'); // } // table); // // // .slice(currentPage * numPerPage, // (currentPage + 1) * numPerPage) // .show(); // }); // }); // Step 3 // document.ready(function() { // $('table.paginated').each(function() { // var currentPage = 0; // var numPerPage = 10; // var table = this; // var repaginate = function() { // // .slice(currentPage * numPerPage, // (currentPage + 1) * numPerPage) // .show(); // }; // var numRows = table.find('tbody tr').length; // var numPages = Math.ceil(numRows / numPerPage); // var <div class="pager"></div>'); // for (var page = 0; page < numPages; page++) { // $('<span class="page-number"></span>').text(page + 1) // .click(function() { // currentPage = page; // repaginate(); // }).appendTo(pager).addClass('clickable'); // } // table); // }); // }); // Step 4 // document.ready(function() { // $('table.paginated').each(function() { // var currentPage = 0; // var numPerPage = 10; // var // var repaginate = function() { // table.find('tbody tr').hide() // .slice(currentPage * numPerPage, // (currentPage + 1) * numPerPage) // .show(); // }; // var numRows = // var numPages = Math.ceil(numRows / numPerPage); // var pager = $('<div class="pager"></div>'); // for (var page = 0; page < numPages; page++) { // $('<span class="page-number"></span>').text(page + 1) // .bind('click', {newPage: page}, function(event) { // currentPage = event.data['newPage']; // repaginate(); // }).appendTo( // } // pager.insertBefore( // }); // }); // Step 5 // document.ready(function() { // $('table.paginated').each(function() { // var currentPage = 0; // var numPerPage = 10; // var table = this; // var repaginate = function() { // // .slice(currentPage * numPerPage, // (currentPage + 1) * numPerPage) // .show(); // }; // var numRows = table.find('tbody tr').length; // var numPages = Math.ceil(numRows / numPerPage); // var <div class="pager"></div>'); // for (var page = 0; page < numPages; page++) { // $('<span class="page-number"></span>').text(page + 1) // .bind('click', {newPage: page}, function(event) { // currentPage = event.data['newPage']; // repaginate(); // this.addClass('active') // .siblings().removeClass('active'); // }).appendTo(pager).addClass('clickable'); // } // table) // .find('span.page-number:first').addClass('active'); // }); // }); // Finished code: Sorting and Paging jQuery.fn.alternateRowColors = function() { $('tbody tr:odd', this) .removeClass('even').addClass('odd'); $('tbody tr:even', this) .removeClass('odd').addClass('even'); return this; }; document.ready(function() { $('table.sortable').each(function() { var table.alternateRowColors(); $('th', header = this; var findSortKey; if (cell) { return cell.text().toUpperCase(); }; } else if (cell) { var key = }; } else if (header.is('.sort-date')) { findSortKey = function(cell.text()); }; } if (findSortKey) { header.addClass('hover'); }, function() { header.is('.sorted-asc')) { sortDirection = -1; } var rows = .each(rows, function(index, row) { var cell); }); rows.sort(function(a, b) { if (a.sortKey < b.sortKey) return -sortDirection; if (a.sortKey > b.sortKey) return sortDirection; return 0; }); table.children('tbody').append(row); row.sortKey = null; }); header.addClass('sorted-asc'); } else { table.find('td').removeClass('sorted') .filter(':nth-child(' + (column + 1) + ')') .addClass('sorted'); table.trigger('repaginate'); }); } }); }); }); document.ready(function() { $('table.paginated').each(function() { var currentPage = 0; var numPerPage = 10; var table.bind('repaginate', function() { table.find('tbody tr').length; var numPages = Math.ceil(numRows / numPerPage); var <div class="pager"></div>'); for (var page = 0; page < numPages; page++) { $('<span class="page-number"></span>').text(page + 1) .bind('click', {newPage: page}, function(event) { currentPage = event.data['newPage']; table.trigger('repaginate'); this.addClass('active') .siblings().removeClass('active'); }).appendTo(pager.insertBefore(// Example: Row striping // Step 1 // document.ready(function() { // $('table.striped tr:odd').addClass('odd'); // $('table.striped tr:even').addClass('even'); // }); // Step 2 // document.ready(function() { // $('table.striped tr:nth-child(odd)').addClass('odd'); // $('table.striped tr:nth-child(even)').addClass('even'); // }); // Step 3 // document.ready(function() { // $('table.striped tr:not(:has(th)):odd').addClass('odd'); // $('table.striped tr:not(:has(th)):even').addClass('even'); // }); // Step 4 // document.ready(function() { // $('table.striped tbody').each(function() { // this.find('tr:not(:has(th)):odd').addClass('odd'); // this.find('tr:not(:has(th)):even').addClass('even'); // }); // }); // Example: Advanced row striping // Step 1 // document.ready(function() { // $('table.striped tbody').each(function() { // this.find('tr:not(:has(th))').filter(function(index) { // return (index % 6) < 3; // }).addClass('odd'); // }); // }); // Step 2 // document.ready(function() { // $('table.striped tbody').each(function() { // this.find('tr:not(:has(th))').addClass('even') // .filter(function(index) { // return (index % 6) < 3; // }).removeClass('even').addClass('odd'); // }); // }); // Example: Interactive row highlighting // Step 1 // document.ready(function() { // var authorCells = $('table.striped td:nth-child(3)'); // // // Perform our highlighting here. // }); // }); // Step 2 // document.ready(function() { // var authorCells = $('table.striped td:nth-child(3)'); // // var authorName = this.text(); // authorCells.each(function(index) { // if (authorName == this.text()) { // this.parent().toggleClass('highlight'); // }; // }); // }); // }); // Step 3 // document.ready(function() { // var // authorCells.click(function() { // var authorName = this.text(); // // if (authorName == this.text()) { // this.parent().toggleClass('highlight'); // } // else { // this.parent().removeClass('highlight'); // } // }); // }); // }); // Example: Tooltips // Step 1, highlight author rows // document.ready(function() { // var authorCells = $('table.striped td:nth-child(3)'); // // .addClass('clickable') // .click(function() { // var authorName = this.text(); // authorCells.each(function(index) { // if (authorName == this.text()) { // this.parent().toggleClass('highlight'); // } // else { // this.parent().removeClass('highlight'); // } // }); // }); // }); // Step 2 // document.ready(function() { // var // var tooltip = $('<div id="tooltip"></div>').appendTo('body'); // // var positionTooltip = function(event) { // var tPosX = event.pageX; // var tPosY = event.pageY + 20; // // }; // var showTooltip = function(event) { // var authorName = this.text(); // tooltip // .text('Highlight all articles by ' + authorName) // .show(); // positionTooltip(event); // }; // var hideTooltip = function() { // // }; // // authorCells // .addClass('clickable') // .hover(showTooltip, hideTooltip) // .mousemove(positionTooltip) // .click(function(event) { // var authorName = this.text(); // // if (authorName == this.text()) { // this.parent().toggleClass('highlight'); // } // else { // this.parent().removeClass('highlight'); // } // }); // }); // }); // Step 3 // document.ready(function() { // var authorCells = $('table.striped td:nth-child(3)'); // var <div id="tooltip"></div>').appendTo('body'); // // var positionTooltip = function(event) { // var tPosX = event.pageX; // var tPosY = event.pageY + 20; // tooltip.css({top: tPosY, left: tPosX}); // }; // var showTooltip = function(event) { // var authorName = this.text(); // var action = 'Highlight'; // if (this.parent().is('.highlight')) { // action = 'Unhighlight'; // } // // .text(action + ' all articles by ' + authorName) // .show(); // positionTooltip(event); // }; // var hideTooltip = function() { // tooltip.hide(); // }; // // // .addClass('clickable') // .hover(showTooltip, hideTooltip) // .mousemove(positionTooltip) // .click(function(event) { // var authorName = this.text(); // authorCells.each(function(index) { // if (authorName == this.text()) { // this.parent().toggleClass('highlight'); // } // else { // this.parent().removeClass('highlight'); // } // }); // showTooltip.call(this, event); // }); // }); // Example: Collapsing and Expanding // document.ready(function() { // var collapseIcon = '../images/bullet_toggle_minus.png'; // var collapseText = 'Collapse this section'; // var expandIcon = '../images/bullet_toggle_plus.png'; // var expandText = 'Expand this section'; // $('table.collapsible tbody').each(function() { // var // $('<img />').attr('src', collapseIcon) // .attr('alt', collapseText) // .prependTo(section.find('th')) // .addClass('clickable') // .click(function() { // if ( // section.removeClass('collapsed') // .find('tr:not(:has(th))').fadeIn('slow'); // this.attr('src', collapseIcon) // .attr('alt', collapseText); // } // else { // // .find('tr:not(:has(th))').fadeOut('fast'); // this.attr('src', expandIcon) // .attr('alt', expandText); // } // }); // }); // }); // Example: Filtering // Step 1 // document.ready(function() { // $('table.filterable').each(function() { // var table = this; // // // if (this.is('.filter-column')) { // var filters = $('<div class="filters"></div>'); // $('<h3></h3>') // .text('Filter by ' + this.text() + ':') // .appendTo( // filters.insertBefore( // } // }); // }); // }); // Step 2 // document.ready(function() { // $('table.filterable').each(function() { // var table = this; // // // if (this.is('.filter-column')) { // var filters = $('<div class="filters"></div>'); // $('<h3></h3>') // .text('Filter by ' + this.text() + ':') // .appendTo( // // var keywords = ['conference', 'release']; // .each(keywords, function(index, keyword) { // $('<div class="filter"></div>').text(keyword) // .bind('click', {key: keyword}, function(event) { // $('tr:not(:has(th))', // var value = $('td', this).eq(column).text(); // if (value == event.data['key']) { // this.show(); // } // else { // this.hide(); // } // }); // this.addClass('active') // .siblings().removeClass('active'); // }).addClass('clickable').appendTo(filters); // }); // // table); // } // }); // }); // }); // Step 3 // document.ready(function() { // $('table.filterable').each(function() { // var // // table.find('th').each(function(column) { // if (this.is('.filter-column')) { // var <div class="filters"></div>'); // $('<h3></h3>') // .text('Filter by ' + this.text() + ':') // .appendTo(filters); // // $('<div class="filter">all</div>').click(function() { // // this.addClass('active') // .siblings().removeClass('active'); // }).addClass('clickable active').appendTo(filters); // // var keywords = {}; // // .each(function() { // keywords[this.text()] = this.text(); // }); // // .each(keywords, function(index, keyword) { // $('<div class="filter"></div>').text(keyword) // .bind('click', {key: keyword}, function(event) { // $('tr:not(:has(th))', // var value = $('td', this).eq(column).text(); // if (value == event.data['key']) { // this.show(); // } // else { // this.hide(); // } // }); // this.addClass('active') // .siblings().removeClass('active'); // }).addClass('clickable').appendTo(filters); // }); // // table); // } // }); // }); // }); // Step 4 // document.ready(function() { // $('table.striped').bind('stripe', function() { // $('tbody', this).each(function() { // this.find('tr:visible:not(:has(th))') // .removeClass('odd').addClass('even') // .filter(function(index) { // return (index % 6) < 3; // }).removeClass('even').addClass('odd'); // }); // }).trigger('stripe'); // }); // document.ready(function() { // $('table.filterable').each(function() { // var // // table.find('th').each(function(column) { // if (this.is('.filter-column')) { // var <div class="filters"></div>'); // $('<h3></h3>') // .text('Filter by ' + this.text() + ':') // .appendTo(filters); // // $('<div class="filter">all</div>').click(function() { // // this.addClass('active') // .siblings().removeClass('active'); // table.trigger('stripe'); // }).addClass('clickable active').appendTo( // // var keywords = {}; // table.find('td:nth-child(' + (column + 1) + ')') // .each(function() { // keywords[this.text()] = this.text(); // }); // // // $('<div class="filter"></div>').text(keyword) // .bind('click', {key: keyword}, function(event) { // $('tr:not(:has(th))', table).each(function() { // var value = $('td', this).eq(column).text(); // if (value == event.data['key']) { // this.show(); // } // else { // this.hide(); // } // }); // this.addClass('active') // .siblings().removeClass('active'); // // }).addClass('clickable').appendTo(filters); // }); // // table); // } // }); // }); // }); // Finished code: Highlighting, Tooltips, Collapsing, and Filtering document.ready(function() { $('table.striped').bind('stripe', function() { $('tbody', this).each(function() { this.find('tr:visible:not(:has(th))') .removeClass('odd').addClass('even') .filter(function(index) { return (index % 6) < 3; }).removeClass('even').addClass('odd'); }); }).trigger('stripe'); }); document.ready(function() { var tooltip = $('<div id="tooltip"></div>').appendTo('body'); var positionTooltip = function(event) { var tPosX = event.pageX; var tPosY = event.pageY + 20; }; var showTooltip = function(event) { var authorName = this.text(); var action = 'Highlight'; if (this.parent().is('.highlight')) { action = 'Unhighlight'; } tooltip .text(action + ' all articles by ' + authorName) .show(); positionTooltip(event); }; var hideTooltip = function() { }; authorCells .addClass('clickable') .hover(showTooltip, hideTooltip) .mousemove(positionTooltip) .click(function(event) { var authorName = this.text(); section = this; $('<img />').attr('src', collapseIcon) .attr('alt', collapseText) .prependTo(section.is('.collapsed')) { section.addClass('collapsed') .find('tr:not(:has(th))') .fadeOut('fast', function() { this.css('display', 'none'); }); this.attr('src', expandIcon) .attr('alt', expandText); } table = this; filters = $('<div class="filters"></div>'); $('<h3></h3>') .text('Filter by ' + this.text() + ':') .appendTo(<div class="filter">all</div>').click(function() { table.find('tbody tr').removeClass('filtered'); this.addClass('active') .siblings().removeClass('active'); filters); var keywords = {}; .each(keywords, function(index, keyword) { $('<div class="filter"></div>').text(keyword) .bind('click', {key: keyword}, function(event) { $('tr:not(:has(th))', table.trigger('stripe'); }).addClass('clickable').appendTo(filters.insertBefore(
(C) Æliens 20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.