triplethreat77
triplethreat77

Reputation: 1296

How to delete multiple or single rows

I have tables with father son relationships. If you delete the father, the sons under it are deleted. You can also delete an individual son. My code so far doesn't delete the father or son with the corresponding id, it deletes the first thing in the table. Here is my code:

<script>
function delVesselAll(num){
    $("#vessel_tab #father" + num).remove();
    $("#vessel_tab .son" + num).remove();
    document.getElementById(id).style.display = 'block';
};
function delSon(num){
    $("#vessel_tab #son" + num).remove();
    document.getElementById(id).style.display = 'block';
};
</script>

And my HTML:

<table id="vessel_tab">
  <tr id="father1">
    <td colspan="2" class="father_header">Father 1</td>
    <td><a style="text-decoration:none;" onclick="showModal('delAllModal')"><input type="button" value="delete" onclick="showModal('delAllModal')"></a></td>
      <div class="delModal" style="z-index:999999; margin-left:200px; margin-top:10px; display:none" id="delAllModal">
Are you sure you want to delete the father and corresponding sons?<br /><input type="button" value="Cancel" class="hide" onclick="hideModal('delAllModal')"/><input type="button" value="Delete" onclick="delVesselAll(1)" id="delete"/></div>
  </tr>
  <tr class="son1" id="son1">
    <td>&nbsp;</td>
    <td>Son 1</td>
    <td><a style="text-decoration:none;" onclick="showModal('delSonModal')"><input type="button" value="delete"></a></td>
      <div class="delModal" style="z-index:999999; margin-left:200px; margin-top:30px; display:none" id="delSonModal">
Please confirm deletion.<br /><input type="button" value="Cancel" class="hide" onclick="hideModal('delSonModal')"/><input type="button" value="Delete" onclick="delSon(1)"/></div>
  </tr>
  <tr class="son2" id="son2">
    <td>&nbsp;</td>
    <td>Son 2</td>
    <td><a style="text-decoration:none;" onclick="showModal('delSonModal')"><input type="button" value="delete"></a></td>
      <div class="delModal" style="z-index:999999; margin-left:200px; margin-top:30px; display:none" id="delSonModal">
Please confirm deletion.<br /><input type="button" value="Cancel" class="hide" onclick="hideModal('delSonModal')"/><input type="button" value="Delete" onclick="delSon(2)"/></div>
  </tr>
  <tr id="father2">
    <td colspan="2">Father 2</td>
    <td><a style="text-decoration:none;" onclick="showModal('delAllModal')"><input type="button" value="delete"></a></td>
      <div class="delModal" style="z-index:999999; margin-left:200px; margin-top:10px; display:none" id="delAllModal">
Are you sure you want to delete the father and corresponding sons?<br /><input type="button" value="Cancel" class="hide" onclick="hideModal('delAllModal')"/><input type="button" value="Delete" onclick="delVesselAll(2)" id="delete"/></div>
  </tr>
 <tr class="son3" id="son3">
    <td>&nbsp;</td>
    <td>Son 3</td>
    <td><a style="text-decoration:none;" onclick="showModal('delSonModal')"><input type="button" value="delete"></a></td>
      <div class="delModal" style="z-index:999999; margin-left:200px; margin-top:30px; display:none" id="delSonModal">
Please confirm deletion.<br /><input type="button" value="Cancel" class="hide" onclick="hideModal('delSonModal')"/><input type="button" value="Delete" onclick="delSon(3)"/></div>
  </tr>
  <tr class="son4" id="son4">
    <td>&nbsp;</td>
    <td>Son 4</td>
    <td><a style="text-decoration:none;" onclick="showModal('delSonModal')"><input type="button" value="delete"></a></td>
      <div class="delModal" style="z-index:999999; margin-left:200px; margin-top:30px; display:none" id="delSonModal">
Please confirm deletion.<br /><input type="button" value="Cancel" class="hide" onclick="hideModal('delSonModal')"/><input type="button" value="Delete" onclick="delSon(4)"/></div>
  </tr>
</table>

Also, the jsfiddle. It's not working correctly(don't know what I did wrong), but my code is there. Thanks in advance! http://jsfiddle.net/7tFFS/1/

Upvotes: 0

Views: 286

Answers (2)

Grinn
Grinn

Reputation: 5543

I cleaned things up and refactored your code. Here's the JSFiddle.

The HTML:

<div class="delModal" style="z-index:999999; margin-left:200px; margin-top:10px; display:none"
id="delAllModal">Are you sure you want to delete the father and corresponding sons?
    <br
    />
    <input type="button" value="Cancel" class="hide" />
    <input type="button" value="Delete" class="delete" />
