2011-08-28 79 views
1

我不知道这甚至有可能,但我们会看到......删除元素,如果最后

我有一个表:

<table cellspacing="0" class="stripey"> 
      <thead> 
       <tr> 
        <td class="center"></td> 
        <td>Plan Name</td> 
        <td class="options">Options</td> 
       </tr> 
      </thead> 
      <tbody> 
       <?php 
       foreach ($result as $row) { 
       ?> 
       <tr class="<?php echo alternator('', 'odd'); ?>" id="record-<?php echo $row['id'];?>"> 
        <td class="center"></td> 
        <td><?php echo $row['name']; ?></td> 
        <td class="options"><img src="<?php echo base_url();?>inc/images/search.png"> <img src="<?php echo base_url();?>inc/images/pencil.png"> <a href="#" class="delete" title="delete"><img src="<?php echo base_url();?>inc/images/delete.png"></a></td> 
       </tr> 
       <?php 
       } 
       ?> 
      </tbody> 
     </table> 

,你可以看到,<tr>重复然而,对于许多在$result ...但我有一个形象让我删除的项目,所以,我写道:

$('a.delete').click(function() { 
    var parent = $(this).parents("tr:first"); 
    $.ajax({ 
     type: 'get', 
     url: '/plans/delete', 
     data: 'id='+ parent.attr('id').replace('record-', ''), 
     beforeSend: function() { 
      parent.animate({'backgroundColor':'#fb6c6c'},300); 
     }, 
     success: function(msg) { 
      parent.children('td').wrapInner('<div>'); 
      parent.children('td').children('div').slideUp(500,function() { 
      parent.remove(); 
      $('.stripey tr').removeClass('odd'); 
      $('.stripey tr:even').addClass('odd'); 

      }); 
     } 
    }); 
    return false;  
}); 

现在我想知道是什么,是如果有可能以某种方式告诉如何很多tr项目有tbody,所以如果有0,我可以用文本替换整个表格,说明没有。

+2

你不应该使用'GET '删除东西;使用'POST'或'DELETE'。 – icktoofay

回答

1

您可以使用jQuery的DOM对象的length属性...

if($('table.stripey tbody tr').length == 0) { 
    // do something 
} 
+0

非常好!谢谢 - 这工作完美... – Justin

+0

接受的答案呢? :) – gislikonrad

+0

@Justin:在这种情况下,请考虑正式投票或接受此答案。 ([FAQ](http://stackoverflow.com/faq#howtoask)。) –

0

您可以使用下面的行来获取定位符click事件处理程序中trs的总数。

var totalNoOfTrs = $(this).closest("tbody").children().length; 

$('a.delete').click(function() { 
    var parent = $(this).parents("tr:first"); 
    var $tr = $(this).closest("tr"); 
    $.ajax({ 
     type: 'post', 
     url: '/plans/delete', 
     data: 'id='+ parent.attr('id').replace('record-', ''), 
     beforeSend: function() { 
      parent.animate({'backgroundColor':'#fb6c6c'},300); 
     }, 
     success: function(msg) { 
      var $tbody = $tr.closest("tbody"); 
      $tr.remove(); 
      if($tbody.children().length > 0){ 
       $('.stripey tr').removeClass('odd'); 
       $('.stripey tr:even').addClass('odd'); 
      } 
      else{ 
       //Replace the table with appropriate message. 
       $('.stripey').replaceWith("<div>No rows to delete</div>"); 
      } 

     } 
    }); 
    return false;  
}); 

在上面的代码this指向锚定元件与class删除和最接近的tbody会给包含所有TRS然后TBODY的children()将给在其中的所有元件TR tbody元素。

+0

所以在我的情况下,我会这样做: var totalNoOfTRs = parent.parent()。children()。length;为了得到它..值得一试 – Justin

+0

你可以使用我的答案中的代码,因为它是。 '$(this).closest(“tbody”)'会给你'tbody'元素。 – ShankarSangoli

+0

我试过了,它会一直返回0,即使我在列表中有1个以上...注:它将需要在成功运行:function ... – Justin

0

您可以检查tbodychildrenlength

if($("tbody").children().length) { 
    console.log("There is still at least one row."); 
}else{ 
    console.log("There are no more rows."); 
}