, , jQuery, ECMAScript 6 ( / Array.from()); , ES5/ . , -, ES6:
function addColumns() {
var clicked = this,
cell = clicked.closest('td'),
table = clicked.closest('table'),
rowsArray = Array.from(table.rows),
index = cell.cellIndex,
cellType, newCell, next;
rowsArray.forEach(function(row) {
cellType = row.children[index].tagName;
newCell = document.createElement(cellType);
switch (cellType.toLowerCase()) {
case 'th':
next = row.children[index + 1];
newCell.textContent = 'Header';
if (next) {
row.insertBefore(newCell, next);
} else {
row.appendChild(newCell);
}
break;
case 'td':
row.insertCell(index + 1);
break;
}
});
}
var links = document.querySelectorAll('a.cht-add'),
arrayOfLinks = Array.from(links);
arrayOfLinks.forEach(link => link.addEventListener('click', addColumns));
function addColumns() {
var clicked = this,
cell = clicked.closest('td'),
table = clicked.closest('table'),
rowsArray = Array.from(table.rows),
index = cell.cellIndex,
cellType, newCell, next;
rowsArray.forEach(function(row) {
cellType = row.children[index].tagName;
newCell = document.createElement(cellType);
switch (cellType.toLowerCase()) {
case 'th':
next = row.children[index + 1];
newCell.textContent = 'Header';
if (next) {
row.insertBefore(newCell, next);
} else {
row.appendChild(newCell);
}
break;
case 'td':
row.insertCell(index + 1);
break;
}
});
}
var links = document.querySelectorAll('a.cht-add'),
arrayOfLinks = Array.from(links);
arrayOfLinks.forEach(link => link.addEventListener('click', addColumns));
table {
border-collapse: collapse;
}
th,
td {
border-bottom: 1px solid #000;
padding: 0.5em;
}
th + th,
td + td {
border-left: 1px solid #000;
}
<section class="yield-report-table">
<table id="sorting">
<tr class="headerrow ui-sortable">
<th class="ui-resizable tac">
<div class="ui-resizable">First Year Corn
<br>Non Limited N, High Pop(1)</div>
</th>
<th class="ui-resizable tac">
<div class="ui-resizable">First Year Corn
<br>Non Limited N, High Pop(2)</div>
</th>
</tr>
<tr>
<td>
<div class="report-icon-list bg-l-green">
<a href="#" class="cht-add"><span><i class="cht-sprite">1</i></span></a>
</div>
</td>
<td>
<div class="report-icon-list bg-l-green">
<a href="#" class="cht-add"><span><i class="cht-sprite">2</i></span></a>
</div>
</td>
</tr>
</table>
</section>
Hide resultJS Fiddle demo.
, ES5/ , ; , :
function closest(startAt, elemType) {
var current = startAt,
tag = elemType.replace(/[^a-z]/gi, '');
while (current.tagName.toLowerCase() !== tag && current.tagName.toLowerCase() !== 'body') {
current = current.parentNode;
}
return current.tagName.toLowerCase() === 'body' ? startAt : current;
}
function addColumns() {
var clicked = this,
cell = closest(clicked, 'td'),
table = closest(cell, 'table'),
rowsArray = Array.prototype.slice.call(table.rows, 0),
index = cell.cellIndex,
cellType, newCell, next;
rowsArray.forEach(function(row) {
cellType = row.children[index].tagName;
newCell = document.createElement(cellType);
switch (cellType.toLowerCase()) {
case 'th':
next = row.children[index + 1];
newCell.textContent = 'Header';
if (next) {
row.insertBefore(newCell, next);
} else {
row.appendChild(newCell);
}
break;
case 'td':
row.insertCell(index + 1);
break;
}
});
}
var links = document.querySelectorAll('a.cht-add'),
arrayOfLinks = Array.prototype.slice.call(links, 0);
arrayOfLinks.forEach(function(link) {
link.addEventListener('click', addColumns);
});
function closest(startAt, elemType) {
var current = startAt,
tag = elemType.replace(/[^a-z]/gi, '');
while (current.tagName.toLowerCase() !== tag && current.tagName.toLowerCase() !== 'html') {
current = current.parentNode;
}
return current.tagName.toLowerCase() === 'body' ? startAt : current;
}
function addColumns() {
var clicked = this,
cell = closest(clicked, 'td'),
table = closest(cell, 'table'),
rowsArray = Array.prototype.slice.call(table.rows, 0),
index = cell.cellIndex,
cellType, newCell, next;
rowsArray.forEach(function(row) {
cellType = row.children[index].tagName;
newCell = document.createElement(cellType);
switch (cellType.toLowerCase()) {
case 'th':
next = row.children[index + 1];
newCell.textContent = 'Header';
if (next) {
row.insertBefore(newCell, next);
} else {
row.appendChild(newCell);
}
break;
case 'td':
row.insertCell(index + 1);
break;
}
});
}
var links = document.querySelectorAll('a.cht-add'),
arrayOfLinks = Array.prototype.slice.call(links, 0);
arrayOfLinks.forEach(function(link) {
link.addEventListener('click', addColumns);
});
table {
border-collapse: collapse;
}
th,
td {
border-bottom: 1px solid #000;
padding: 0.5em;
}
th + th,
td + td {
border-left: 1px solid #000;
}
<section class="yield-report-table">
<table id="sorting">
<tr class="headerrow ui-sortable">
<th class="ui-resizable tac">
<div class="ui-resizable">First Year Corn
<br>Non Limited N, High Pop(1)</div>
</th>
<th class="ui-resizable tac">
<div class="ui-resizable">First Year Corn
<br>Non Limited N, High Pop(2)</div>
</th>
</tr>
<tr>
<td>
<div class="report-icon-list bg-l-green">
<a href="#" class="cht-add"><span><i class="cht-sprite">1</i></span></a>
</div>
</td>
<td>
<div class="report-icon-list bg-l-green">
<a href="#" class="cht-add"><span><i class="cht-sprite">2</i></span></a>
</div>
</td>
</tr>
</table>
</section>
Hide resultJS Fiddle demo.
jQuery, :
function addColumn() {
var clicked = $(this),
cell = clicked.closest('td'),
table = clicked.closest('table'),
index = cell.prop('cellIndex'),
th = $('<th />', {
'text': 'Heading'
}),
td = $('<td />');
table.find('tr > :nth-child(' + (index + 1) + ')').after(function(){
return $(this).is('th') ? th.clone(true) : td.clone(true);
})
}
$('td a.cht-add').on('click', addColumn);
function addColumn() {
var clicked = $(this),
cell = clicked.closest('td'),
table = clicked.closest('table'),
index = cell.prop('cellIndex'),
th = $('<th />', {
'text': 'Heading'
}),
td = $('<td />');
table.find('tr > :nth-child(' + (index + 1) + ')').after(function() {
return $(this).is('th') ? th.clone(true) : td.clone(true);
})
}
$('td a.cht-add').on('click', addColumn);
table {
border-collapse: collapse;
}
th,
td {
border-bottom: 1px solid #000;
padding: 0.5em;
}
th + th,
td + td {
border-left: 1px solid #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="yield-report-table">
<table id="sorting">
<tr class="headerrow ui-sortable">
<th class="ui-resizable tac">
<div class="ui-resizable">First Year Corn
<br>Non Limited N, High Pop(1)</div>
</th>
<th class="ui-resizable tac">
<div class="ui-resizable">First Year Corn
<br>Non Limited N, High Pop(2)</div>
</th>
</tr>
<tr>
<td>
<div class="report-icon-list bg-l-green">
<a href="#" class="cht-add"><span><i class="cht-sprite">1</i></span></a>
</div>
</td>
<td>
<div class="report-icon-list bg-l-green">
<a href="#" class="cht-add"><span><i class="cht-sprite">2</i></span></a>
</div>
</td>
</tr>
</table>
</section>
Hide resultJS Fiddle demo.
: