简体   繁体   English

如何使用jquery触发dataTable搜索窗格的onclick事件?

[英]How to trigger onclick event of searchpanes of dataTable using jquery?

In DataTables, there are searchpanes facility.在 DataTables 中,有搜索窗格工具。 I want to triger it on page load.我想在页面加载时触发它。 Filter should be selected from arguments which are passed in url.过滤器应从 url 中通过的 arguments 中选择。 在此处输入图像描述

I have code that will extract value of rows for filter from URL and it will select all the rows those are passed in url, but it is only selection of the rows(I mean it will only highlight that row, in actual filter is not applied and Data of the table remain as it is).我有代码将从 URL 中提取过滤器的行值,它将 select 所有在 url 中传递的行都将突出显示它不会应用的行,但它只是在过滤器中的实际选择(和表的数据保持原样)。

My code of selecting row is shown below:我的选择行代码如下所示:

if(str1 == str2) {
  $(this).addClass("selected");
  if(!$("#DataTables_Table_"+id+"_wrapper").hasClass('dtsp-selected')) {
    $("#DataTables_Table_"+id+"_wrapper").addClass('dtsp-selected');
  }
}

here "str1" is string passed from url and "str2" is string that match in searchpanes table.这里“str1”是从 url 传递的字符串,“str2”是在 searchpanes 表中匹配的字符串。 "id" is searchpane table's id. “id”是搜索窗格表的 id。 By using this code, selection of correct row is working but actual filter is not applied.通过使用此代码,可以选择正确的行,但未应用实际过滤器。

I am looking for a trigger event that will apply filter after adding select class.我正在寻找一个触发事件,该事件将在添加 select class 后应用过滤器。

In DataTables, the SearchPanes extension uses the Select extension under the hood to manage the selection of filter values.在 DataTables 中, SearchPanes扩展在后台使用Select扩展来管理过滤器值的选择。 In fact, each search pane is itself a DataTable instance using the Select extension.事实上,每个搜索窗格本身就是一个使用Select扩展名的 DataTable 实例。 When you directly add the DataTable-specific classes ( dtsp-selected ) for programmatically triggering the filtering, you actually short-circuit DataTable's Select extension, what you should do instead is use its API (see general documentation here ).当您直接添加 DataTable 特定的类 ( dtsp-selected ) 以以编程方式触发过滤时,实际上是短路了 DataTable 的Select扩展,您应该改为使用它的 API (请参阅此处的一般文档)。

So what you need to do is first get the DataTable instances of the panes.因此,您需要首先获取窗格的 DataTable 实例。 It would be something like this:它会是这样的:

var panes = document.querySelectorAll('.dtsp-searchPanes table.dataTable');
var pane1DataTable = $(panes[0]).DataTable();

You can now leverage the rows().every() method (see method doc here ) to loop through the pane rows.您现在可以利用 rows().every() 方法(请参阅此处的方法文档)来遍历窗格行。 And once you get to a specific row you want to select, use the row().select() method (see method doc here ), so you get something like this:一旦您到达您想要 select 的特定行,请使用row().select()方法(请参阅此处的方法文档),因此您会得到如下内容:

pane1DataTable.rows().every( function ( rowIdx, tableLoop, rowLoop ) {
    // here the context is the row
    // perform you logic to check if the row must be selected then:
    this.select();
} );

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

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