Show only selected rows/ Hide rows not selected
Show only selected rows/ Hide rows not selected
anu_vaid
Posts: 1Questions: 1Answers: 0
Here's what I came up with to show only selected rows on button click (I am using select and buttons extensions)
buttons: [
{
extend: 'selected',
text: 'Show selected rows only',
action: function ( e, dt, button, config ) {
if (button.text() == 'Show selected rows only') {
dt.rows({ selected: false }).nodes().to$().css({"display":"none"});
button.text('Show all');
}
else {
dt.rows({ selected: false }).nodes().to$().css({"display":"table-row"});
button.text('Show selected rows only');
}
}
}
],
It works we'll, but I was wondering if I am doing it right and if this can be improved.
This discussion has been closed.
Answers
Thanks a lot for this! This really helped. I changed it a little:
- When you select one row all other rows are hidden
- A "Show all" button is shown as soon as you have selected one row
- if you click this button all rows reappear and the button disappears again (meaning that the "Show all" button is only visible once you have selected one row.)
That button seems to work only for one page of data, instead of the whole table. Do you have a solution for all pages?
Also, if combined with the 'selectAll' button, the custom button doesn't behave as desired (i.e. press 'Show Selected Rows', then 'Select All' and then 'Show All'. It will show nothing.) A way to fix this is to replace
dt.rows({selected:false})
in the else statement with
dt.rows()
.Right it shows one page of data; that's what it was meant to do. This may be everything or just part of the data depending on your paging and page length settings. The button was not meant to overwrite those settings but you can surely add this to the button's functionality.
Maybe interesting for you. Since I am using my "ShowAll" button quite frequently I defined it as a custom button that I can reuse everywhere:
and a usage example:
Once you click the button all the child tables of the selected row are being hidden. In this example you can select a user from the parent table. Subsequently you can see and edit all the information of that user which is in child tables: e.g. her various phone numbers, addresses, roles, departments and what have you ... Clicking on the "ShowAll" button means you show all users and hide all child tables until you select the same or another user.
If you wanted to really show all rows not just the ones of the current page you would probably need to do this:
I added this line
pls see: https://datatables.net/reference/api/page.len()