CMB2-Post-Search-field icon indicating copy to clipboard operation
CMB2-Post-Search-field copied to clipboard

Remember selection (code inside)

Open MordiSacks opened this issue 7 years ago • 4 comments

Hi, I wrote a few lines of code, that rechecks selected items

Add the code in init.php after line 188 (search.$response.html(data);)

                            search.$response.find('input').each(function (k, v) {
                                if (search.$idInput.val().split(',').indexOf(v.value) >= 0) {
                                    jQuery(v).prop('checked', true);
                                }
                            });

MordiSacks avatar Dec 01 '16 12:12 MordiSacks

updated comment, it is line 188, not 199

MordiSacks avatar Jan 31 '17 11:01 MordiSacks

For multiple selection: An id of each element must be trimmed otherwise only first checkbox will be selected:


let postIds = $.map(search.$idInput.val().split(","), $.trim);						search.$response.find('input').each(function ( k, v ) {					
if ( postIds.indexOf( v.value ) >= 0 ) {              	
  $(v).prop('checked', true);
}
});

aposidelov avatar Jul 30 '18 09:07 aposidelov

@jtsternberg I think this should be in CMB2-Post-Search-field plugin core.

slaFFik avatar Jan 20 '19 17:01 slaFFik

The only issue so far: by default WP loads only 50 items in the list. So if you have more and the one selected is beyond those 50 default selected - they won't be display and selected (and when you click Submit - ignored). We need to alter this behavior, so at the top of the list we will always load the selected items, and after that - those that are left. We will need to use pre_get_posts, as you have examples here: https://github.com/CMB2/CMB2-Post-Search-field/issues/21#issuecomment-235641777 - for post statuses.

slaFFik avatar Jan 20 '19 20:01 slaFFik