Storing button indices of multi level buttons
Storing button indices of multi level buttons
This in an extension to this discussion.
In this test case, I am using the same global_index variable to store active button filters. The approach as in the previois discussion works well unless a multi level button is used. Filter 1 always has index 0, Filter 2 has index 1, but Filter 3 and 4 that are both inside the multi level Button Group both get index -1.
The index values of the multi level buttons are either the same for all of them as -1 or sometimes 0 and 1 which are the same indices of Filter 1 and 2 and this interferes with them. Thus, activating Filter 3 and then 4, and then deactivating 4 does not work as expected.
How can I make the indices for buttons inside multi level button groups unique to add and remove them from global_index correctly?
This question has an accepted answers - jump to answer
Answers
I've made a smaller example: https://live.datatables.net/hoyayubu/1/edit
Clicking on the filter buttons, you can see the index returned in the console such as
$('.filter1').index()
Filter 1 gets index 0, Filter 2 gets index 1, but Filter 3 and 4 that are both inside the multi level Button Group both get index -1.
How can Filter 3 and 4 get their own indices that I could use to acess the buttons later?
You can use
button().index()
to get a unique index for each button. As 3 and 4 are in a collection, they have indexes like "2-1" - see your example updated here: https://live.datatables.net/hoyayubu/3/editColin
This works! Thank you so much @colin