Feature #43825
openPreserve checkbox selection in autocomplete search for members, group users, and watchers
Description
When adding members, group users, or watchers, checkbox selections are lost when the search query changes.
This makes it difficult to select multiple users while searching.
Save and restore checkbox states across AJAX requests in observeSearchfield.
With this patch, only users currently visible in the search results will be submitted. Users who were checked but are hidden by the current search query will not be included.
I would appreciate feedback on whether this behavior is acceptable or if hidden selections should also be submitted.
Files
Updated by Go MAEDA 20 days ago
Thank you for posting the patch.
However, I found an issue where a previously unchecked user can be checked again unexpectedly after another search refresh. You can reproduce it with the following steps:
1. Open the "Add" dialog (members/watchers/group).
2. Check user A.
3. Type a search that hides user A.
4. Clear the search so user A appears again.
5. Uncheck user A.
6. Type any new search term that matches user A.
7. Observe that user A is checked again even though it was unchecked.
Updated by Go MAEDA 20 days ago
Uchino-san, could you take a look at the following fix?
It should fix the issue I pointed out in #note-2.
diff --git a/app/assets/javascripts/application-legacy.js b/app/assets/javascripts/application-legacy.js
index bea54ae9a..9490c2748 100644
--- a/app/assets/javascripts/application-legacy.js
+++ b/app/assets/javascripts/application-legacy.js
@@ -810,6 +810,8 @@ function observeSearchfield(fieldId, targetId, url, options) {
$(options.checkboxSelector).each(function() {
if ($(this).prop('checked')) {
checkedValues[$(this).val()] = true;
+ } else {
+ delete checkedValues[$(this).val()];
}
});
}
Updated by [Agileware]Kota Uchino 19 days ago
@Go MAEDA Thank you for the review and the suggested fix. I had overlooked the case where unchecking is not reflected — I appreciate you catching that.
I've updated the patch (preserve_checkbox_state_v2.patch) to include your fix and also address an additional issue:
With the previous patch, users who were checked but not currently visible in the search results would not be submitted. This is problematic when there are many users and pagination is involved — for example, if a user searches to find someone on page 2, checks them, then clears the search, that user returns to page 2 and is no longer visible on page 1, so their selection is lost on submit.
The updated patch solves this by injecting hidden <input> elements for checked users that are not currently visible as checkboxes. This ensures all selections are submitted regardless of the current search query or pagination page.
- Added
else { delete checkedValues[...] }insaveChecked()as you suggested - Added a delegated
changeevent handler on checkboxes to track check/uncheck in real time - Added
restoreChecked()which injects hidden inputs (<input type="hidden" class="hidden-checked-value">) for checked values not visible in the current DOM - Added
ajax:before/ajax:completehandlers for pagination links (a[data-remote]) to preserve selections across page navigation - Added
test_unchecked_user_should_not_be_rechecked_after_searchto cover the scenario from #note-2 - Added
test_add_user_to_group_submitting_while_filteredto verify hidden selections are submitted
Note: I'm aware that checkbox selections are also lost when navigating between pagination pages (e.g., check a user on page 1, navigate to page 2, then go back to page 1). I think that could be addressed as a separate issue.