At once I will make a reservation that adding a column was written by the first virus that occurred to me, but in the examples this is not the main idea, so if you like, you can use any other option.
Here is the first option, but it’s probably not suitable for the author, after all, the column is moving a bit, but in terms of accuracy on the web, it will be more adaptive:
$(document).ready(function() { var i = 0; $('button').click(function() { $('tr').prepend('<td>cell ' + i + '</td>'); i = i - 1; }); });
table { margin: 0 auto; } td { border: 1px solid #ccc; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tbody> <tr> <td>cell 1</td> <td>cell 2</td> <td>cell 3</td> <td>cell 4</td> <td>cell 5</td> <td>cell 6</td> </tr> <tr> <td>cell 1</td> <td>cell 2</td> <td>cell 3</td> <td>cell 4</td> <td>cell 5</td> <td>cell 6</td> </tr> </tbody> </table> <button>Click me</button>
The second option, this is exactly what the author wants, took the indentation for the example of a random, and the end of the sheet is determined by the middle cell in the table and if it is more than the remaining space, the cell is not added and the message appears:
$(document).ready(function() { var i = 0; $('button').click(function() { var tabMargin = parseFloat($('table').css('margin-left')); var tableCellLength = $('tr').find('td').length; var tableWidth = parseFloat($('table').css('width')); if (tabMargin - tableWidth / tableCellLength - 15 > 0) { $('tr').prepend('<td>cell ' + i + '</td>'); var addCellWidth = parseFloat($('tr').find('td').eq(0).css('width')); $('table').css('margin-left', tabMargin - addCellWidth - 6 + 'px'); i = i - 1; } else { alert('Вы пытаетесь нарисовать ячейку на столе'); } }); });
table { margin-left: 175px; } td { border: 1px solid #ccc; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tbody> <tr> <td>cell 1</td> <td>cell 2</td> <td>cell 3</td> <td>cell 4</td> <td>cell 5</td> <td>cell 6</td> </tr> <tr> <td>cell 1</td> <td>cell 2</td> <td>cell 3</td> <td>cell 4</td> <td>cell 5</td> <td>cell 6</td> </tr> </tbody> </table> <button>Click me</button>
UPD. Option 3. The table no longer jumps, but did not limit the width, you can optionally take from the second option:
$(document).ready(function() { var j = 0; var tableRowLength = $('#mainTable').find('tr').length; for (var i = 0; i < tableRowLength; i++) { $('#tablePaste').append('<tr></tr>'); } $('button').click(function() { $('#tablePaste').find('tr').prepend('<td>cell ' + j + '</td>'); j = j - 1; }); });
table { margin-left: 200px; } td { border: 1px solid #ccc; } .paste-box { position: relative; width: 201px; text-align: right; } #tablePaste { position: absolute; top: 0; right: 0; } #tablePaste td { white-space: nowrap; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="paste-box"> <table id="tablePaste"> </table> </div> <table id="mainTable"> <tbody> <tr> <td>cell 1</td> <td>cell 2</td> <td>cell 3</td> <td>cell 4</td> <td>cell 5</td> <td>cell 6</td> </tr> <tr> <td>cell 1</td> <td>cell 2</td> <td>cell 3</td> <td>cell 4</td> <td>cell 5</td> <td>cell 6</td> </tr> </tbody> </table> <button>Click me</button>
Ps. I apologize for my jQuery, if you wish, you can rewrite to native JS, but I'm lazy. :)