简体   繁体   English

使用 columnDefs 的 dataTable 不起作用,如果使用它来隐藏特定的列和数据,然后将其隐藏的数据传递给数据库

[英]dataTable using columnDefs not working if using it to hide specific column and data then passes it's hidden data to database

What I did is I have a two checkbox each row.我所做的是每行有两个复选框。 The reason why I have two checkboxes is that the first checkbox is for my status so I used boolean (0,1) second checkbox inside my row is for the ID so that when I update my status it will go to the specific ID's.我有两个复选框的原因是第一个复选框用于我的状态,所以我在我的行中使用 boolean (0,1) 第二个复选框用于 ID,因此当我更新我的状态时,它将 go 到特定的 ID。 The problem is this.问题是这样的。 I don't know much about dataTables and I research and found that I can hide a column using css and configure it inside columnDefs or aocolumnDefs.我对dataTables了解不多,我研究并发现我可以使用css隐藏列并将其配置在columnDefs或aocolumnDefs中。 What I did was I hid the checkbox column for ID so that it won't look messed up.我所做的是我隐藏了 ID 的复选框列,这样它就不会看起来乱七八糟。 So let us say I have 20 data inside my dataTable, 1st page shows 1-10 of data and then when I updated those 10 items it is okay when pushing it to the server and inserting the data to the database, sadly when I tried to update second page it shows error, the $_POST variable inside my PHP won't read the hidden column and it's checkboxes that was checked.因此,假设我的数据表中有 20 个数据,第一页显示 1-10 个数据,然后当我更新这 10 个项目时,可以将其推送到服务器并将数据插入数据库,遗憾的是,当我尝试更新第二页它显示错误,我的 PHP 中的 $_POST 变量不会读取隐藏列,并且它的复选框已被选中。 I figured it out that the second page could not read the hidden column.我发现第二页无法读取隐藏列。 I am really confused on what to do.我真的很困惑该怎么做。 To elaborate more, I will display images of what my problem is.为了详细说明,我将显示我的问题的图像。 Thank you in advance for helping!提前感谢您的帮助!

Reference is >>>> this参考是>>>> 这里

Here is the first page image where the data's status are successfully updated to my database (this is the first page)这是数据状态成功更新到我的数据库的第一页图像(这是第一页)

在此处输入图像描述

Here is the second page and it does not update the status of data to the database because it does not fetch the value from the hidden column.这是第二页,它不会将数据状态更新到数据库,因为它不会从隐藏列中获取值。

在此处输入图像描述

The error that I received我收到的错误

在此处输入图像描述

Here is without hiding my ID column and it works.这里没有隐藏我的 ID 列,它可以工作。 The data are updated.数据已更新。

在此处输入图像描述

My whole code我的整个代码

 < script > //For checkboxes to pass the value to database $(document).ready(function() { $('.is_checked_status').on('click', function() { var checkAll = this.checked; // find the row var row = $(this).closest("tr"); // find the checkboxes in the row row.find('input.subCheckbox').each(function() { this.checked = checkAll; }); }); }); // Code for dataTable, solution for the bug during ascending and descending $(document).ready(function() { $("#dataTables").DataTable({ aaSorting: [ [0, 'asc'] ], bPaginate: true, bFilter: true, bInfo: true, bSortable: true, bRetrieve: true, aoColumnDefs: [{ "aTargets": [0], "bSortable": true }, { "aTargets": [1], "bSortable": true }, { "aTargets": [2], "bSortable": true } ] }); //.column(5).visible(false) }); // Separated this code due to prohibitions by dataTables.net $('#dataTables').DataTable({ aoColumnDefs: [{ aTargets: [5], "sClass": "hide_column" }] }); < /script>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="row"> <div class="col-lg-12"> <div class="row"> <div class="col-lg-12"> <div class="panel panel-default"> <div class="panel-heading"> Emailed Data for approval </div> <.-- /?panel-heading --> <div class="panel-body"> <div class="table-responsive"> <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables"> <thead> <tr> <th> Control Number </th> <th> Tools Specification </th> <th> Supplier </th> <th> <center> Status </center> </th> <th> <center> Select data to approve </center> </th> <th> <center> ID </center> </th> </tr> </thead> <;php $con->next_result(), $result = mysqli_query($con;"CALL GetAllRequestedTools()")? ?> <tbody> <;php while ($row = mysqli_fetch_assoc($result)) { echo "<tr>". echo "<td><a href='edit_tools_approval?php.ID=".$row['ID']:"' style='color;red.'>". $row['reg_input']; "</a></td>". echo "<td>". $row['reg_tools_spec']; "</td>". echo "<td>". $row['reg_supplier']; "</td>"? ?> <td> <center> <label data-id="<;php echo $row['ID']??>" class="statusButton <?php echo ($row['req_stats']): 'label-success'? 'label-danger'?>"> <?php echo ($row['req_stats']): 'Approved'? 'Pending'?> </label> </center> </td> <td> <center> <input name="chk_status[]" class="is_checked_status" type="checkbox" value=" <;php echo $row['req_stats']??>"> </center> </td> <td class="hide_me"> <center> <input name="chk_id[]" type="checkbox" class="subCheckbox" value="<;php echo $row['tools_req_id']??>"> </center> </td> <;php echo "</tr>"? } ?> </tbody> </table> </div> </div> </div> </div> </div> <br> <div class="col-lg-15"> <div class="form-group"> <button type="button" id="submitBtn" class="btn btn-success pull- right" data-toggle="modal" data-target="#myModal"> <span class="fa fa-check"></span> Update </button> </div> <div class="form-group"> <button type="button" id="submitBtn" class="btn btn-danger pull- right" data-toggle="modal" data-target="#cancelModal"> <span class="fa fa-times"></span> Cancel </button> </div> </div> </div> </div>

CSS code CSS代码

.hide_column {
    display: none;
}

Figured out that it cannot be done during pagination.发现它不能在分页期间完成。 It is because of the limit of pagination, example is if you have a data 1-10, only the 1-10 data is accepted and 11....n onwards it is disregarded despite of any logic.这是因为分页的限制,例如,如果您有数据 1-10,则仅接受 1-10 的数据,而从 11....n 开始,尽管有任何逻辑,都将被忽略。 That is why it is better to think of other logic for the UI这就是为什么最好为 UI 考虑其他逻辑

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

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