首页 > 解决方案 > 如何在模式内提交时隐藏表格列

问题描述

在用户个人资料页面上,我有一个表格,其中标题不在顶部而是在左侧,而描述在右侧。

当用户单击“编辑个人资料”时,会打开一个模式。

在模态中,我想隐藏表格列,或者在这种情况下我猜是通过选择复选框然后点击“保存更改”按钮来选择行。单击“保存更改”时,他们的选择也应保存并更新到他们的个人资料中。

我还希望在他们注册后第一次访问他们的个人资料页面时已经检查了一些复选框。然后他们应该能够在那里改变它。

我的代码不起作用,我尝试了所有可以在隐藏列中找到的方法......我已经找了好几天了。任何帮助深表感谢!

TABLE
<div class="row">
<table class="table table-responsive-sm" id="profileTable" id="table-pagination" cellspacing="0" style="width:100%" >
              <tbody>
                  <?php



          $sql = "SELECT `dancing_since`, `dancer_dob`, `school`, `school_code`,`teacher_name`, `class_location`, `date_enrolled`, `date_firstdance`, `first_dance_name`, `current_lvl` FROM `dancers` WHERE name = '$name' AND dancer_name = '$dancerName'";
          $res = mysqli_query($con,$sql); 

          //if($res==FALSE){
          //die('there was an error running query [' . $con->error . ']');
          //  }

          while($row=mysqli_fetch_array($res)){
              echo '
              <tr>
              <th class="tcol1">Dancing Since:</th>
              <td class="tcol1">'.$row["dancing_since"].'</td>
              </tr>

              <tr>
              <th class="tcol2">D.O.B:</th>
              <td class="tcol2">'.$row["dancer_dob"].'</td>
              </tr>


              <tr>
              <th class="tcol3">School:</th>
              <td class="tcol3">'.$row["school"].'</td>
              </tr>

              <tr>
              <th class="tcol4">School Code:</th>
              <td class="tcol4">'.$row["school_code"].'</td>
              </tr>

              <tr>
              <th class="tcol5">Teachers Name:</th>
              <td class="tcol5">'.$row["teacher_name"].'</td>
              </tr>

              <tr>
              <th class="tcol6">Class Location:</th>
              <td class="tcol6">'.$row["class_location"].'</td>
              </tr>

              <tr>
              <th class="tcol7">Date Enrolled:</th>
              <td class="tcol7">'.$row["date_enrolled"].'</td>
              </tr>

              <tr>
              <th class="tcol8">First Feis</th>
              <td class="tcol8">'.$row["first_dance_name"].'</td>
              </tr>

              <tr>
              <th class="tcol9">Date of First Feis:</th>
              <td class="tcol9">'.$row["date_firstdance"].'</td>
              </tr> 

              <tr>
              <th class="tcol10">Current Level:</th>
              <td class="tcol10">'.$row["current_lvl"].'</td>
              </tr> 
              ';  
    }

    ?>
</tbody>

模态

<!--POPUP MODAL FORM-->
                        <div class="formModal">
                            <div class="formModal text-right">
                                <button type="button" class="btn btn-outline-primary editprofile-btn" data-toggle="modal" data-target=".bd-editprofile-modal-lg">Edit Profile</button>
                            </div>


                            <!-- Modal -->

                            <div class="modal fade bd-editprofile-modal-lg" id="editProfile" tabindex="-1" role="dialog" aria-labelledby="editProfile" aria-hidden="true">
                                <div class="modal-dialog modal-dialog-centered modal-success modal-lg" role="document">
                                    <div class="modal-content">
                                        <form id="updateForm" method="post" class="form-horizontal" name="tcol" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" role="form" data-toggle="validator">
                                            <div class="controls">

                                                <!--Modal cascading tabs-->
                                                <div class="modal-c-tabs">

                                                    <!-- Nav tabs -->
                                                    <ul class="nav nav-tabs tabs-2 light-blue darken-3" role="tablist">
                                                        <li class="nav-item">
                                                            <a class="nav-link active" data-toggle="tab" href="#panel7" role="tab">Edit Profile Information</a>
                                                        </li>
                                                        <li class="nav-item">
                                                            <a class="nav-link" data-toggle="tab" href="#panel8" role="tab">Change visiblity</a>
                                                        </li>
                                                    </ul>

                                                    <!-- Tab panels -->
                                                    <div class="tab-content">

                                                        <!--Panel 7-->
                                                        //some code for edit profile form



                                                        <!-- PANEL 8 -->

                                                        <div class="tab-pane fade" id="panel8" role="tabpanel">
                                                            <div class="modal-body">
                                                                <div class="container-fluid">
                                                                    <div class="panel-8-heading text-center">
                                                                        <b>Select up to 10 abilities you would like to appear on your profile.</b>
                                                                    </div>
                                                                    <br>

                                                                    <div class="row">
                                                                        <div class="col-sm-4">
                                                                            <input type="checkbox" name="col1" class="hidecol" id="col_1" data-column="0"/> Dancing Since
                                                                        </div>

                                                                        <div class="col-sm-4">
                                                                            <input type="checkbox" name="col2" class="hidecol" id="col_2" checked="checked" data-column="1" /> D.O.B
                                                                        </div>

                                                                        <div class="col-sm-4">
                                                                            <input type="checkbox" name="col3" class="hidecol" id="col_3" data-column="2"/> School
                                                                        </div>      
                                                                    </div>

                                                                    <div class="row">
                                                                        <div class="col-sm-4">
                                                                            <input type="checkbox" name="col4" class="hidecol" id="col_4" checked="checked" data-column="3"/>  School Code
                                                                        </div>

                                                                        <div class="col-sm-4">
                                                                            <input type="checkbox" name="col5" class="hidecol" id="col_5" checked="checked" data-column="4"/>  Teacher's Name
                                                                        </div>

                                                                        <div class="col-sm-4">
                                                                            <input type="checkbox" name="col6" class="hidecol" id="col_6" checked="checked" data-column="5"/> Class Location
                                                                        </div>
                                                                    </div>

                                                                    <div class="row">
                                                                        <div class="col-sm-4">
                                                                            <input type="checkbox" class="hidecol" name="col7" id="col_7" checked="checked" data-column="6"/> Date Enrolled
                                                                        </div>

                                                                        <div class="col-sm-4">
                                                                            <input type="checkbox" class="hidecol" name="col8" id="col_8" data-column="7" /> First competition
                                                                        </div>

                                                                        <div class="col-sm-4">
                                                                            <input type="checkbox" class="hidecol" name="col9" id="col_9" checked="checked" data-column="8"/> Date of First dance
                                                                        </div>
                                                                    </div>

                                                                    <div class="row">
                                                                        <div class="col-sm-4">
                                                                            <input type="checkbox" class="hidecol" name="col10" id="col_10" data-column="9"  /> Current Level
                                                                        </div>
                                                                        <div class="col-sm-4">
                                                                        </div>
                                                                        <div class="col-sm-4">
                                                                        </div>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>

                                                        <!-- PANEL 8 END -->
                                                    </div>
                                                </div>

                                                <!-- MODAL FOOTER -->
                                                <div class="modal-footer">
                                                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                                    <button type="submit" class="btn btn-primary" name="save" id="save">Save changes</button>
                                                </div>
                                            </div>
                                        </form>
                                    </div>
                                </div>

