Skip to content

Instantly share code, notes, and snippets.

@rxnlabs
Created July 31, 2015 21:30
Show Gist options
  • Save rxnlabs/eb8845e59a559120aadb to your computer and use it in GitHub Desktop.
Save rxnlabs/eb8845e59a559120aadb to your computer and use it in GitHub Desktop.
JS - Sort a table with stocks into groups using the Footable plugin and jQuery
// sort the table after window object is done loading
jQuery(window).ready(function($){
var $ = jQuery;
// sort Portfolio table by the portfolio group
sort_portfolio_group();
});
// sort the portfolio by group and by the selected sorting order
function sort_portfolio_group(){
var $ = jQuery;
// hide the tradeview charts that are current showing and append them to page
$('.tradeview').text('Trading Chart');
$('.tradeview_row').remove();
var load_tables = sort_tables();
load_tables.then(function(promise){
$('table.footable').css('visibility','hidden');
$('table.footable').css('display','table');
// apply the resized columns to the shown footables
$('.footable').each(function(){
var footable = $(this).data('footable');
footable.resize();
});
return promise;
}).done(function(){
var window_size = $('table.footable').outerWidth();
if( window_size <= footable_breakpoints.phone ){
//expand_portfolio_group();
}
$('table.footable').css('visibility','visible');
$('.pt_preloader').remove();
});
}
// deferred function to call after page load to increase perceived load speed
function sort_tables(){
var $ = jQuery;
var deferred = $.Deferred();
$('table.footable').each(function(){
var table_id = $(this).attr('id');
var current_footable = $(this);
var duplicateChk = {};
$('#'+table_id+' tr').each(function(){
var portfolio_group = $(this).attr('id');
// check to see if duplicate portfolio group names are in the table
if (typeof portfolio_group !== typeof undefined && portfolio_group !== false) {
var portfolio_group_row = $(this);
var first_row = $('#'+table_id+' tr[data-group='+portfolio_group+']').first();
var i = 0;
var last_row_selected = null;
$('#'+table_id+' tr[data-group='+portfolio_group+']').each(function(){
if( i != 0 ){
// append each member of the group after the previous member of the group
$(last_row_selected).after($(this));
}
last_row_selected = $(this);
i++;
});
// after grouping all members of the portfolio group together, add the group name before the first member of the group
$(first_row).before(portfolio_group_row);
}
});
});
deferred.resolve();
return deferred.promise();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment