Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd row to html table below the selected row when previous row has rowspan
    primarykey
    data
    text
    <p>I have the following code to add rows to a table using a context menu plugin so you can right click the cell you want to add the row below.</p> <pre><code>(function($){ function scanTable( $table ) { var m = []; $table.children( "tr" ).each( function( y, row ) { $( row ).children( "td, th" ).each( function( x, cell ) { var $cell = $( cell ), cspan = $cell.attr( "colspan" ) | 0, rspan = $cell.attr( "rowspan" ) | 0, tx, ty; cspan = cspan ? cspan : 1; rspan = rspan ? rspan : 1; for( ; m[y] &amp;&amp; m[y][x]; ++x ); //skip already occupied cells in current row for( tx = x; tx &lt; x + cspan; ++tx ) { //mark matrix elements occupied by current cell with true for( ty = y; ty &lt; y + rspan; ++ty ) { if( !m[ty] ) { //fill missing rows m[ty] = []; } m[ty][tx] = true; } } var pos = { top: y, left: x }; $cell.data( "cellPos", pos ); } ); } ); }; /* plugin */ $.fn.cellPos = function( rescan ) { var $cell = this.first(), pos = $cell.data( "cellPos" ); if( !pos || rescan ) { var $table = $cell.closest( "table, thead, tbody, tfoot" ); scanTable( $table ); } pos = $cell.data( "cellPos" ); return pos; } })(jQuery); appendMe(); function appendMe() { $('table.test td').find("em").remove() $('table.test td').removeAttr("realCellEq").append(function(){ return "&lt;em&gt; " + $(this).cellPos().left + "&lt;/em&gt;" }).attr("realCellEq", function() { return $(this).cellPos().left }); } $(function () { $.contextMenu({ selector: 'table.test td', items: { "addRowBelow": { name: "Add Row Below", callback: function (key, options) { $(this).parents("tr").after("&lt;tr class='freshAdd'&gt;&lt;/tr&gt;"); $(this).parents("tr").find("td").each( function() { var thisRowSpan = ($(this).attr("rowspan")) ? $(this).attr("rowspan") : 1; if(thisRowSpan &gt; 1) { $(this).attr("rowspan", (parseInt($(this).attr("rowspan"),10)+1)); } else { $(this).clone().appendTo("tr.freshAdd"); } }); $("tr.freshAdd").find("td").html(""); $("tr.freshAdd").removeClass("freshAdd"); appendMe(); } } } }); }); </code></pre> <p>The trouble is i can't work out what I need to do to take into consideration rowspans.</p> <p>Here's a jsfiddle to explain what I mean.</p> <p><a href="http://jsfiddle.net/many_tentacles/sqjak/1/" rel="nofollow">http://jsfiddle.net/many_tentacles/sqjak/1/</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload