gpt4 book ai didi

javascript - 数据表搜索不适用于列表类型的列

转载 作者:行者123 更新时间:2023-12-05 04:30:47 27 4
gpt4 key购买 nike

我在 html 页面上有数据表。在我的数据表中,我在页脚处进行了按列搜索。此搜索适用于所有列,但一列具有选择选项元素(下拉列表),因此它不起作用。每次当它开始搜索而不是列表的选定值时,它都会搜索所有列表值。

我的 table 看起来像:- enter image description here

在这里您可以看到位置搜索工作正常。它的绘制匹配值正确。 enter image description here

具有列表值的薪水列包含:- 适配器、Adater1、电容器、二极管但是在这里,当我尝试搜索时,它不会搜索选定的值,它会搜索每一行的整个列表,这就是为什么当我键入 Adapter1 时它无法分离,因为每一行都有 Adapter1 作为值。理想情况下它应该只显示一行,但这是我的输出:-

enter image description here

但是当我键入列表中不存在的任何值时,它会正确显示,因此搜索有效但它采用所有值而不是选定值:- enter image description here

你能帮我解决这个问题吗?

下面是我的代码

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>BootStrap</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<!-- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> -->

<link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap-table.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.5/css/jquery.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/select/1.3.4/css/select.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="https://editor.datatables.net/extensions/Editor/css/editor.dataTables.min.css">

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/datetime/1.1.2/css/dataTables.dateTime.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/2.2.2/css/buttons.dataTables.min.css">
<!-- <link type="text/css" href="//gyrocode.github.io/jquery-datatables-checkboxes/1.2.12/css/dataTables.checkboxes.css" rel="stylesheet" /> -->
<!-- https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.css -->
</head>
<body>

<div class="container mb-3 mt-3">

<div class="panel panel-default">
<div class="panel-heading"><b> Demo </b> </div>

<div class="panel-body">

<div class="tbl_user_data"><table class="table table-striped table-bordered" id = "mydatatable"
>

<thead>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr row_id = "123">
<td></td>
<td><div class="row_data" edit_type="click" col_name="fname">Tiger Nixon</div></td>
<td><div class="row_data" edit_type="click" col_name="pos">Developer</div></td>
<td><div class="row_data" edit_type="click" col_name="off">Edinburgh</div></td>
<td><div class="row_data" edit_type="click" col_name="age">61</div></td>
<td><div class="row_data" edit_type="click" col_name="startdate">2011/04/25</div></td>
<td><div class="row_data" edit_type="lov" col_name="sal">
<select class="example-dropUp" id="selhw">
<option >Adapter</option>
<option selected>Adapter1</option>
<option >Capacitor</option>
<option >Diode</option>
</select>
</div></td>
</tr>
<tr row_id = "124">
<td></td>
<td><div class="row_data" edit_type="click" col_name="fname">Tiger Nixon1</div></td>
<td><div class="row_data" edit_type="click" col_name="pos">System Architect1</div></td>
<td><div class="row_data" edit_type="click" col_name="off">Edinburgh1</div></td>
<td><div class="row_data" edit_type="click" col_name="age">611</div></td>
<td><div class="row_data" edit_type="click" col_name="startdate">2011/04/25</div></td>
<td><div class="row_data" edit_type="lov" col_name="sal">
<select class="example-dropUp" id="selhw">
<option >Adapter</option>
<option >Adapter1</option>
<option selected>Capacitor</option>
<option >Diode</option>
</select>
</div></td>
</tr>
<tr row_id = "125">
<td></td>
<td><div class="row_data" edit_type="click" col_name="fname">Tiger Nixon2</div></td>
<td><div class="row_data" edit_type="click" col_name="pos">System Architect2</div></td>
<td><div class="row_data" edit_type="click" col_name="off">Edinburgh2</div></td>
<td><div class="row_data" edit_type="click" col_name="age">61</div></td>
<td><div class="row_data" edit_type="click" col_name="startdate">2011/04/25</div></td>
<td><div class="row_data" edit_type="lov" col_name="sal">
<select class="example-dropUp" id="selhw">
<option >Adapter</option>
<option >Adapter1</option>
<option >Capacitor</option>
<option selected>Diode</option>
</select>
</div></td>
<tfoot>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
</tbody>
</table>
</div>
</div>
</div>



<div class="panel panel-default">
<div class="panel-heading"><b>HTML Table Edits/Upates</b> </div>

<div class="panel-body">

<p>All the changes will be displayed below</p>
<div class="post_msg"> </div>

</div>


