我是jquery新手,正在使用datatable动态创建一个表。我已经能够创建一个datatable,并且能够添加这样的过滤功能,但是我还不能在我的代码中使用类似excel的过滤选项(如下面的示例)。
我发现了一些额外的插件,它们可以提供类似的功能,但还没有找到一种手动实现的方法。有没有办法不用额外的插件就可以手动完成?
工作示例:
例1
例2
我的代码:
// Create search header
var new_row = $("<tr class='search-header'/>");
$('#example thead th').each(function(i) {
var title = $(this).text();
var new_th = $('<th style="' + $(this).attr('style') + '" />');
$(new_th).append('<input type="text" placeholder="' + title + '" data-index="' + i + '"/>');
$(new_row).append(new_th);
});
$('#example thead').prepend(new_row);
// Init DataTable
var table = $('#example').DataTable({
"scrollX": true,
"searching": true,
});
// Filter event handler
$(table.table().container()).on('keyup', 'thead input', function() {
table
.column($(this).data('index'))
.search(this.value)
.draw();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.25/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.js"></script>
<table id="example" class="display nowrap" cellspacing="0" width="100%">
<thead>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
<th>Extn.</th>
<th>E-mail</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tiger</td>
<td>Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>$320,800</td>
<td>5421</td>
<td>t.nixon@datatables.net</td>
</tr>
<tr>
<td>Garrett</td>
<td>Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011/07/25</td>
<td>$170,750</td>
<td>8422</td>
<td>g.winters@datatables.net</td>
</tr>
<tr>
<td>Ashton</td>
<td>Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td>2009/01/12</td>
<td>$86,000</td>
<td>1562</td>
<td>a.cox@datatables.net</td>
</tr>
<tr>
<td>Cedric</td>
<td>Kelly</td>
<td>Senior Javascript Developer</td>
<td>Edinburgh</td>
<td>22</td>
<td>2012/03/29</td>
<td>$433,060</td>
<td>6224</td>
<td>c.kelly@datatables.net</td>
</tr>
<tr>
<td>Airi</td>
<td>Satou</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>33</td>
<td>2008/11/28</td>
<td>$162,700</td>
<td>5407</td>
<td>a.satou@datatables.net</td>
</tr>
<tr>
<td>Brielle</td>
<td>Williamson</td>
<td>Integration Specialist</td>
<td>New York</td>
<td>61</td>
<td>2012/12/02</td>
<td>$372,000</td>
<td>4804</td>
<td>b.williamson@datatables.net</td>
</tr>
</tbody>
</table>
暂无答案!
目前还没有任何答案,快来回答吧!