Skip to content

Instantly share code, notes, and snippets.

@lski
Created August 23, 2014 16:58
Show Gist options
  • Save lski/77a9055cf9e43b884443 to your computer and use it in GitHub Desktop.
Save lski/77a9055cf9e43b884443 to your computer and use it in GitHub Desktop.
jQuery plugin to return the zero based column position of a table cell, regardless of colspans
/**
* An extension to JQuery that returns the actual column position of the table cell regardless of columns hvaing a colspan
*
* returns column index (zerobased)
*
* Based on code from SolutionYogi on stackoverflow
* http://stackoverflow.com/questions/1166452/finding-column-index-using-jquery-when-table-contains-column-spanning-cells
*/
(function($) {
$.fn.columnPosition = function() {
if(! this.is('td') && ! this.is('th'))
return -1;
var allCells = this.parent('tr').children();
var normalIndex = allCells.index(this);
var nonColSpanIndex = 0;
allCells.each(
function(i, item)
{
if(i == normalIndex)
return false;
var colspan = $(item).attr('colspan');
colspan = colspan ? parseInt(colspan, 10) : 1;
nonColSpanIndex += colspan;
}
);
return nonColSpanIndex;
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment