简体   繁体   English

刷新后jQuery数据表保留过滤器参数

[英]jQuery datatable retain filter parameter after refresh

I am working with a datatable, as per some of my previous questions.根据我之前的一些问题,我正在使用数据表。 I was able to add INPUT fields at the top of the table that conducts individual column searches in the datatable.我能够在表的顶部添加 INPUT 字段,在数据表中进行单独的列搜索。

What I need to do now is retain the parameter entered in the INPUT field (or fields) after the page refreshes.我现在需要做的是在页面刷新后保留在 INPUT 字段(或多个字段)中输入的参数。

Here is my code so far:到目前为止,这是我的代码:

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

 // set and print the datatable
 var $dataTable = $('#example1').DataTable({
   "ajax": 'api/dateset.php',
   "iDisplayLength": 25,
   "order": [[ 6, "desc" ]],
   "scrollY": 580,
   "scrollX": true,
   "bDestroy": true,
   "stateSave": true
 });

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

If you'll notice in the portion above where I set the $dataTable, you should see "stateSave": true .如果您注意到上面我设置 $dataTable 的部分,您应该会看到 "stateSave": true 。 Using that, when the page refreshes, it does save the parameter search entered by the user, but it doesn't display the text in the INPUT field.使用它,当页面刷新时,它会保存用户输入的参数搜索,但不会在 INPUT 字段中显示文本。

That is where I am stuck.这就是我被困的地方。

Here is a visual representation:这是一个视觉表示:

Before refresh -刷新前——

在此处输入图片说明

After refresh -刷新后——

在此处输入图片说明

As you see in the second picture, the search is good for BOOKING beginning with TEST222, but the text is no longer visible in the BOOKING INPUT field.正如您在第二张图片中看到的,搜索适用于以 TEST222 开头的 BOOKING,但文本在 BOOKING INPUT 字段中不再可见。

I did come across this post: https://datatables.net/reference/option/stateSaveCallback我确实遇到过这篇文章: https : //datatables.net/reference/option/stateSaveCallback

But I am not sure how to implement that code into my code.但我不确定如何将该代码实现到我的代码中。 I am not even sure if stateSaveCallback is the right function to use.我什至不确定 stateSaveCallback 是否是正确使用的函数。

I finally found this post: http://live.datatables.net/neqozaj/1/edit我终于找到了这篇文章: http : //live.datatables.net/neqozaj/1/edit

Utilizing that post, I was able to add this piece of code to the overall function:利用那篇文章,我能够将这段代码添加到整个函数中:

 var state = $dataTable.state.loaded();
 if ( state ) {
   $dataTable.columns().eq( 0 ).each( function ( colIdx ) {
   var colSearch = state.columns[colIdx].search;

   if ( colSearch.search ) {
     $('input', $('.filters th')[colIdx]).val( colSearch.search );
   }
  });
  $dataTable.draw();
 }

Using this, I was able to to achieve the effect I wanted.使用这个,我能够达到我想要的效果。

If you have column level filter then try the below code.如果您有列级过滤器,请尝试以下代码。

// Restore state, search and column level filter
    var state = table.state.loaded();
    if (state) {
        table.columns().eq(0).each(function (colIdx) {
            var colSearch = state.columns[colIdx].search;

            if (colSearch.search) {
                $('input', table.column(colIdx).header()).val(colSearch.search);
            }
        });

        table.draw();
    }


    // Apply the search
    table.columns().eq(0).each(function (colIdx) {
        $('input', table.column(colIdx).header()).on('keyup change', function () {
            table
                .column(colIdx)
                .search(this.value)
                .draw();
        });
    });

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

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