查询

$(document).ready(function() {


//modal checkbox


$('#profileTable tr td:nth-child(3)').hide();
});

编辑

下面是用户表和带有复选框的模式的图像。我希望能够隐藏整行。因此,如果用户取消选择位置,那么在他们的个人资料中,他们将不再看到“位置:NJ”

简介表

配置文件表

模态 模态

标签: javascriptphpjqueryhtmlcss

解决方案


我不知道这只是代码的摘录还是错误,因为我在模态中找不到任何<table>定义。从您的 jquery 代码中,您需要一个带有 id 的元素profileTable

我建议您说明您尝试过的内容以及无效的内容。您也可以将问题划分为较小的规模并确定根本原因。例如尝试模态外的隐藏/显示机制,或分解 jquery 代码(如果$('#profileTable tr td:nth-child(3)')不返回则.hide()无效)

更新 1

我认为您正在尝试隐藏表格列(垂直列)。首先,该表有 2 个 id,profileTable并且table-pagination. 删除其中任何一个,使表只有一个 id。由于#是一个 id 选择器,多个 id 可能会弄乱 css。其次,让我们把“模态”放在一边。浏览器对模态一无所知,它只知道有一堆不同样式的嵌套 html 元素。无论有没有模态,javascript 都应该以相同的方式工作。它可以帮助您分解模型并更轻松地找到根本原因。

我根据您提供的代码段制作了一个演示。底部的两个按钮显示如何隐藏和显示列。

https://codepen.io/anon/pen/oMpRbq

更新 2

了解需求并更新演示。隐藏/显示行而不是列。概念差不多,不同的是选择tr而不是td。我只做 2 个复选框,因为它们都是一样的。

$('#filter-panel').on('change', 'input', function(e) {
  var rowIndex = $(e.currentTarget).val();

  $('#profileTable tr:nth-child(' + rowIndex + ')').toggle();

});
table {
  border: 1px solid black;
  border-collapse: collapse;
}

table td,
table th {
  border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
  <table class="table table-responsive-sm" id="profileTable" cellspacing="0" style="width:100%">
    <tbody>
      <tr>
        <th class="tcol1">Dancing Since:</th>
        <td class="tcol1">'.$row["dancing_since"].'</td>
      </tr>

      <tr>
        <th class="tcol2">D.O.B:</th>
        <td class="tcol2">'.$row["dancer_dob"].'</td>
      </tr>

      <tr>
        <th class="tcol3">School:</th>
        <td class="tcol3">'.$row["school"].'</td>
      </tr>

      <tr>
        <th class="tcol4">School Code:</th>
        <td class="tcol4">'.$row["school_code"].'</td>
      </tr>

      <tr>
        <th class="tcol5">Teachers Name:</th>
        <td class="tcol5">'.$row["teacher_name"].'</td>
      </tr>

      <tr>
        <th class="tcol6">Class Location:</th>
        <td class="tcol6">'.$row["class_location"].'</td>
      </tr>

      <tr>
        <th class="tcol7">Date Enrolled:</th>
        <td class="tcol7">'.$row["date_enrolled"].'</td>
      </tr>

      <tr>
        <th class="tcol8">First Feis</th>
        <td class="tcol8">'.$row["first_dance_name"].'</td>
      </tr>

      <tr>
        <th class="tcol9">Date of First Feis:</th>
        <td class="tcol9">'.$row["date_firstdance"].'</td>
      </tr>

      <tr>
        <th class="tcol10">Current Level:</th>
        <td class="tcol10">'.$row["current_lvl"].'</td>
      </tr>
    </tbody>
  </table>
  <br /><br />

  <div id="filter-panel">

    <input type="checkbox" name="vehicle" value="1" checked/> dancing since
    <input type="checkbox" name="vehicle" value="2" checked/> d.o.b

  </div>


推荐阅读