This is an expansion of a previous question Dynamically append html table cell and the html has been slightly changed.
<div class="docs-main">
<h2>Workflows</h2>
<table class="tablesaw" data-tablesaw-mode="stack" data-tablesaw-sortable data-tablesaw-sortable-switch data-tablesaw-minimap data-tablesaw-mode-switch>
<thead>
<tr>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="persist">Workflow Name</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-sortable-default-col data-tablesaw-priority="1">Title</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="2">Assigned To</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="3">Status</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="4">Due Date</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="5">Outcome</th>
</tr>
</thead>
<tbody>
<tr>
<td class="title">
<input type="text" />
</td>
<td>
<input type="text" />
</td>
<td>
<select id="e1">
<option value="1">Name 1</option>
<option value="2">Name 2</option>
<option value="3">Name 3</option>
</select>
</td>
<td>
<select id="e2">
<option value="#00ff00">Complete</option>
<option value="#ffff00">In Progress</option>
<option value="#ff0000">Incomplete</option>
</select>
</td>
<td>
<input type="datetime" />
</td>
<td>
<input type="text" />
</td>
</tr>
</tbody>
</table>
<input type="submit" value="Add Row" />
The updated jsfiddle is https://jsfiddle.net/marcwebdude/48ng08tq/13/ . The problem is the append function at lines 47-64 where it is automatically appending a row.
$(function () {
var rowTamplate = $('table.tablesaw tbody tr').eq(0);
var rowContent = [
'<input type="text" value="Name" />',
'<input type="text" value="Title" />',
'<select><option>Name 1</option><option>Name 2</option><option>Name 3</option></select>',
'<select><option value="#00ff00">Complete</option><option value="#ffff00">In Progress</option><option value="#ff0000">Incomplete</option></select>',
'<input type="datetime" value="Select Date" />',
'<input type="text" />'
];
var rowToadd = rowTamplate.clone();
rowToadd.find('td').each(function (index, element) {
$(element).append(rowContent[index]);
});
rowToadd.insertAfter('table.tablesaw tr:eq(2)');
for (var i = 0; i < 10; i++) {
rowToadd.clone().insertAfter('table.tablesaw tr:eq(2)');
}
I am looking for a solution that when the button is clicked it adds a row with the appended html. Not to automatically add it after a currently existing row. For instance if this table was blank, a button click would add a row with these 6 specified columns and the inputs that coincide with each cell.
The second issue with my existing script is that it's not formatting the appended html, when a row is added manually into the html file, it's formatting correctly, but not if it's appended via jquery.
The updated jsfiddle with revised jquery is https://jsfiddle.net/marcwebdude/48ng08tq/73/ , and so far it's appending contents into the table correctly. Here's the html
<div class="docs-main">
<h2>Workflows</h2>
<table class="tablesaw" data-tablesaw-mode="stack" data-tablesaw-sortable data-tablesaw-sortable-switch data-tablesaw-minimap data-tablesaw-mode-switch>
<thead>
<tr>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="persist">Workflow Name</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-sortable-default-col data-tablesaw-priority="1">Title</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="2">Assigned To</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="3">Status</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="4">Due Date</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="5">Outcome</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<input type="submit" value="Add Row" id="button" />
</div>
and here's the jquery
$(document).ready(function () {
$('#button').on('click', function(){
$('.tablesaw').find('tbody').append($('<tr><td><input type="text" value="Name" /></td><td class="title"><input type="text" value="Title" /></td><td><select class="e1"><option value="0">-- User --</option><option>Name 1</option><option>Name 2</option><option>Name 3</option></select></td><td><select class="e2"><option value="#fff">-- Status --</option><option value="#00ff00">Complete</option><option value="#ffff00">In Progress</option><option value="#ff0000">Incomplete</option></select></td><td><input type="datetime" value="Select Date" /></td><td><input type="text" /></td></tr>'));
$('.e1').select2();
$('.e2').select2().on('change', function () {
$(this).next()
.find('.select2-selection')
.css({ backgroundColor: this.value });
}).trigger('change');
});
});
This appends the correct data into each cell as well as inherits styles once the row is created.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.