Skip Navigation

[Resolved] Pressing Reset button makes filter options disappear.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 3 replies, has 3 voices.

Last updated by Christopher Amirian 11 months, 1 week ago.

Assisted by: Christopher Amirian.

Author
Posts
#2758600
Screenshot 2024-09-26 at 10.57.57 AM.png
Screenshot 2024-09-26 at 10.55.55 AM.png

Tell us what you are trying to do?

Trying to fix a bug that makes filtering options in my view disappear when the "Reset" button is hit.

Is there any documentation that you are following?

N/A

Is there a similar example that we can see?

Yes, I've attached images of the before and after hitting the "Reset" button

What is the link to your site?

Here is the link to the page the view exists on: hidden link

#2760812

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I need to check what's going wrong with the issue.

Have you added any custom code or any custom filter - if yes, what if you try to disable that for a moment and test - do you see it working as expected?

#2761997

Hi Minesh,

We were recently provided with some custom JavaScript from a different support agent to get our filter name sorted in alphabetical order. When commenting out that custom code it makes all filtering options disappear like in the second picture I sent in my initial post.

Happy to answer any other questions regarding this issue. Thank you for your help thus far.

#2762764

Christopher Amirian
Supporter

Languages: English (English )

Hi,

Please replace the previously suggested Javascript code to:

document.addEventListener("DOMContentLoaded", function() {
    function sortRadioAndDropdown() {
        // ********* For the .custom-radio radio buttons *********
        var customRadio = document.querySelector('.custom-radio');
        
        if (customRadio) {
            // Get all the radio inputs and their associated labels
            var radioItems = Array.from(customRadio.querySelectorAll('input[type="radio"]')).map(function(radio) {
                var label = customRadio.querySelector('label[for="' + radio.id + '"]');
                return {
                    radio: radio,
                    label: label
                };
            });

            // Sort the radio buttons alphabetically by their label text content
            radioItems.sort(function(a, b) {
                var textA = a.label.textContent.toLowerCase();
                var textB = b.label.textContent.toLowerCase();
                return textA.localeCompare(textB);
            });

            // Clear the container content
            customRadio.innerHTML = '';

            // Create a document fragment to re-append sorted elements
            var fragment = document.createDocumentFragment();

            // Append sorted radios and labels to the fragment
            radioItems.forEach(function(item) {
                fragment.appendChild(item.radio);
                fragment.appendChild(item.label);
            });

            // Append the fragment back into the container
            customRadio.appendChild(fragment);

            // Show the element with opacity transition after sorting
            setTimeout(function() {
                customRadio.style.opacity = 1;
            }, 100); // Delay to avoid flickering
        }

        // ********* For the <select> dropdown *********
        var selectDropdown = document.querySelector('select[name="wpv-post-topic"]');

        if (selectDropdown) {
            // Get all the options except the first one (which is "All")
            var options = Array.from(selectDropdown.querySelectorAll('option')).slice(1);

            // Sort the options alphabetically by their text content
            options.sort(function(a, b) {
                var textA = a.textContent.toLowerCase();
                var textB = b.textContent.toLowerCase();
                return textA.localeCompare(textB);
            });

            // Clear all the options from the dropdown
            selectDropdown.innerHTML = '';

            // Re-add the "All" option first
            var allOption = document.createElement('option');
            allOption.value = "0";
            allOption.textContent = "All";
            selectDropdown.appendChild(allOption);

            // Re-append the sorted options to the dropdown
            options.forEach(function(option) {
                selectDropdown.appendChild(option);
            });

            // Show the element with opacity transition after sorting
            setTimeout(function() {
                selectDropdown.style.opacity = 1;
            }, 100); // Delay to avoid flickering
        }
    }

    // Call the function when the DOM is loaded
    sortRadioAndDropdown();

    // Listen for AJAX completion (using jQuery for example)
    jQuery(document).ajaxComplete(function() {
        // Re-run the sorting logic after the AJAX request completes
        sortRadioAndDropdown();
    });
});

The new code also considers the Ajax loading feature that the RESET button has.

Thanks.