seachPanes: Show pane by column name, not ordinal position
seachPanes: Show pane by column name, not ordinal position
Hi
I created a test case to demonstrate my problem but there is a script error so I can't show my issue. Test case is here live.datatables.net/kupizova/1/edit
All I want to do is show and hide panes based on their name, not the column position in the table. This is because the user can reOrder columns.
In my real world project, the searchPanes are ok and displayed in either a modal or a sidebar (user choice) but some panes like a telephone number or email address do not and should not be available for filtering because there would only ever be one. By rights, it should automatically be excluded from the panes because of the uniqueness.
If you were to apply this: -
$(document).ready(function() {
$('#example').DataTable( {
searchPanes:true,
dom: 'Pfrtip',
columnDefs:[
{
searchPanes:{
show: true,
},
targets: [0],
},
{
searchPanes:{
show: false,
},
targets: [2],
}
]
});
});
Then pane 2 would not be shown
I want to replace targets: [2]
with `targets: ['name:telephone', 'name:email']'
Is this possible?
Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide
Answers
The
columnDefs.targets
docs list all of the options you can use:You can use a class name that you can assign using
columns.className
.Kevin
Thanks Kevin, I appreciate your response.
The solution I was looking for was the
columns.className
method you identified but I can't get it to work.For a fuller picture, I am using
stateSaveCallback
andstateLoadCallback
to/from MySQL and using features such ascolReorder
etc.The searchPanes are drawn in my own bootstrap4 modal or sidebar (user preference) at
intComplete
usingtbl.searchPanes.container().appendTo('#myContainer');
If I could make the testcase display the searchPanes I could show you the working code
Thanks in advance for any further tips
FYI @kthorngren
I've got a working (or not) test case now at http://live.datatables.net/kupizova/1/edit
Please note that
targets: ['nopane']
is the line of code which is being ignored.if you change it to
targets: [4]
then it works as you would expect.I need to refer to the className and not the ordinal position of the column because my users can use
colReorder
Having experminted further, I've now managed to answer my own question and have a solution to share.
If, like me you are presenting a large number of tables to the system's users then dynamics are very important.
In my case, it's important that the panes are shown/hidden based on the column object and not the column's ordinal position because the user can move columns using
colReorder
and as such the usualcolumnDefs
approach wouldn't work because we don't know the position of the column once it has been moved and we are difining it's visibility before we initialize the dataTable.This test case demonstrates column manipulation to achieve the showing/hiding of specific panes outside the rules set by the default threashold for uniqueness.
In the test case, I have hard-coded the column info but in my real project the column info is generated on the fly by the PHP code which returns the data for the table from MySQL as per this example PHP code:-
Our js for the datatable is echoed in php so that we can use all our dynamics. It goes something like this (Only an excerpt, not everything shown)
Well, I guess using
columns.className
isn't applied early enough forcolumnDefs.targets
to use. You will need to apply them before. I assigned the class directly in theth
.http://live.datatables.net/kupizova/2/edit
Kevin
Thanks for your help Kevin. I can see from your example that assigning the class directly in the
th
is also a way to acheive the same goal.