</div>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/esm/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script>
<script src="https://cdn.datatables.net/select/1.3.4/js/dataTables.select.min.js"></script>
<script type="text/javascript" src="/js/bootstrap-table.min.js"></script>
<script src="https://editor.datatables.net/extensions/Editor/js/dataTables.editor.min.js"></script>
<script src="https://cdn.datatables.net/datetime/1.1.2/js/dataTables.dateTime.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.2.2/js/dataTables.buttons.min.js"></script>





<!-- <script type="text/javascript" src="//gyrocode.github.io/jquery-datatables-checkboxes/1.2.12/js/dataTables.checkboxes.min.js"></script> -->
<script type="text/javascript">
$(document).ready(function($)
{

$('#mydatatable tfoot th').each( function (index) {
//alert("in Tfoot");
console.log("tfoot th");
var title = $(this).text();
var notapplyFilteronColumn =[0]
if(notapplyFilteronColumn.indexOf(index) < 0 )
{
$(this).html( '<input type="text" id="txtName'+index+'" placeholder="Search '+title+'" data-index="'+index+'" />' );
}
} );

var table = $('#mydatatable').DataTable({
order:[[3,'desc']],
// processing: true,
pagingType:'full_numbers',
pageLength:20,
scrollY:500,
scrollX:true,
searchable: true,
// bFilter: false,
lengthMenu:[[5,10,25,50,-1],[5,10,25,50,"All"]],
select: {
style: 'multi'
},
columnDefs: [ {
orderable: false,
className: 'select-checkbox',
targets: 0,
}
],

select: {
style: 'multi',
selector: 'td:first-child'
},
search:
{
caseInsensitive: true,
smart: true
},
order: [[ 1, 'asc' ]],

initComplete: function () {
// Apply the search
console.log("Inside initcomplete");
this.api().columns().every( function (index) {
var that = this;
$( 'input', this.footer() ).on( 'keyup change clear', function () {
console.log("inside Keyup change");
console.log("index = "+index);
if ( that.search() !== this.value ) {
that
.search(this.value)
.draw();
}

} );

} );


} //initcomplete



}); //dt



});

</script>


</body>
</html>

最佳答案

当您使用 search(...) 以这种方式调用 API,您将被限制为搜索每一列中的所有内容。在您的情况下,这有两个问题:

(1) 每个包含选择列表的单元格实际上包含 <select>...</select> 的完整 HTML 文本element - 包括 select 使用的所有选项。这就是搜索似乎不起作用的原因。事实上,它工作正常。它总是会找到每个选择选项,无论它是否是已选择选项。

(2) 用户可以随时选择任何选择选项 - 所有此类更改仅在 DOM 中捕获。底层 DataTable(即 JavaScript 对象)对此类更改一无所知。所以,没有直接的方法来告诉 DataTables search()有关此类更改的功能。

解决方案

一种解决方案是使用 $.fn.dataTable.ext.search 创建自定义 DataTables 搜索功能.可以看到a basic example here .

下面是一个可运行的片段,它基于您问题中代码的简化版本,展示了如何调整 $.fn.dataTable.ext.search还处理包含选择列表的列:

// remove the default global filter function:
while($.fn.dataTable.ext.search.length > 0) {
$.fn.dataTable.ext.search.pop();
}

// add our custom filter function:
$.fn.dataTable.ext.search.push(
function( settings, data, rowIdx ) {
// get the TD node containing the drop-down list:
var selectListTdNode = $('#example').DataTable().rows(rowIdx).nodes().to$().find( 'td' )[5]
// get the selected value:
var selection = $( selectListTdNode ).find(":selected").text();

// process each field in the row:
var displayRow = true; // start by assuming the row should be displayed
data.forEach(function (item, idx) {
// for the 6th column (idx = 5), we need to use the selected drop-down
// value, and not the contents of the cell:
item = (idx === 5) ? selection : item;

var searchField = $('#example').DataTable().columns(idx).footer();
var searchValue = $(searchField).find( 'input' ).val();
// should the row be displayed? 'false' means 'do not display':
if (searchValue !== '' & !item.toLowerCase().includes(searchValue.toLowerCase())) {
displayRow = false;
}
});
return displayRow;
}
);

$(document).ready(function() {

// create an input field in each footer cell:
$('#example tfoot th').each( function () {
var title = $(this).text();
$(this).html( '<input type="text" placeholder="Search '+title+'" />' );
} );

var table = $('#example').DataTable({
dom: 'lrtip' // hide the global filter box ('f')
});

// create change events to trigger a re-draw, which also calls the filter function:
$('#example tfoot th input').keyup( function() {
table.draw();
} );
$('#example td select').change( function() {
table.draw();
} );

} );
<html>
<head>
<meta charset="UTF-8">
<title>Demo</title>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<script src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.22/css/jquery.dataTables.css">
<link rel="stylesheet" type="text/css" href="https://datatables.net/media/css/site-examples.css">

