如果状态为活动或非活动数据键,则隐藏或显示div

时间:2014-10-04 00:48:51

标签: jquery codeigniter

我需要根据状态ID切换隐藏或显示按钮。如果status为active = 1,则仅显示删除按钮。如果状态为非活动= 0,则仅显示重新激活的按钮。但是,如果显示状态为活动或非活动状态,则必须从表中引入相应的数据。

请参阅此帖子https://stackoverflow.com/questions/26187896/codeigniter-two-conditions-with-same-variable-breaking-statement 关于这个新问题的背景知识。在上一个问题中,我想用php解决我的问题。现在我已经使用Jquery使其更简单。但是,我在设置函数以切换隐藏和显示按钮时遇到了问题。

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

我应该如何设置此功能以达到我需要的效果。

<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>

2 个答案:

答案 0 :(得分:2)

首先,你不能在打破语义标记的DIV中包装TD。你必须把DIV放在TD里面。

其次,您不得在多个元素上使用相同的ID(标识符),这也会破坏语义标记。您应该阅读更多关于正确HTML标记以及使用Javascript来定位DOM元素的内容。

答案 1 :(得分:0)

你的一个问题是你的标记被破坏了!您不能将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("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>";

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

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

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