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 about 2 months ago
- Target version set to Candidate for next minor release
Updated by Go MAEDA about 2 months 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 about 2 months 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 about 2 months 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.
Updated by Go MAEDA 4 days ago
Thank you for updating the patch.
There is still an edge case in observeSearchfield: if the dialog opens with no matching checkboxes, cbName is initialized as undefined and never recovers. As a result, the dialog may drop some selections from the submission.
Applying the patch fix-for-43825-note-4.patch to the patch in #note-4 would fixes the issue.
Reproduction Steps
1. Prepare an issue where the watcher candidates are initially empty.Specifically:
- do not add any members to the project
- make the project public
- allow "Non member" to "View issues"
- create one issue
2. Open that issue as admin, and open the watcher-add modal from "Add" in the sidebar.
3. Immediately after opening the modal, confirm that the candidate list is empty.
4. Enter the name of an active user outside the project into the search box.
Example: "John Smith"
After searching, candidates will appear.
5. Check that user.
6. Then search for another user name so that the user you just selected disappears from the list.
Example: "Dave Lopper"
7. In that state, click "Add" without clearing the search.
Expected Result
The initially selected "John Smith" is preserved via a hidden input and submitted.
Actual Result
Only "Dave Lopper" is added as a watcher