jquery按字段过滤"删除"

时间:2018-02-02 11:29:18

标签: jquery

我在尝试显示标记为"已删除的注册表时出现问题"在我的客户表中。当我从组合中选择值&#34时,我想动态选择这些行;是"或"不"使用jquery。你能帮帮我吗?

<table id="customer_data" class="table table-striped table-bordered">  
<h3>Customer</h3>                           
 <thead>
   <tr>  
      <td class="tiles">ID</td>  
      <td class="titles">NAME</td>  
      <td class="titles">PHONE</td>  
      <td class="titles">DELETED</td>  
      <td class="titles" width="10%"><a width href ="customer_management.php"       class="button_new">New</a></td>
   </tr>  
 </thead>                            

<?php 
while($row= mysqli_fetch_array($res))
{
?>                          
<tr>                                     
  <td width="10%"><?php echo $row ['memo'];?></td>
  <td width="50%"><?php echo $row ['name'];?></td>
  <td width="30%"><?php echo $row ['phone'];?></td>
  <td width="10%">

  <?php if ($row['DELETED']==0){echo 'NO';} else{echo 'YES';}?>
  </td>                                    
  <td width="10%"><a href ="customer_management.php?id=<?php echo $row['ID'];   ?>&oper=mod" class="button">MODIFY</a></td>
</tr>
<?php
}  
?>  

</table> 

<!-- COMBO WITH 2 VALUES, YES OR NOT, TO SELECT THE DELETED CUSTOMERS OR NOT -->

<label for="show_deleted">Show deleted customers:</label>
<select id="show_deleted" name="show_deleted">                        
<?php
    echo "<option SELECTED value='0'>NO" ;
    echo "<option value='1'>YES" ;
?>            
</select>

$(document).ready(function(){      
    $('#customer_data').DataTable();      
});  
</script>

1 个答案:

答案 0 :(得分:1)

在“已删除:是/否 - td”中添加一个类,以便更轻松地选择它(例如class="td-deleted):

<td class="td-deleted" width="10%">
  <?php if ($row['DELETED']==0){echo 'NO';} else{echo 'YES';}?>
</td>  

然后你可以使用这样的代码:

$(function() {
  hide_deleted_rows();
  $('#show_deleted').change(function(){
    if ($(this).val()=='0'){
      hide_deleted_rows();
    }else{
      $('#customer_data').find('tr').each(function(){
        $(this).show();
      });
    } 
  });
});

function hide_deleted_rows(){
  $('#customer_data').find('tr').each(function(){
    if($(this).find('.td-deleted').html()=="YES"){
      $(this).hide();
    }
  });
}

JSFIDDLE