2
我有一個簡單的jQuery Datatable,其中包含4列,其中一列是下拉列表。jQuery DataTable - 搜索一列下拉列表
<!-- HTML CODE -->
<body>
<table id="vendorListing">
<tfoot>
<tr>
<th class="searchBox">Vendor Location</th>
<th class="searchBox">Currency</th>
<th class="searchBox">Vendor Type</th>
<th class="searchBox">Vendor</th>
</tr>
</tfoot>
<thead>
<tr>
<th>Vendor Location</th>
<th>Currency</th>
<th>Vendor Type</th>
<th>Vendor</th>
</tr>
</thead>
<tbody>
<tr id="1">
<td>
<span id="vendorLocation_1" class="vendorLocation">New York</span>
</td>
<td>
<span id="vendorCurrency_1" class="vendorCurrency">American</span>
</td>
<td>
<span id="vendorCurrency_1" class="vendorCurrency">Steel</span>
</td>
<td>
<select id="vendorDropdown_1" class="vendorDropdown">
<option value="1" selected="selected">Vendor Name 1</option>
<option value="2">Vendor Name 2</option>
<option value="3">Vendor Name 3</option>
<option value="4">Vendor Name 4</option>
<option value="5">Vendor Name 5</option>
</select>
</td>
</tr>
<tr id="2">
<td>
<span id="vendorLocation_2" class="vendorLocation">Montreal</span>
</td>
<td>
<span id="vendorCurrency_1" class="vendorCurrency">Canadian</span>
</td>
<td>
<span id="vendorCurrency_1" class="vendorCurrency">Plastic</span>
</td>
<td>
<select id="vendorDropdown_2" class="vendorDropdown">
<option value="1">Vendor Name 1</option>
<option value="2" selected="selected">Vendor Name 2</option>
<option value="3">Vendor Name 3</option>
<option value="4">Vendor Name 4</option>
<option value="5">Vendor Name 5</option>
</select>
</td>
</tr>
<tr id="3">
<td>
<span id="vendorLocation_3" class="vendorLocation">Toronto</span>
</td>
<td>
<span id="vendorCurrency_1" class="vendorCurrency">Canadian</span>
</td>
<td>
<span id="vendorCurrency_1" class="vendorCurrency">Logistics</span>
</td>
<td>
<select id="vendorDropdown_3" class="vendorDropdown">
<option value="1">Vendor Name 1</option>
<option value="2">Vendor Name 2</option>
<option value="3">Vendor Name 3</option>
<option value="4">Vendor Name 4</option>
<option value="5" selected="selected">Vendor Name 5</option>
</select>
</td>
</tr>
<tr id="4">
<td>
<span id="vendorLocation_4" class="vendorLocation">Los Angeles</span>
</td>
<td>
<span id="vendorCurrency_1" class="vendorCurrency">American</span>
</td>
<td>
<span id="vendorCurrency_1" class="vendorCurrency">Lumber</span>
</td>
<td>
<select id="vendorDropdown_4" class="vendorDropdown">
<option value="1">Vendor Name 1</option>
<option value="2">Vendor Name 2</option>
<option value="3">Vendor Name 3</option>
<option value="4" selected="selected">Vendor Name 4</option>
<option value="5">Vendor Name 5</option>
</select>
</td>
</tr>
<tr id="5">
<td>
<span id="vendorLocation_5" class="vendorLocation">Seattle</span>
</td>
<td>
<span id="vendorCurrency_1" class="vendorCurrency">American</span>
</td>
<td>
<span id="vendorCurrency_1" class="vendorCurrency">Services</span>
</td>
<td>
<select id="vendorDropdown_5" class="vendorDropdown">
<option value="1">Vendor Name 1</option>
<option value="2">Vendor Name 2</option>
<option value="3">Vendor Name 3</option>
<option value="4" selected="selected">Vendor Name 4</option>
<option value="5">Vendor Name 5</option>
</select>
</td>
</tr>
</tbody>
</table>
</body>
<!-- CSS -->
#vendorListing_wrapper {
width: 800px;
}
#vendorListing_filter {
display: none;
}
.odd {
background: #dddddd !important;
}
.even {
background: #ffffff;
}
<!-- jQuery -->
var vendorTable = "";
$(function() {
$('#vendorListing tfoot th.searchBox').each(function() {
var title = $(this).text();
$(this).html('<input type="text" placeholder="Search ' + title + '" id="search_' + title.replace(" ", "") + '" />');
});
vendorTable = $("#vendorListing").DataTable();
vendorTable.columns().every(function() {
var that = this;
$('input', this.footer()).on('keyup change', function() {
if (that.search() !== this.value) {
that
.search(this.value)
.draw();
}
});
});
});
從上面的代碼可以看出,您可以單獨搜索每個列。我遇到的問題是僅使用下拉列表搜索列中的選定選項。例如,當我搜索名稱1時,我應該只能得到紐約行,但是我得到所有行,因爲名稱1仍然存在於所有下拉列表中,但它並未被選中。
任何知道如何過濾搜索功能,所以只有選定的項目作爲結果?
https://jsfiddle.net/wbfsLx2x/2/
謝謝!
這樣做的伎倆,嗯。沒有意識到默認搜索必須被覆蓋。 - 謝謝! – Robert