Forums › Forums › Search & Filter Pro › A lot of checkbox or radio button: Show more / Show less
Tagged: V3
- This topic has 15 replies, 4 voices, and was last updated 4 years, 11 months ago by Trevor.
-
Anonymous(Private) September 13, 2019 at 11:30 am #220939
Hi,
Do you know if it is possible to put “Show more/Show less” button when we have a lot of options on a filter (checkbox)?
Default view:
☐ Value 1
☐ Value 2
☐ Value 3
+ Show moreAt the click of the button:
☐ Value 1
☐ Value 2
☐ Value 3
☐ Value 4
☐ Value 5
☐ Value 6
☐ Value 7
+ Show lessThanks. 🙂
Anonymous(Private) November 14, 2019 at 4:15 pm #226730Hi Trevor, Jordan,
As promised, here is a functional code that allows you to dynamically display the More / Less buttons. 🙂
Just change the “limit” variable to choose the maximum number of items to display.jQuery:
(function ($) { "use strict"; var $target = $('[data-sf-field-input-type="radio"], [data-sf-field-input-type="checkbox"]'), more_txt = '+ Show more', less_txt = '– Show less', items_expand = []; $(document).on('sf:init sf:ajaxfinish', '.searchandfilter', function() { $target.each(function(index, element) { // Variables var $el = $(element), item_total = $el.find('li').length, limit = 5, li_height = 0; if( item_total > limit) { // Collapse and add "Show more" button $el.addClass('sf-field-collapse').append('<div class="show-more-less">' + more_txt + '</div>'); // Get height of each <li>... $el.find('li:lt(' + limit + ')').each(function() { li_height += $(this).outerHeight(true); }); // ...and set total height on <ul> $el.find('ul').css('height', li_height); } }); // Re-expand if previously opened $.each(items_expand, function(index, value) { var index_child = value + 1, $el_expand = $('.searchandfilter > ul > li:nth-child('+ index_child + ')'); $el_expand.removeClass('sf-field-collapse').addClass('sf-field-expand'); $el_expand.find('.show-more-less').text(less_txt); }); }); $('body').on('click', '.show-more-less', function() { var $btn = $(this), $parent = $btn.closest('li'), parent_index = $parent.index(); if( $parent.hasClass('sf-field-collapse') ) { // Expand $parent.removeClass('sf-field-collapse').addClass('sf-field-expand'); $btn.text(less_txt); // Add index in array items_expand.push(parent_index); } else { // Collapse $parent.addClass('sf-field-collapse').removeClass('sf-field-expand'); $btn.text(more_txt); // Remove index in array items_expand = $.grep(items_expand, function(value) { return value != parent_index; }); } }); }(jQuery));
CSS
.sf-field-collapse { ul { overflow-y: hidden; } } .sf-field-expand { ul { height: auto !important; overflow-y: auto !important; } } .show-more-less { display: inline-block; margin-top: 0.5em; font-size: 0.8rem; color: blue; cursor: pointer; }
Enjoy! 🙂
-
AuthorPosts