I think I have been looking at this code too long.
Goal: Add and take away columns from a selected row
I'm almost there with this problem. What's working:
- Add columns to selected row ONCE
- If column has additional rows and another is selected, remove the columns from the original and add to the new selected
- Remove columns from row when re-selected
For example:
Say I select row where ID = 2...the extra columns for that row will display. I then reselect the same row...the extra columns are not removed. But if I select that row again, for a 3rd consecutive time, the extra columns will not display.
I have found that what is causing this is that because of my last if statement, the columns are never re-added.
jQuery
var lastClicked;
$(document).ready(function() {
var tr = $('tbody tr');
$(tr).each(function () {
$(this).addClass("notShown");
});
$(tr).click(function() {
var rowID = $(this).attr('id');
var editRow = "<td><a href='edit?id=" + rowID + "' id='editBtn'>Edit</a></td>";
var deleteRow = "<td><a id='deleteBtn' onclick='confirmDelete(" + rowID + ")'>Delete</a></td>";
$(tr).each(function () {
if ($(this).hasClass('shown')) {
for (var i = 0; i < 2; i++) {
$(this).find("td:first-child").remove();
}
$(this).removeClass("shown").addClass("notShown");
}
});
if (lastClicked != $(this).attr('id')) {
$(this).prepend(editRow);
$(this).prepend(deleteRow);
$(this).removeClass("notShown").addClass("shown");
}
lastClicked = $(this).attr('id');
});
});