</head>

<body>

<div style="margin: 20px;">

<table id="example" class="display dataTable cell-border" style="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>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>
<select>
<option >Adapter</option>
<option >Adapter1</option>
<option >Capacitor</option>
<option selected>Diode</option>
</select>
</td>
</tr>
<tr>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011/07/25</td>
<td>
<select>
<option >Adapter</option>
<option >Adapter1</option>
<option >Capacitor</option>
<option selected>Diode</option>
</select>
</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>
<select>
<option >Adapter</option>
<option >Adapter1</option>
<option >Capacitor</option>
<option selected>Diode</option>
</select>
</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>Senior Javascript Developer</td>
<td>Edinburgh</td>
<td>22</td>
<td>2012/03/29</td>
<td>
<select>
<option >Adapter</option>
<option >Adapter1</option>
<option >Capacitor</option>
<option selected>Diode</option>
</select>
</td>
</tr>
<tr>
<td>Airi Satou</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>33</td>
<td>2008/11/28</td>
<td>
<select>
<option >Adapter</option>
<option >Adapter1</option>
<option >Capacitor</option>
<option selected>Diode</option>
</select>
</td>
</tr>
<tr>
<td>Brielle Williamson</td>
<td>Integration Specialist</td>
<td>New York</td>
<td>61</td>
<td>2012/12/02</td>
<td>
<select>
<option >Adapter</option>
<option >Adapter1</option>
<option >Capacitor</option>
<option selected>Diode</option>
</select>
</td>
</tr>
<tr>
<td>Herrod Chandler</td>
<td>Sales Assistant</td>
<td>San Francisco</td>
<td>59</td>
<td>2012/08/06</td>
<td>
<select>
<option >Adapter</option>
<option >Adapter1</option>
<option >Capacitor</option>
<option selected>Diode</option>
</select>
</td>
</tr>
<tr>
<td>Rhona Davidson</td>
<td>Integration Specialist</td>
<td>Tokyo</td>
<td>55</td>
<td>2010/10/14</td>
<td>
<select>
<option >Adapter</option>
<option >Adapter1</option>
<option >Capacitor</option>
<option selected>Diode</option>
</select>
</td>
</tr>
<tr>
<td>Colleen Hurst</td>
<td>Javascript Developer</td>
<td>San Francisco</td>
<td>39</td>
<td>2009/09/15</td>
<td>
<select>
<option >Adapter</option>
<option >Adapter1</option>
<option >Capacitor</option>
<option selected>Diode</option>
</select>
</td>
</tr>
<tr>
<td>Sonya Frost</td>
<td>Software Engineer</td>
<td>Edinburgh</td>
<td>23</td>
<td>2008/12/13</td>
<td>
<select>
<option >Adapter</option>
<option >Adapter1</option>
<option selected >Capacitor</option>
<option>Diode</option>
</select>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
</table>

</div>



</body>
</html>

解释

我在代码中添加了注释来解释详细步骤,但总的来说,它的工作原理如下:

首先,我们删除了默认的搜索功能(全局搜索框使用了它)——我们还使用数据表 dom 隐藏了全局搜索框本身。选项。

然后我们定义替换搜索函数。

此函数需要访问 DOM,以提取每个选择列表的所有选定值 - 这就是 find(":selected").text()

(我们只需要在表格的最后一列使用它,所以所有其他列都可以使用它们的单元格内容而无需这个额外的步骤。)

每次执行 draw() 时都会触发搜索功能.当它触发时,它会针对表中的每一行单独执行,并确定是否应显示或隐藏每一行。它通过返回一个 true 来做到这一点。或 false值,基于将每个字段的值与列的搜索词进行比较。

如果任何 搜索词与相关字段的数据不匹配,则隐藏该行。这就是我们确保所有列过滤器指令相互组合评估的方式。该解决方案使用 displayRow用于跟踪每一行的整体“可显示”状态的 boolean 值。

最后,在脚本的末尾,我们需要创建事件处理程序,这将强制重新绘制 DataTables,并且还会导致再次调用我们的自定义过滤器函数。请注意,我们不仅为页脚中的输入字段创建了这些事件处理程序,而且还为每个选择列表创建了这些事件处理程序 - 因为用户可以更改选择列表的值,并且过滤需要立即将此类更改考虑在内。

最后的说明

对于数据表,通常有不止一种方法来完成一项任务。但在这种情况下,我肯定会建议使用自定义函数,以获得最大的灵 active 。

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

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