</div>
<div class="delModal" style="z-index:999999; margin-left:200px; margin-top:30px; display:none"
id="delSonModal">Please confirm deletion.
    <br />
    <input type="button" value="Cancel" class="hide" />
    <input type="button" value="Delete" class="delete" />
</div>
<table id="vessel_tab">
    <tr id="father1">
        <td colspan="2" class="father_header">Father 1</td>
        <td>
            <input type="button" value="delete" class="delete" />
        </td>
    </tr>
    <tr class="son1" id="son1" data-father="father1">
        <td>&nbsp;</td>
        <td>Son 1</td>
        <td>
            <input type="button" value="delete" class="delete" />
        </td>
    </tr>
    <tr class="son2" id="son2" data-father="father1">
        <td>&nbsp;</td>
        <td>Son 2</td>
        <td>
            <input type="button" value="delete" class="delete" />
        </td>
    </tr>
    <tr id="father2">
        <td colspan="2">Father 2</td>
        <td>
            <input type="button" value="delete" class="delete" />
        </td>
    </tr>
    <tr class="son3" id="son3" data-father="father2">
        <td>&nbsp;</td>
        <td>Son 3</td>
        <td>
            <input type="button" value="delete" class="delete" />
        </td>
    </tr>
    <tr class="son4" id="son4" data-father="father2">
        <td>&nbsp;</td>
        <td>Son 4</td>
        <td>
            <input type="button" value="delete" class="delete" />
        </td>
    </tr>
</table>

The JS:

function showModal(id, child) {
    var $modal = $(child ? "#delSonModal" : "#delAllModal");
    $modal.fadeIn("slow")
        .find(".delete")
        .one("click", function () {
            $("#" + id + ", [data-father=" + id + "]").fadeOut("slow");
            $modal.fadeOut("slow");
        });
}


$("input.hide").click(function () {
    $(this).closest("div.delModal").fadeOut("slow");
});

$("#vessel_tab input.delete").bind("click", function () {
    var $tr = $(this).closest("tr");
    showModal($tr.attr("id"), !!$tr.data("father"));
});

EDIT:

Your Fiddle wasn't working because of your use of in-HTML onClick handlers. It's not necessarily wrong, but JSFiddle didn't seem to like it. I replaced those with jQuery handlers, throughout.

In the HTML, your repeating of your dialogs wasn't necessary so I took those out and put them outside the table. There were some anchor tags that were superfluous as well, which I removed. I also added slashes to the end of your input tags... Not really all that necessary, but XHTML requires it and it made Fiddle crazy, so that's that. I used classes instead of ID's in some places (like the cancel/delete buttons) to give a simple way to reference them in jQuery as a group. (You can't have duplicate ID tags without things throwing up.) The data-father tag on all the sons is what is used later in showModal to determine which dialog (delAllModal or delSonModal) should be displayed, and more importantly to link the sons to their fathers to allow their deletion as a whole unit.

In the JS:

  • A lot of the code I rewrote to just execute in-line instead of calling a function. The exception being showModal.
  • $("input.hide").click... Wires up both of the Cancel buttons in the dialogs.
  • $("#vessel_tab input.delete")... Wires up all of the Delete buttons in the table. It's basically just a wrapper for showModal...
  • ...which brings us to the meat. showModal:
    • The var $modal... line picks which dialog to display based on if the clicked button was for a child.
    • The dialog is shown, and its delete button is wired up. Notice that I use one here, and not on. This will make a click handler for the button, but dispose of it immediately upon it's use. on would have probably worked fine, but this leaves less mess behind.
    • The click handler calls fadeOut on both the passed ID, and any elements that have that have that ID as their data-father attribute - effectively, ID's children. For Fathers, this hides the Father and the Sons. For Sons it only hides the Son since no elements will have data-father attributes matching the Son's ID.
    • Finally, we close the dialog.

I hope this is all pretty clear. Happy Hacking!

Upvotes: 1

GGio
GGio

Reputation: 7653

I would change the structure of html little bit.

Why dont you add sons to be contained under father element. For example:

<tr id="father_1">
  <td class="father_1">Father 1</td>
  <td class="son_of_f1" id="son_1">Son 1</td>
  <td class="son_of_f1" id="son_2">Son 2</td>
  <td class="son_of_f1" id="son_3">Son 3</td>
  <td class="son_of_f1" id="son_4">Son 4</td>
</tr>

Then you can do following with JQuery

 $('#father_1').fadeOut(500);
 $('.son_of_f1').fadeOut(500);` 

and you can make _1 to be dynamic and save it into variable

NOTE: using classes rather than IDS for each son is better since you cant have two sons have the same id.

Upvotes: 0

Related Questions