Reputation: 63
I have a table, that is dynamicallly increased with Firebase, and I need a delete
and edit
button on each row of the table, currently, the remove button is working, but I am having trouble with the edit button, I saw a few examples around, but i'm not sure how to do it using append()...
Here's what I have so far:
HTML
<table id="tableAssets" class="mdl-data-table mdl-js-data-table mdl-shadow--2dp">
<thead>
<tr id="tableHeader">
<th class="mdl-data-table__cell--non-numeric">Name</th>
<th class="mdl-data-table__cell--non-numeric">Brand</th>
<th class="mdl-data-table__cell--non-numeric"> </th>
</tr>
</thead>
<tbody id="table_body"> </tbody>
</table>
JavaScript
rootRef.on("child_added", snap => {
var assetKey = snap.child("id").val();
var name = snap.child("name").val();
var brand = snap.child("brand").val();
$("#table_body").append("<tr data-id='"+assetKey+"'>"+
"<td class='mdl-data-table__cell--non-numeric'>" + name + "</td>" +
"<td class='mdl-data-table__cell--non-numeric'>" + brand + "</td>" +
"<td class='mdl-data-table__cell--non-numeric'><div buttons>"+
"<button class='edit-btn'><i class='material-icons'>mode_edit</i></button>"+" "+
"<button class='delete-btn'><i class='material-icons'>delete</i></button>"+" "+
"</div></td></tr>");
});
And here is what I was thinking on doing with the edit button: Hide the whole row, and add a new one with the saved information, but with text fields, and change the edit button with a save button, but I have no idea how I should be doing this...
$("#table_body").on('click','.edit-btn', function(e){
var $row = $(this).closest('tr');
$row.hide();
});
Upvotes: 0
Views: 3669
Reputation: 6383
I'd suggest you to do this:
$("#table_body").append("<tr id='"+assetKey+"'>
<button class='edit-btn' onclick=edit(\'"+assetKey+"\')><i class='material-icons'>mode_edit</i></button>
('#'+id).hide();
And to make sense of this and it is not just in words, a functional example using your code on jsfiddle here.
Hope this is of help!
Upvotes: 1