I'm currently using jQuery datatables to make the table I am displaying. Currently I have it set up so when I select a row in the table, the text in that row appears in a textbox in another panel. I can then type in something else and press "Save" and it changes the text in that row. My current issue is when I add a new row via a button, I cant edit the text in the new row, I can select it however... Example: I have 3 rows, I select Test1 and I can successfully change the text. Then I click Add Row and then I select the new row and attempt to change the text in that row, it changes the previously selected row's text (a row that was in the table upon load). How can I fix this issue? I've looked into cell.data, cell.edit but that's for the Editor which I'm not currently using for various reasons.
Table:
<div class="panel-body">
<table id="data-table" class="table table-striped table-bordered nowrap" width="100%">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>All</td>
</tr>
<tr class="even">
<td>Test1</td>
</tr>
<tr class="odd">
<td>Test2</td>
</tr>
</tbody>
</table>
<div class="col-md-8 col-md-offset-1">
<button type="submit" class="btn btn-sm btn-primary m-r-5" id="addbtn" >New Group</button>
</div>
</div>
//Text box
<div class="form-group">
<label class="col-md-4 control-label">Group Name:</label>
<div class="col-md-8">
<input type="text" id="groupname" class="form-control" value="Name"/>
</div>
</div>
Code for making the selected row's text appear in textbox:
(function () {
var table = document.querySelector('#data-table');
var textbox = document.querySelector('#groupname');
table.addEventListener('click', onTableClick);
function onTableClick (e) {
var tr = e.target.parentElement;
var data = [];
for (var td of tr.children) {
data.push(td.innerHTML);
}
textbox.value = data[0];
}
})();
Changing the selected row's text:
var row = null;
$("#data-table tr td").click(function() {
$("#groupname").val($(this).text());
row = $(this);
$("#saverow").click(function() {
if (row != null) {
row.text($("#groupname").val());
}
});
});
Add row:
$("#addbtn").click( function() {
var t = $('#data-table').DataTable();
$('#data-table').dataTable();
t.row.add( [
"New Group"
] ).draw( false );
});