简体   繁体   English

使用jQuery在表行的div中显示/隐藏图标

[英]show/hide icons in div in table row using jquery

I have a table, its is data displayed using foreach. 我有一个表,它是使用foreach显示的数据。 one column have 3 icons, by default they are hidden. 一栏包含3个图标,默认情况下它们是隐藏的。 And also have checkbox. 并且也有复选框。 icons and checkbox are in every row. 图标和复选框在每一行中。 Now when select any checkbox, icons of all rows are appearing. 现在,选中任何复选框时,将显示所有行的图标。 Instead I want only one row icons should be displayed on selecting checkbox. 相反,我希望在选择复选框时只显示一行图标。 Can somebody help please. 请有人帮忙。 thanks in advance. 提前致谢。 here is the code. 这是代码。 in view 鉴于

<?php $sn= $this->uri->segment(4); foreach($companies as $company){ $sn++;?>
   <tr class="parent" style="font-size:11px;color:#3D4061;border-bottom:1px solid #3d406136">
      <td><input type="checkbox" class="text-center check_data mt-2" onchange="changevalue()"></td>
      <td class="text-center" style=""><h6><?php echo $sn; ?></h6></td>
      <td class="text-center" style=""><h6><?php echo $company->company_name; ?></h6></td>
      <td class="text-center" style=""><h6><?php echo $company->contact_no; ?></h6></td>
      <?php if($company->activation_status == 1){?>
        <td class="text-center" style=""><h6 class="text-success font-weight-bold" style="font-size:13px">Active</h6></td>
      <?php } ?>
      <?php if($company->activation_status == 0){?>
        <td class="text-center" style=""><h6 class="text-danger font-weight-bold" style="font-size:13px">Inactive</h6></td>
      <?php } ?>
      <td class="text-center" style="">
        <div class="status">
          <a href="<?php echo base_url().'superadmin/company_ctrl/edit_company/'.$company->id; ?>" data-toggle="tooltip" data-placement="top" title="EDIT" class="f13"><i class="fa fa-edit"></i></a>
          <?php if($company->activation_status == 0){?>
            <a href="<?php echo base_url().'superadmin/company_ctrl/activate_company/'.$company->id; ?>" data-toggle="tooltip" data-placement="top" title="ACTIVE" class="f13"><i class="fa fa-dot-circle-o"></i></a>
          <?php }else{ ?>
            <a href="<?php echo base_url().'superadmin/company_ctrl/deactivate_company/'.$company->id; ?>" data-toggle="tooltip" data-placement="top" title="INACTIVE" class="f13"><i class="fa fa-exclamation-triangle"></i></a>
          <?php } ?>
            <a href="<?php echo base_url().'superadmin/company_ctrl/delete_company/'.$company->id; ?>" data-toggle="tooltip" data-placement="top" title="DELETE" class="f13 delete_row"><i class="fa fa-trash-alt"></i></a>
        </div>
       </td>
       <td ><i class="fa fa-chevron-down down" ></i></td>
   </tr>

and script is 和脚本是

  <script>
  $('.status').hide();
    function changevalue(){
      if($(".check_data").is(":checked"))
      $('.status').show();
      else
      $(".status").hide();
      $('.cchild').hide();
    }
</script>

You can give dynamic Ids to every checkbox and status div. 您可以为每个复选框和状态div赋予动态ID。 Use your record's id may be companyId to create Ids like 使用记录的ID可能是companyId来创建ID,例如

<input type="checkbox" id="checkbox_('<?php echo $company->id; ?>')>

I don't know the syntax for php. 我不知道php的语法。

and then you can pass the current companyId in onchange="changevalue(companyId)" 然后您可以在onchange="changevalue(companyId)"传递当前的onchange="changevalue(companyId)"

and then your code will be like this 然后您的代码将像这样

function changevalue(companyId){
      if($("#checkbox" + companyId).is(":checked"))
      $('#status' + companyId).show();
      else
      $("#status" + companyId).hide();
    }

you can also use .parent() and .find() methods to get specific row like that: 您还可以使用.parent().find()方法来获取特定行,如下所示:

<input type="checkbox" function="changevalue(this)">

and your javacript must be like that: 并且您的javacript必须是这样的:

function changevalue(elem){
  if($(elem).is(":checked"))
  $(elem).parent().parent().find('.status').show();
  else
  $(elem).parent().parent().find('.status').hide();
}

This code can be simplified but it's a good starting example to understand parent-child usage of html elements. 这段代码可以简化,但这是理解html元素parent-child用法的一个很好的入门示例。 At this example first .parent() goes to <td> of checkbox element and second .parent() goes to <tr> element then it searches for .status class in it then hides it. 在此示例中,第一个.parent()移至复选框元素的<td> ,第二个.parent()移至<tr>元素,然后在其中搜索.status类,然后将其隐藏。

and you can simplfy it like that: 您可以这样简化:

function changevalue(elem){
  if($(elem).is(":checked"))
  $(elem).parents("tr").find('.status').show();
  else
  $(elem).parents("tr").find('.status').hide();
}
function changevalue() {
    var $this = $(this),
        $status = $this.parents("tr").find(".status");

    if ($this.is(":checked")) {
        $status.show();
    } else {
        $status.hide();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM