Show/Hide button not working

Show/Hide button not working

anibha88anibha88 Posts: 5Questions: 2Answers: 0

Link to test case: https://live.datatables.net/jepukula/1/
Debugger code (debug.datatables.net): Error uploading data
Error messages shown: Nothing in console
Description of problem: Colvis functionality is not working. I don't see any errors in the console. I have an array from the backend (RoR) which is basically an array of hashes. The code looks as below:
```<%
attributes_with_visibility = []
current_subscription.all_numeric_attributes.
for_tree_level(current_subscription.project_level_tree).each do |attr|
visibility = "hide-attr"

attributes_with_visibility << {name: attr.name, attribute: attr, visibility: visibility}

end
%>```
This is what I am trying to display in the dropdown in a way that everything should be hidden by default and should appear on the datatable whichever is clicked. The configuration is as below:

$('#snapshot_table').DataTable({
...
dom: 'Bft',
          columnDefs: [
            {
              orderable: false,
              targets: 'unsort'
            },
            {
              visible: false,
              targets: 'hide-attr'
            }
          ],
          buttons: [
            {
              extend: 'colvis',
              columns: ':not(:first-child)',
              className: 'btn-sm btn-outline',
              text: 'Hide / Show Columns &nbsp;<span class="caret"></span>'
            },
          ],
});

Please help.
Thanks in advance!

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    Thanks for the test case, but there's a lot going on - for example why are you initialising then destroying the table immediately?

    Please can you give steps on how to reproduce the issue you would like support with,

    Colin

  • anibha88anibha88 Posts: 5Questions: 2Answers: 0
    edited September 2023

    Hi Colin,
    Thanks for the response. Sorry for the confusion.
    In our rails application, there is another place where we have a similar kind of UI, and the code with data table configuration is working well as expected (the columns are hidden by default and displayed on the table when clicked from the dropdown). When I tried to implement the same logic the columns visibility dropdown is not working. I have read in some forums that improper initialization of data table or some conflicts might be one of the reasons and so I am destroying the table and reinitializing with the same configuration. I have updated the same with another test case link.
    https://live.datatables.net/beluhiho/1/
    Basically, this is what I am trying to achieve: I have an array of hashes coming from the backend (which is referred to as attributes_with_visibility in my code) which should be populated in the dropdown and by default everything should be hidden. Once I click on any of the attributes in the dropdown, the clicked or selected attribute should be displayed in the table.
    Thanks,

  • anibha88anibha88 Posts: 5Questions: 2Answers: 0

    Hi Colin,
    Sorry for the confusion. In our RoR application, there is another page with the same UI and functionality and it works well (show/hide columns) without any issues. I am trying the achieve the same functionality, but I am not able to get the show/hide columns thing working. I read in some forums that one of the reasons could be improper initialization or conflicts that might cause some issues. So I was destroying the table and initializing it again with the same configuration.
    I have updated the test case link with the original instance of datatable.
    https://live.datatables.net/beluhiho/1/
    In short, what I am trying to achieve is that I have an array of hashes data that is coming from the backend ( it is referred to as attributes_with_visibility in my code) which is present in the dropdown and everything should be hidden by default. When any of the attribute is clicked or selected, the selected attribute should appear in the datatable.
    Thanks,

  • kthorngrenkthorngren Posts: 21,211Questions: 26Answers: 4,928

    Your test case wasn't loading the Column Visibility library so the button wasn't showing. I added it here:
    https://live.datatables.net/bemulazi/1/edit

    Column Visibility is working. Its a bit confusing because it is using the titles from the second row which many are empty and there are some duplicates.

    As Colin asked please provide detailed steps to show the issue you are trying to solve.

    Kevin

Sign In or Register to comment.