简体   繁体   English

将列搜索应用于当前jQuery DataTable第2部分

[英]Apply column search to current jQuery DataTable part 2

So I was able to get part 1 of my question to work, which is located here: Apply column search to current jQuery DataTable 因此,我能够解决我的问题的第1部分,该部分位于此处: 将列搜索应用于当前jQuery DataTable

That was utilizing a dropdown select method to the individual columns. 那是利用下拉选择方法对各个列。 However it appears that utilizing an INPUT box would be more effective. 但是,似乎使用输入框会更有效。

So I came across this fiddle: http://jsfiddle.net/dmurph/b71jtjf1/ 所以我遇到了这个小提琴: http : //jsfiddle.net/dmurph/b71jtjf1/

This is exactly what I am looking for. 这正是我想要的。 So first off, I added to my current table: 所以首先,我添加到当前表中:

 <table class="table table-bordered" id="example1" width="100%">
 <thead>
   <tr>
     <th>Edit/Del</th>
     <th>Booking</th>
     <th>Quote</th>
     ........
   </tr>
 </thead>
 <thead class="filters">
   <tr>
     <th>Edit/Del</th>
     <th>Booking</th>
     <th>Quote</th>
     ........
   </tr>
 </thead>
 //  the DATATABLE IN BETWEEN </thead> and </table>
 </table>

Now utilizing the code from the jfiddle link I provided above, this is what I have in total: 现在,利用我上面提供的jfiddle链接中的代码,这就是我总共拥有的:

 $('#example1 .filters th').each(function(){
   var title = $('#example1 thead th').eq($(this).index()).text();
   $(this).html('<input type="text" placeholder="Search '+title+'" />');
 });     

My original javascript to print the datatable comes next: 接下来是我原来的用于打印数据表的JavaScript:

 var $dataTable = $('#example1').DataTable({
   "ajax": serviceUrl,
   "iDisplayLength": 25,
   "order": [[ 6, "desc" ]],
   "scrollY": 600,
   "scrollX": true,
   "bDestroy": true,
   "columnDefs": [ { 
       "targets": 0,
       "render": function ( data, type, full, meta ) {
         return '
          <a class="editBookingLink" id="editBookingLink" href="#">Edit</a>
          <a class="delBookingLink" id="delBookingLink" href="#">Del</a>';
       }
     }]
   });

So far so good...the datatable still displays. 到目前为止一切顺利...数据表仍然显示。 But here comes the part where I'm having the issue: 但是这里是我遇到问题的部分:

Immediately after the above code, I have this (according to the jfiddle link): 在上面的代码之后,我立即有了这个(根据jfiddle链接):

 $dataTable.columns().eq(0).each(function(colIdx){
   $('input', $('.filters th')[colIdx]).on('keyup change', function(){
     table
       .column(colIdx)
       .search(this.value)
       .draw();
   });
 });

So errors until I try to search the INPUT field...well first of all, the column search doesn't search anything, but then I check the console and the error I am receiving is "Uncaught ReferenceError: table is not defined" pointing to line 805, which doesn't really make sense, because line 805 is in my original code where it reads below: 因此出现错误,直到我尝试搜索INPUT字段为止……首先,列搜索不会搜索任何内容,但是随后我检查了控制台,并且收到的错误是“ Uncaught ReferenceError:未定义表”指向到第805行,这没有任何意义,因为第805行在我的原始代码中显示如下:

 "scrollX": true

I am not sure why I am getting this error. 我不确定为什么会收到此错误。

Change table to $dataTable , so it reads: table更改为$dataTable ,因此它显示为:

$dataTable
   .column(colIdx)
   .search(this.value)
   .draw();

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

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