如果状态通过数据键处于活动或非活动状态,则隐藏或显示div [英] Hide or show div if status is active or inactive by data key

查看:90
本文介绍了如果状态通过数据键处于活动或非活动状态,则隐藏或显示div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据状态ID切换隐藏或显示按钮。如果状态为活动= 1,则仅显示删除按钮。如果状态为inactive = 0,则只显示重新激活按钮。但如果显示状态为有效或无效,则还必须从表中输入相应的数据。



请参阅此帖子代码:具有相同变量break语句的两个条件
用于此新问题的背景。在上一个问题,我想使用php解决我的问题。现在我已经诉诸于Jquery,使它更简单。但是,我无法设置该功能来切换隐藏和显示按钮。



我已经在标记中设置了我的数据,我认为是正确的。 p>

我应该如何设置这个函数来实现我需要的结果。

 < div class =container-dstyle =padding-left:0;& 
< div class =container-fluid>
< div class =row>
< div class =col-md-6 col-md-offset-3 section-header buffer-bottom-md>
< h1>用户< / h1>
< / div>
< / div>
<?php
/ * start a1:成功时显示删除消息* /
if($ this-> session-> flashdata('ses_user_deleted'))
{
?>
< div class =row>
< div class =col-md-12>
<?php echo $ this-> session-> flashdata('ses_user_deleted');?>
< / div>
< / div>
<?php
}
/ * end a1:成功时显示删除消息* /

<?php / * start:email search form - line no 23 to 43 * /?>
< div class =row>
< div class =col-md-4 col-md-offset-4 buffer-bottom-md> <?php echo validation_errors(); ?>
<?php
$ attributes = array('style'=>'padding:0;');
echo form_open(base_url('users / index'),$ attributes);
?>
< table width =100%cellpadding =5>
< tr>
< td><?php
$ data_email = array('name'=>'email_id','class'=>'form-control','placeholder'=&电子邮件');
echo form_input($ data_email);
?>< / td>
< / tr>
< tr>
< td class =text-center>< input class =btn btn-primary btn-blocktype =submitname =searchvalue =Search/>
<?php echo anchor(users / reset,reset);?>< / td>
< / tr>
< / table>
< / form>
< / div>
< / div>
<?php / * end:email search form * /?>
<?php echo $ this-> pagination-> create_links();?>
< table class =table>
< tr>
< td>< strong>用户ID< / strong>< / td>
< td>< strong>用户名< / strong>< / td>
< td>< strong>名称< / strong>< / td>
< td>< strong>公司< / strong>< / td>
< td>< strong>电话< / strong>< / td>
< td>< strong>手机< / strong>< / td>
< td>< strong>电子邮件< / strong>< / td>
< td>< strong>网站< / strong>< / td>
< td>< strong>状态< / strong>< / td>
< td>< / td>
<?php
if(isset($ show_all))
{
?>< td colspan = 2>< a class =btn btn-primary btn -blockhref =<?php echo base_url('users /')?>>只显示活动用户< / td><?php
}
else
{
?>< td colspan = 2>< a class =btn btn-primary btn-blockhref =<?php echo base_url('users / all')?>> ;包含已删除的用户< / td><?php
}

< / tr>
<?php
/ * start a2 - 显示每个用户的编辑链接 - 在第36行和第48行进行的更改* /

foreach($ users as $ row => ; $ value)
{

$ status =(html_escape($ value ['status'])== 0)? inactive:active;

echo< tr>
< td>< a title ='点击查看此用户的详细信息'href ='.base_url('Users / search /'。 html_escape($ value ['id']))。'>。html_escape($ value ['id'])。< / a>< / td>
< td> html_escape($ value ['username'])。< / td>
< td>。html_escape($ value ['firstName'])。.html_escape($ value ['surName'] )。< / td>
< td>。html_escape($ value ['company']) 'telephone'])。< / td>
< td>。html_escape($ value ['mobile'])< / td>
< td>。html_escape ($ value ['email'])。< / td>
< td>。html_escape($ value ['website'])< / td>
< td> ;。$ status。< / td>
< td>< a href =。 base_url(users / edit /。html_escape($ value ['id']))。 >编辑< / a>< / td>;
// echo $ status;死();


