数据表搜索框

时间:2016-08-30 15:25:18

标签: javascript jquery html css

我想在我的Datatable上进行搜索,但我希望将respetive搜索框放在某个位置。我遇到了一些代码,我改变了所以它说明了我的问题(你可以在小提琴中查看它)。基本上我有四个div,位于表外,每个div用于搜索特定列。但是,我正在努力让它发挥作用。

非常感谢任何帮助!

这是JSFiddle

代码:

<div>
  <label>Name:</label>
  <input id="Name" type="text">
</div>
<div>
  <label>Position:</label>
  <input id="Position" type="text">
</div>
<div>
  <label>Office:</label>
  <input id="Office" type="text">
</div>
<div>
  <label>Age:</label>
  <input id="Age" type="text">
</div>
<br/>
<br/>
<table id="example" class="display" cellspacing="0" width="100%">
  <thead>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Age</th>
      <th>Start date</th>
      <th>Salary</th>
    </tr>
  </thead>
  <tfoot>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Age</th>
      <th>Start date</th>
      <th>Salary</th>
    </tr>
  </tfoot>
  <tbody>
    <tr>
      <td>Tiger Nixon</td>
      <td>System Architect</td>
      <td>Edinburgh</td>
      <td>61</td>
      <td>2011/04/25</td>
      <td>$320,800</td>
    </tr>
    <tr>
      <td>Garrett Winters</td>
      <td>Accountant</td>
      <td>Tokyo</td>
      <td>63</td>
      <td>2011/07/25</td>
      <td>$170,750</td>
    </tr>
    <tr>
      <td>Ashton Cox</td>
      <td>Junior Technical Author</td>
      <td>San Francisco</td>
      <td>66</td>
      <td>2009/01/12</td>
      <td>$86,000</td>
    </tr>
    <tr>
      <td>Cedric Kelly</td>
      <td>Senior Javascript Developer</td>
      <td>Edinburgh</td>
      <td>22</td>
      <td>2012/03/29</td>
      <td>$433,060</td>
    </tr>
    <tr>
      <td>Gavin Cortez</td>
      <td>Team Leader</td>
      <td>San Francisco</td>
      <td>22</td>
      <td>2008/10/26</td>
      <td>$235,500</td>
    </tr>
    <tr>
      <td>Martena Mccray</td>
      <td>Post-Sales support</td>
      <td>Edinburgh</td>
      <td>46</td>
      <td>2011/03/09</td>
      <td>$324,050</td>
    </tr>
    <tr>
      <td>Unity Butler</td>
      <td>Marketing Designer</td>
      <td>San Francisco</td>
      <td>47</td>
      <td>2009/12/09</td>
      <td>$85,675</td>
    </tr>
    <tr>
      <td>Howard Hatfield</td>
      <td>Office Manager</td>
      <td>San Francisco</td>
      <td>51</td>
      <td>2008/12/16</td>
      <td>$164,500</td>
    </tr>
    <tr>
      <td>Donna Snider</td>
      <td>Customer Support</td>
      <td>New York</td>
      <td>27</td>
      <td>2011/01/25</td>
      <td>$112,000</td>
    </tr>
  </tbody>
</table>

JavaScript的:

$(document).ready(function() {
  // Setup - add a text input to each footer cell
  $('#example tfoot th').each(function() {
    var title = $(this).text();
    $(this).html('<input type="text" placeholder="Search ' + title + '" />');
  });
  // DataTable
  var table = $('#example').DataTable();
  // Apply the search
  table.columns().every(function() {
    var that = this;
    $('input', this.footer()).on('keyup change', function() {
      if(that.search() !== this.value) {
        that.search(this.value).draw();
      }
    });
  });
});

CSS:

tfoot input {
  width: 100%;
  padding: 3px;
  box-sizing: border-box;
}

1 个答案:

答案 0 :(得分:0)

你需要像DataTables的YADCF过滤插件。它允许您使用外部输入控件过滤列。