简体   繁体   English

触发DataTable高亮显示并根据输入更改进行过滤

[英]Trigger DataTable highlight and filter base on input change

Description 描述

I have a table 我有桌子

<table id="account-table" class="table display nowrap" cellspacing="0" width="100%">
    <thead>
    <tr class="text-left">
      <th>ID</th>
      <th>Type</th>
      <th>Name</th>
      <th>Email Address</th>
      <th>Action</th>
    </tr>
    </thead>
</table>

Here is my search input box 这是我的搜索输入框

<input type="text" class="form-control mb20" id="search" placeholder="Search">

Attempt 尝试

I've tried this settings 我已经尝试过此设置

**include**

<script src="https://cdn.datatables.net/plug-ins/1.10.10/features/searchHighlight/dataTables.searchHighlight.min.js" type="text/javascript"></script>
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://bartaz.github.io/sandbox.js/jquery.highlight.js"></script>



**settings**

$('#account-table').DataTable({
    data: data,
    deferRender: true,
    paging: true,
    searching: true,
    bLengthChange : false,
    Filter: false,
    Info:false,
    searchHighlight:true,
    iDisplayLength: 10,
});


**trigger the draw while typing**

$('#search').keyup(function(){
  $('#account-table').search($(this).val()).draw();
});

I'm not sure why my search functionality is not working when I start typing the search input box. 我不确定为什么当我开始输入搜索输入框时我的搜索功能不起作用。

There is 0 error in the console. 控制台中有0错误。

How can I debug this? 我该如何调试?

You shouldn't need to create your own search box, as datatable will create one for you above the table. 您不需要创建自己的搜索框,因为数据表会在表格上方为您创建一个搜索框。

Are you seeing one generated called: <input type="search" class="" placeholder="" aria-controls="account-table"> ? 您是否看到一个生成的名为: <input type="search" class="" placeholder="" aria-controls="account-table">

Here's a JSFiddle with all your code and with a working filter and highlight: JSFiddle 这是一个JSFiddle,其中包含您的所有代码以及有效的过滤器和亮点: JSFiddle

If you DO need to use your own search box, then use 如果您确实需要使用自己的搜索框,请使用

$('#search').keyup(function(){
  $('#account-table').DataTable().search($(this).val()).draw();
});

The reason it wasn't working was because $('#account-table') returns jQuery's results from its search for elements matching that selector - and DOM elements don't have functions called search on them. 它不起作用的原因是因为$('#account-table')通过搜索与选择器匹配的元素来返回jQuery的结果-DOM元素没有所谓的search功能。 Calling .DataTable() on it causes DataTable to return the table object that produced that DOM table. 在其上调用.DataTable()会使DataTable返回生成该DOM表的表对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM