JQuery Datatables SearchPane Filter Value

为君一笑 提交于 2021-01-05 07:34:05

问题


I am using the SearchPanes extension to Datatables, I am trying to capture the value of the item in the SearchPane they chose, it appears that since SearchPanes uses filter() instead of search() that value is not available. Am I wrong?


回答1:


You can access the selections as follows:

1) Add stateSave: true to the DataTable initialization definition. See this example.

This will cause all selections to be saved in the browser's local storage.

2) Use the following logic to access the browser's local storage:

var myStorage = window.localStorage;
var searchPanes = JSON.parse(myStorage.getItem('yourStorageIndexGoesHere'));
//console.log(searchPanes); // the full JSON - large!
//console.log(searchPanes['searchPanes']['panes']); // one object per search pane
searchPanes['searchPanes']['panes'].forEach(function(pane) { 
  console.log('ID = ' + pane.id + ' - selected: ' + pane.selected); 
});

In my case, I used the search panes shown in this demo.

Here is a screenshot with some selections:

Here is what the sample code writes to the browser console for the above selections:

The "ID" data value is a zero-based column index. So, column 3 (index 2) is the Office column, and column 6 (index 5) is the Salary column.

The related "selected" data are arrays, containing one or more value. You can iterate the arrays to get each separate value.

You will need to replace yourStorageIndexGoesHere with the actual name of your storage entry. The easiest (manual) way to find this is to perform a filter using SearchPanes, and then open your browser tools (usually F12). Then (assuming FireFox in my case) navigate to Storage > Local Storage > and select the relevant key text.

Points to Note:

a) This assumes you are OK with activating the "local storage" feature. It means that the browser will remember the last applied filter, and re-apply it when a user returns to the DataTable browser page. If users do not want that feature, then my solution will not be suitable for you.

b) I can't advise you on where you need to place the JavaScript I provided, because I don't know what you want to do with this information. But, for example, you might want to use it after every draw() event - in which case, see here.




回答2:


Jsut want to add, how the search pattern or the preselected searchPanes filters can be erased on demand - is stateSave is been enabled.

.on( 'stateLoadParams.dt', function (e, settings, data) {
                    //get the last search pattern, if this setting is enabled
                    //if not, erase last search
                    if (!cnf_lastSearch) {
                        //erase search input field
                        data.search.search = '';
                        data.start = 0;
                        //console.log(data)
                    } else {

                    }
                    //search through search panes array and erase content
                    if (!cnf_searchPanes) {
                        $.each((data['searchPanes']['panes']), function( i, val ) {
                          val.selected = '';
                        });
                    }
            })


来源:https://stackoverflow.com/questions/61690093/jquery-datatables-searchpane-filter-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!