javascript Bootstrap表,包含:筛选列和可折叠行

ht4b089n  于 2023-05-27  发布在  Java
关注(0)|答案(1)|浏览(174)

在这个Django项目中,我试图创建一个Bootstrap Table,其中包含可折叠的行和过滤器列。我已经成功地使可折叠的行完美地工作,使用这个:

<tr data-toggle="collapse" data-target="#hidden-row-{{ post.id }}">
<script>
document.addEventListener('DOMContentLoaded', function() {
  var rows = document.querySelectorAll('.table tbody tr[data-toggle="collapse"]');
  rows.forEach(function(row) {
    row.addEventListener('click', function() {
      var target = this.getAttribute('data-target');
      var hiddenRow = document.querySelector(target);
      var display = window.getComputedStyle(hiddenRow).display;
      hiddenRow.style.display = (display === 'none') ? 'table-row' : 'none';
    });
  });
});
</script>

但是过滤器列不起作用,我很好奇这些脚本和类是否相互冲突,不能一起使用?
这是完整的代码:

<div class="container">
    <div class="table-wrapper">
        <div class="col-md-12">
            <div class="panel-heading">
                <h4 class="jy-text">Current Job Posts</h4>
            </div>
            <div class="table-responsive">
            
            <link href="https://unpkg.com/bootstrap-table@1.21.4/dist/bootstrap-table.min.css" rel="stylesheet">

            <script src="https://unpkg.com/bootstrap-table@1.21.4/dist/bootstrap-table.min.js"></script>
            <script src="https://unpkg.com/bootstrap-table@1.21.4/dist/extensions/filter-control/bootstrap-table-filter-control.min.js"></script>
                <table class="table table-sm" id="postTable" data-filter-control="true" data-show-search-clear-button="true">
                    <thead>
                        <tr>
                            <th scope="col"><h6 class="jy-text">Detail<h6></th>
                            <th scope="col"><h6 class="jy-text">Date<h6></th>
                            <th scope="col"><h6 class="jy-text">Title<h6></th>
                            <th scope="col"><h6 class="jy-text" data-filter-control="select">Position<h6></th>
                            <th scope="col"><h6 class="jy-text">Type<h6></th>
                            <th scope="col"><h6 class="jy-text">Location<h6></th>
                            <th scope="col"><h6 class="jy-text">LOA<h6></th>
                        </tr>
                    </thead>
                    <tbody>
                    {% for post in post_list %}
                        <tr data-toggle="collapse" data-target="#hidden-row-{{ post.id }}">
                            <td>
                                <button class="btn btn-default btn-xs"><p class="jy-text">Detail</p></button>
                            </td>
                            <td scope="row"><p class="jy-text">{{ post.date_post }}</p></td>
                            <td scope="row"><p class="jy-text">{{ post.title }}</p></td>
                            <td scope="row"><p class="jy-text">{{ post.position }}</p></td>
                            <td scope="row"><p class="jy-text">{{ post.employment }}</p></td>
                            <td scope="row"><p class="jy-text">{{ post.location }}</p></td>
                            <td scope="row"><p class="jy-text">{{ post.vessel_size }}</p></td>
                        </tr>
                        <!-- Hidden Row -->
                        <tr id="hidden-row-{{ post.id }}" class="collapse">
                            <td colspan="7" class="hiddenRow">
                                <p class="jy-text">Description: {{ post.description }}
                                <p class="jy-text">Contact this job!</p>
                                <a href="mailto:{{ post.email }}"><i class="fa-solid fa-circle-phone fa-2xs"></i></a> <a href="tel:{{ post.phone }}"><i class="fa-solid fa-circle-envelope fa-2xs"></i></a>
                            </td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<script>
  $(function() {
    $('#postTable').bootstrapTable()
  })
</script>

<!-- Collapsable rows -->
<script>
document.addEventListener('DOMContentLoaded', function() {
  var rows = document.querySelectorAll('.table tbody tr[data-toggle="collapse"]');
  rows.forEach(function(row) {
    row.addEventListener('click', function() {
      var target = this.getAttribute('data-target');
      var hiddenRow = document.querySelector(target);
      var display = window.getComputedStyle(hiddenRow).display;
      hiddenRow.style.display = (display === 'none') ? 'table-row' : 'none';
    });
  });
});
</script>
xriantvc

xriantvc1#

我建议您使用Datatables(https://datatables.net/examples/styling/bootstrap5.html),而不是使用引导表。我希望这是有益的,不仅过滤器,你可以做分页,排序等。从上面的同一个链接查看更多示例。

相关问题