tags:

views:

387

answers:

3

i have an table

<table class="oldService">
    <thead>
           <th>name</th>
           <th>age</th>
           <th>action</th>
    </thead>    
    <tbody>
    <?php foreach($array as $k=>$v){ ?>       
          <tr>
              <td><?php echo $k[name] ?></td>
              <td><?php echo $k[age]?></td>
              <td id="<?php $k[id]" class="delme">X</td>
          </tr>
    <?php } ?>        
    </tbody>
<table>

now i want to delete any row by clicking on X of each row except first and last row, and also need to confirm before deletion.

i used below jquery

<script type="text/javascript">
jQuery(document).ready(function(){
    jQuery('table.oldService>tbody tr').not(':first').not(':last').click(function(){
    if(confirm('want to delete!')){
    jQuery(jQuery(this).addClass('del').fadeTo(400, 0, function() { 
            jQuery(this).remove()}));
    jQuery.get('deleteService.php', {id:jQuery(this).attr('id')});
    }
    else return false;});
});

</script>

this is working perfect,but it execute by click on that row( means any td), i want that this event only occour when user click on X(third td) . please suggest me how to modify this jquery so that the event occur on click of X.

UPDATE: i want that minimum one row should be in tbody, means if there is only one row then no function to delete that row, and if there is many rows then any row he can delete but not the all rows.

+1  A: 

I didn't implement all your animation and AJAX logic, but here is how you could do this in general:

$(function() {
    $('table.oldService td.delme:not(:first):not(:last)').click(function() {
       $(this).closest('tr').remove(); 
    });
});

Also, I highly recommend you run your code through JSLint. The code sample you posted has a massive number of errors in it.

Jimmy Cuadra
@jitter Please verify what you're saying before downvoting unfairly. My selector does not select what you suggest. It selects all table cells with the class `delme`, except for the first and last ones (which appear in the first and last table rows.) `delme` is applied to a `td`, not a `tr`, as you state. Read his HTML again. What I wrote works for me just fine. See for yourself: http://jsbin.com/iyumo
Jimmy Cuadra
where is confirm box code?
diEcho
A: 

Try this :

    <script type="text/javascript">
        jQuery(document).ready(function(){
    jQuery('table.oldService>tbody tr img.delete').not(':first').not(':last').click(function () {
        if(confirm('want to delete!'))
        {
          jQuery(jQuery(this).addClass('del').fadeTo(400, 0, function() { 
                jQuery(this).parent().parent().remove()}));
          jQuery.get('deleteService.php', {id:jQuery(this).attr('id')});
        }
        else return false;});
    });
</script>

html :

<table class="oldService">
        <thead>
               <th>name</th>
               <th>age</th>
               <th>action</th>
        </thead>    
        <tbody>
        <?php foreach($array as $k=>$v){ ?>       
              <tr>
                  <td><?php echo $v['name'] ?></td>
                  <td><?php echo $v['age']?></td>
                  <td><img id="<?php echo $v['id']?>" class="delete" src="del.gif" /></td>
              </tr>
        <?php } ?>        
        </tbody>
<table>
bader
+1  A: 

You can do this with a bit less code like this:

jQuery('table.oldService').delegate('td.delme', 'click', function() {
    if(jQuery(this).parent().siblings(":not(.del)").length === 0) return;
    if(!confirm('want to delete!')) return;

    jQuery.get('deleteService.php', { id:this.id });
    jQuery(this).parent().addClass('del').fadeTo(400, 0, function() {
        jQuery(this).remove();
    });
});​

This attaches one event handler for the table instead of 1 per <td>. First we check if there are any siblings left that aren't deleted (prevent last deletion). Then check if they confirm, then delete the row.

You also need a few html fixes based on the posted question. The last tag needs to be </table> instead of <table>, and those <td> elements in the <thead> need to be wrapped in <tr></tr>. Let me know if you have any trouble, you can see a working demo of this here.

Nick Craver
@NIck..Your code is working perfect in the demo but not working in my page,it delete each row in my page, will u plz elaborate above code! what means of .del in `siblings(":not(.del)").length` ?
diEcho
@I Like PHP - When deleting we're adding the `"del"` class to the row, this checks to make sure there this isn't the last row without that class (no siblings rows without it), meaning it's the only one not deleted yet, make sense?
Nick Craver
is there no function named (delagate) in jquery 1.3.2??
diEcho
it's still not working dear?? it delete all rows:(
diEcho
@I Like PHP - This is for 1.4.2, for 1.3.2 replace the first line with: `jQuery('table.oldService td.delme').live('click', function() {`
Nick Craver
Thankyou, i solved by you tip , actually there is a tr where is a input type button in my table, thats y complete data rows were deleting.
diEcho