echo< td>< div id ='a'data-inactive ='deleted'>< a href =。 base_url('Users / delete /'。html_escape($ value ['id']));?> onclick ='return confirm(真的要删除此用户吗?)'<?php echo>删除< / a< / td>< / div>;

echo< td>< div id ='i'data-active ='active'>< a href =。 base_url('Users / reactivate /'。html_escape($ value ['id']));?> onclick ='return confirm(重新激活此用户?)'<?php echo>重新激活< / a< / td>< / div>;

echo< / tr>;
}



/ * end a2-显示每个用户的编辑链接* /
?>
< / table>
<?php echo $ this-> pagination-> create_links();?>
< div class =row>
< div class =col-md-12>
< hr>
< / div>
< / div>

< / div>
< / div>
< / div>
<?php / * start a3 - 调用所需的jquery和bootstrap.js文件以显示删除消息* /?>
< script src =http://code.jquery.com/jquery-1.11.0.min.js>< / script>
< script src =http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/js/bootstrap.js>< / script>
<?php
/ * end a3 - 调用必需的jquery和bootstrap.js文件以显示删除消息* /

< script>
// var mydata = $(a).data();

$(#i)。hide();

< / script>


解决方案

您的问题之一是您的标记已损坏!您不能将div作为< tr> 的直接后代,因此请将其移动到您的 td 并关闭您的< div !!!

  echo td>< div id ='active'data-inactive ='deleted'>< a href =。 base_url('Users / delete /'。html_escape($ value ['id']));?> onclick ='return confirm(真的要删除此用户吗?)'<?php echo>删除< / a< / td>< / div>; 

echo< td>< div id ='inactive'data-active ='active'>< a href =。 base_url('Users / reactivate /'。html_escape($ value ['id']));?> onclick ='return confirm(重新激活此用户?)'<?php echo>重新激活< / a< / td>< / div>;

此外,为了更优雅地设置变量,请尝试使用if / else语句:

  $ status =(html_escape($ value ['status'])== 0)? inactive:active; 

您遇到的另一个问题是您有多个ID实例。将活动/非活动更改为类或其他数据属性。


I need to toggle a hide or show button based on a status id. If status is active=1 then show delete button only. If status is inactive=0 then show only reactivated button. But if status active or inactive is shown it has to bring in the corresponding data as well from the table.

Please see this post Codeigniter: Two conditions with same variable breaking statement for background on this new question. In the previous question I wanted to solve my problem using php. Now I have resorted to Jquery to make it simpler. However, I am having trouble setting up the function to toggle the hide and show button.

I have already setup my data in the markup which I think is correct.

How should I setup this function to achieve the results I need.

<div class="container-d" style="padding-left:0;"> 
  <div class="container-fluid">
    <div class="row">
      <div class="col-md-6 col-md-offset-3 section-header buffer-bottom-md">
        <h1>Users</h1> 
      </div>
    </div>
      <?php 
     /*start a1 : show delete message on success*/
     if($this->session->flashdata('ses_user_deleted'))
     {
   ?>
   <div class="row">
     <div class="col-md-12">
      <?php echo $this->session->flashdata('ses_user_deleted');?>
     </div>
   </div>
   <?php
     }
     /*end a1 : show delete message on success*/
   ?>
   <?php /*start : email search form - line no 23 to 43*/?>
<div class="row">
  <div class="col-md-4 col-md-offset-4 buffer-bottom-md"> <?php echo validation_errors(); ?>
    <?php 
      $attributes = array('style' => 'padding:0;');
      echo form_open(base_url('users/index'), $attributes);
      ?>
    <table width="100%" cellpadding="5">
      <tr>
        <td><?php
             $data_email = array('name'  => 'email_id', 'class' =>'form-control', 'placeholder' =>'Email');
             echo form_input($data_email);
            ?></td>
      </tr>
      <tr>
        <td class="text-center"><input class="btn btn-primary btn-block" type="submit" name="search" value="Search"/ >
          <?php echo anchor("users/reset","reset");?></td>
      </tr>
    </table>
    </form>
  </div>
</div>
<?php /*end : email search form*/ ?>
    <?php echo $this->pagination->create_links();?>
    <table class ="table">
        <tr>
            <td><strong>User Id</strong></td>
            <td><strong>Username</strong></td>
            <td><strong>Name</strong></td>
            <td><strong>Company</strong></td>
            <td><strong>Telephone</strong></td>
            <td><strong>Mobile</strong></td>
            <td><strong>Email</strong></td>
            <td><strong>Website</strong></td>
            <td><strong>status</strong></td>
            <td></td>
            <?php
                if (isset($show_all))
                    {
                        ?><td colspan=2><a class="btn btn-primary btn-block" href="<?php echo base_url('users/')?>">Show only active users</td><?php
                    }
                else
                    {
                        ?><td colspan=2><a class="btn btn-primary btn-block" href="<?php echo base_url('users/all')?>">Include deleted users</td><?php
                    }
            ?>
        </tr>
    <?php 
    /*start a2 - show edit link for each user - changes made on line no36 and 48 */

      foreach($users as $row =>$value)
        {

               $status= (html_escape($value['status'])==0) ? "inactive" : "active";

              echo "<tr>
                      <td><a title = 'Click to view details of this user' href = '".base_url('Users/search/'.html_escape($value['id']))."'>".html_escape($value['id'])."</a></td>
                      <td>".html_escape($value['username'])."</td>
                      <td>".html_escape($value['firstName'])." ".html_escape($value['surName'])."</td>
                      <td>".html_escape($value['company'])."</td>
                      <td>".html_escape($value['telephone'])."</td>
                      <td>".html_escape($value['mobile'])."</td>
                      <td>".html_escape($value['email'])."</td>
                      <td>".html_escape($value['website'])."</td>
                      <td>".$status."</td>
                      <td><a href = " . base_url("users/edit/".html_escape($value['id'])) . ">Edit</a></td>";
                      //echo $status; die(); 


              echo "<td><div id='a' data-inactive='deleted'><a href = " . base_url('Users/delete/' . html_escape($value['id']));?> onclick = 'return confirm("Really delete this user?")'<?php echo ">Delete</a</td></div>";

              echo "<td><div id='i' data-active='active'><a href = " . base_url('Users/reactivate/' . html_escape($value['id']));?> onclick = 'return confirm("Reactivate this user?")'<?php echo ">Reactivate</a</td></div>";

              echo "</tr>";  
        }



        /*end a2- show edit link for each user*/
    ?>
    </table>
    <?php echo $this->pagination->create_links();?>
    <div class="row">
      <div class="col-md-12">
        <hr>
      </div>
    </div>

  </div>
  </div>
 </div>
 <?php /*start a3 - call required jquery and bootstrap.js file to show delete message*/?>
 <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
 <script src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/js/bootstrap.js"></script>
  <?php
  /*end a3 - call required jquery and bootstrap.js file to show delete message*/
  ?>
  <script>
 //var mydata = $( "a" ).data();

 $("#i").hide();

</script>

解决方案

One of your problems is that your markup is broken! You can't have a div as a direct descendent of a <tr>, so move it inside your td and close your <div!!!

echo "<td><div id='active' data-inactive='deleted'><a href = " . base_url('Users/delete/' . html_escape($value['id']));?> onclick = 'return confirm("Really delete this user?")'<?php echo ">Delete</a</td></div>";

echo "<td><div id='inactive' data-active='active'><a href = " . base_url('Users/reactivate/' . html_escape($value['id']));?> onclick = 'return confirm("Reactivate this user?")'<?php echo ">Reactivate</a</td></div>";

Also, for a more elegant way to set your variable, try this instead of your if/else statement:

$status= (html_escape($value['status'])==0) ? "inactive" : "active"; 

Another issue you are having is that you have multiple instances of an id. change that active/inactive to a class or another data property.

这篇关于如果状态通过数据键处于活动或非活动状态,则隐藏或显示div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