简体   繁体   English

在标题(th)上添加按钮以在引导表中隐藏和显示过滤器控件

[英]Add button on header (th) to hide and show filter control in bootstrap table

I am using bootstrap table with filter control.我正在使用带有过滤器控件的引导表。 I want to be able to add a button(icon) on each of my th's to hide and show the filter control.我希望能够在我的每个 th 上添加一个按钮(图标)来隐藏和显示过滤器控件。

Is there a way to do that?有没有办法做到这一点?

This is an example on bootstrap table: https://live.bootstrap-table.com/example/extensions/filter-control.html这是引导表的示例: https : //live.bootstrap-table.com/example/extensions/filter-control.html

As you can see the filter control takes up a lot of place on the header.如您所见,过滤器控件在标题上占据了很多位置。 I just wanted to see if it was possible to only add the filter control by adding an icon on the header that adds the filter control on click of that icon.我只是想看看是否可以通过在标题上添加一个图标来添加过滤器控件,该图标在单击该图标时添加过滤器控件。

My code:我的代码:

<table
  id="table"
  data-url="json/data1.json"
  data-filter-control="true"
  data-show-search-clear-button="true">
  <thead>
    <tr>
      <th data-field="id">ID</th>
      <th data-field="name" data-filter-control="input" data-sortable="true">Item Name</th>
      <th data-field="price" data-filter-control="select" data-sortable="true">Item Price</th>
    </tr>
  </thead>
</table>

JS JS

 $(function() {
    $('#table').bootstrapTable()
  })

When I try and add the append for the icon:当我尝试为图标添加附加时:

    $('#table thead th').each(function() {
    s = $('<span style="position: absolute; left: 4px; top: 8px; cursor: pointer;">?</span>')
    s.click(function(e) {
        e.stopPropagation();
        alert('Question mark clicked')
    });
    $(this).append(s)
  })

For starters, it only adds the icon on the first th and the second problem is that on click it, it sorts the data instead of giving me the dummy alert I set up to see if the click is working.对于初学者来说,它只是增加了第一个图标th和第二个问题是,点击它,它的数据,而不是各种各样的给我假警报我成立了,看看点击工作。

How do I add a clickable icon on the left without affecting the sorting?如何在不影响排序的情况下在左侧添加可点击的图标?

You can append span which you have created inside each loop under th-inner so that it will appear side by side of the header .Also , you can hide filter-control div this contains the filter which you need to use either input or select .So , whenever ?您可以在th-inner下的each循环中附加您创建的span ,以便它与标题并排显示。此外,您可以隐藏filter-control div,它包含您需要使用的过滤器inputselect 。那么,无论何时? is clicked just get reference of this div to show/hide same.单击只是获取此 div 的参考以显示/隐藏相同。

Demo Code :演示代码

 $(function() { $('#table').bootstrapTable() $(".filter-control").hide() //hide filter control $('#table thead th').each(function() { s = $('<span style="position: absolute;margin-left:10px">?</span>') s.click(function(e) { e.stopPropagation(); //get closest filter control div hide/show same $(this).closest("th").find(".filter-control").toggle(); }); //append span inside th-inner class $(this).find(".th-inner").append(s) }) })
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.8.1/bootstrap-table.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> <script src="https://unpkg.com/bootstrap-table@1.18.0/dist/bootstrap-table.min.js"></script> <script src="https://rawgit.com/wenzhixin/bootstrap-table/master/dist/extensions/filter-control/bootstrap-table-filter-control.js"></script> <table id="table" data-filter-control="true" data-show-search-clear-button="true"> <thead> <tr> <th data-field="id">ID</th> <th data-field="name" data-filter-control="input">Item Name</th> <th data-field="price" data-filter-control="select">Item Price</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>Item 1</td> <td>$1</td> </tr> <tr> <td>2</td> <td>Item 2</td> <td>$2</td> </tr> </tbody> </table>

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

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