gpt4 book ai didi

javascript - 数据表搜索框

转载 作者:行者123 更新时间:2023-11-28 05:17:44 24 4
gpt4 key购买 nike

我想在我的数据表上进行搜索,但我希望将相应的搜索框定位在某个位置。我遇到了一些代码,我对其进行了更改以说明我的问题(您可以在下面的 fiddle 中查看)。基本上我有四个 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;
}

最佳答案

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

关于javascript - 数据表搜索框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39231527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com