Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://www.javascripttoolbox.com/temp/table_cellindex.html" rel="nofollow noreferrer">This sample</a> may help you find your solution. It seems to demonstrate your problem as well as have some sample code to generate a matrix to help you solve it.</p> <p><strong>EDIT:</strong> I liked the puzzle and decided to play with it for a bit, here is a "functioning" example of implementing that sample (although sometimes the table doesn't seem to redraw correctly. This should probably help you get further along the way.</p> <pre><code>function getTableState(t) { var matrix = []; var lookup = {}; var trs = t.getElementsByTagName('TR'); var c; for (var i=0; trs[i]; i++) { lookup[i] = []; for (var j=0; c = trs[i].cells[j]; j++) { var rowIndex = c.parentNode.rowIndex; var rowSpan = c.rowSpan || 1; var colSpan = c.colSpan || 1; var firstAvailCol; // initalized the matrix in this row if needed. if(typeof(matrix[rowIndex])=="undefined") { matrix[rowIndex] = []; } // Find first available column in the first row for (var k=0; k&lt;matrix[rowIndex].length+1; k++) { if (typeof(matrix[rowIndex][k])=="undefined") { firstAvailCol = k; break; } } lookup[rowIndex][c.cellIndex] = firstAvailCol; for (var k=rowIndex; k&lt;rowIndex+rowSpan; k++) { if(typeof(matrix[k])=="undefined") { matrix[k] = []; } var matrixrow = matrix[k]; for (var l=firstAvailCol; l&lt;firstAvailCol+colSpan; l++) { matrixrow[l] = {cell: c, rowIndex: rowIndex}; } } } } // lets build a little object that has some useful funcitons for this table state. return { cellMatrix: matrix, lookupTable: lookup, // returns the "Real" column number from a passed in cell getRealColFromElement: function (cell) { var row = cell.parentNode.rowIndex; var col = cell.cellIndex; return this.lookupTable[row][col]; }, // returns the "point" to insert at for a square in the perceived row/column getPointForRowAndColumn: function (row,col) { var matrixRow = this.cellMatrix[row]; var ret = 0; // lets look at the matrix again - this time any row that shouldn't be in this row doesn't count. for (var i=0; i&lt;col; i++) { if (matrixRow[i].rowIndex == row) ret++; } return ret; } }; } function scheduleClick(e) { if (e.target.className != 'event') return; //Get useful info before deletion var numRows = e.target.rowSpan; var cellIndex = e.target.cellIndex; var rowIndex = e.target.parentNode.rowIndex; var table = e.target.parentNode.parentNode; var tableState = getTableState(table); var colIndex = tableState.getRealColFromElement(e.target); //Deletion e.target.parentNode.deleteCell(cellIndex); //Insert empty cells in each row for(var i = 0; i &lt; numRows; i++) { var row = table.rows[rowIndex + i]; row.insertCell(tableState.getPointForRowAndColumn(rowIndex+i, colIndex)); } } </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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