【问题标题】:Use jQuery to search value in a Bootstrap 4's Table inside a collapse使用 jQuery 在折叠内的 Bootstrap 4 的表中搜索值
【发布时间】:2020-02-25 14:07:12
【问题描述】:

我在折叠内使用 Bootstrap 4 表,我想在表中搜索一个值,如果找到该值,打开折叠并向我显示包含该值的行。

<div id="collapseStudents" class="collapse ml-4" aria-labelledby="headingOne" data-parent="#accordionTypes">
<table class="table table-striped table-borderless">
    <thead>
        <tr>
            <th scope="col" class="text border-bottom">N.</th>
            <th scope="col" class="text border-bottom">Classe</th>
            <th scope="col" class="text border-bottom">Nome e Cognome</th>
            <th scope="col" class="text border-bottom">Ruoli</th>
        </tr>
    </thead>
    <tbody>
        {% for number, data in students_table.items() %}
        <tr>
            <th scope="row" class="text">{{number}}</th>
            <td class="text"><span class="badge badge-secondary">{{data['class']}}</span></td>
            <td class="text">{{data['name']}}</td>
            <td class="text roles" style="width: 30%">
                <span class="badge badge-danger align-text-top ml-1 admin-chip" style="display: {{none if not 'admin' in data['roles'] else block}}; position: relative; top: 2px;">Admin <a class="text-light chip" href=""><i class="fa fa-times" aria-hidden="true" style="position:relative;top:0.3px" data-role="admin" data-user="{{data['name'] + '-' +number}}"></i></a></span>
            </td>
        </tr>
        {% endfor %}
    </tbody>
</table>

【问题讨论】:

    标签: javascript jquery html bootstrap-4 bootstrap-table


    【解决方案1】:

    您可以尝试以下脚本:

    $('.table tbody').find('tr').each(function(){
      $(this).find('td').each(function(){
        if($(this:contains('valueToSearch'))){
          $('#collapseStudents').collapse();
          // here you can set how to show the row, ex. as bold text
          $(this).text('<b>' + $(this).text() + '</b>');
        }
      });
    });
    

    【讨论】:

    • 尝试使用 $(this).text() 将 :contains 替换为更简单的指令
    • 如果我有多个折叠,如何仅切换包含我的值的表的折叠?
    • 给单个元素一个唯一的 id 并用 $('#id') 选择,或者用 $('.className').first() 或 last() 选择目标
    • 你能给我一个完整的例子吗?
    • $('#collapse2')。折叠();
    猜你喜欢
    • 2017-01-31
    • 1970-01-01
    • 2016-09-10
    • 2019-03-09
    • 2021-01-26
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多