Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultiSelect, closeOnSelect:false - Jumps to top of list each selection #1513

Closed
zambodi opened this issue Jul 9, 2013 · 20 comments
Closed

Comments

@zambodi
Copy link

zambodi commented Jul 9, 2013

When using a select with mode of multiple and closeOnSelect as false, the drop down jumps to the top of the list with each selection. This requires the user to scroll back down to where they were in the list to find the next selection. Search works well; however, you need to know what you're searching for. Sometimes, as a user, you don't know what choices your list contains.

Here is a jsFiddle:
http://jsfiddle.net/WP97R/1/

@zambodi
Copy link
Author

zambodi commented Jul 10, 2013

Hey, I was able to fix this in the Select2.js file, so that when in the multi-select & closeOnSelect is false, it will stop selecting the first entry in the list. It's a small trivial fix, but eliminates a great amount of frustration. Thought I'd share:
Version: 3.4.1 >>> onSelect: Line: 2664 >>> postprocessResults(null, null, this.opts.closeOnSelect);

image

@ivaynberg
Copy link
Contributor

thanks

@joelbrewer
Copy link

Hmm. I am still getting this issue in 3.5.1 -- It is very frustrating for my client to have to scroll back down to where they were after each selection.

@kevin-brown
Copy link
Member

Reopening this pending further debugging.

@kevin-brown kevin-brown reopened this Jan 14, 2015
@kevin-brown kevin-brown modified the milestones: 3.5.3, 3.4.2 Jan 14, 2015
@kevin-brown
Copy link
Member

Confirmed as fixed in 3.5.2 again.

@kevin-brown kevin-brown modified the milestones: 3.5.2, 3.5.3 Jan 16, 2015
@BonDoQ
Copy link

BonDoQ commented Sep 14, 2016

This Bug is happening again with version 4.0.3

http://jsfiddle.net/BonDoQ/rzvemnqy/

Problem:
https://github.com/select2/select2/blob/master/src/js/select2/results.js#L279
It always highlight the firstSelectedItem / firstItem

Solution:
check this condition before trigger that function would prevent the behaviour for that case

if(!(self.options.get('multiple') && !self.options.get('closeOnSelect'))) {
  self.highlightFirstItem();
}

@NewLIST
Copy link

NewLIST commented Sep 20, 2016

I solved this with the following:

var scrollTop;
$('#mySelect2').on("select2:selecting", function( event ){
    var $pr = $( '#'+event.params.args.data._resultId ).parent();
    scrollTop = $pr.prop('scrollTop');
});
$('#mySelect2').on("select2:select", function( event ){
    var $pr = $( '#'+event.params.data._resultId ).parent();
    $pr.prop('scrollTop', scrollTop );
});

Perhaps not the most elegant solution, but essentially we listen for the selecting event and then grab the current scrollTop of the selectable options panel. On the actual selection, we reset the panel's scrollTop back to its original. This happens so fast that you should see no jump in the control window. Only test with Select2 v. 4.x.

The nice thing about this solution is that you don't have to hack the component or include anything in your config functions.

@jackson-sandland
Copy link

jackson-sandland commented Dec 14, 2016

I am able to resolve this locally by commenting out self.highlightFirstItem in the results.js file. However, this is not a great fix, because whenever the node_modules are installed the change will be overwritten. I suggest that this feature be set to a boolean, so that devs can easily turn it on or off. The list we are using for our clients is long, and jumping to the top of the list doesn't improve the UX. What is a better workaround? I've tried the DOM manipulation but haven't been able to lock it down. Thanks!

self.setClasses(); self.highlightFirstItem();

@publishest
Copy link

Could noy find a better way than this...

` var scrollTopSl2;

$(document).on('select2:selecting', 'select[multiple]', function(event){

scrollTopSl2 = $( '#'+event.params.args.data._resultId ).closest('#select2-'+event.target.id+'-results').prop('scrollTop');

});

$(document).on('select2:select', 'select[multiple]', function(event){

 $( '#'+event.params.data._resultId ).closest('#select2-'+event.target.id+'-results').prop('scrollTop', scrollTopSl2 );

});

$(document).on('select2:unselecting', 'select[multiple]', function(event){

scrollTopSl2 = $( '#'+event.params.args.data._resultId ).closest('#select2-'+event.target.id+'-results').prop('scrollTop')

});

$(document).on('select2:unselect', 'select[multiple]', function(event){

 $( '#'+event.params.data._resultId ).closest('#select2-'+event.target.id+'-results').prop('scrollTop', scrollTopSl2 );

}); `

@Ecksters
Copy link

Seems like this is happening in the current version.

@shawty
Copy link

shawty commented Apr 10, 2017

plus 1 to that, happening for me too. Just done an NPM update to make sure I have the latest build (April 4th 2017) and still no diff.

@MicahBrown
Copy link

@jackson-sandland's solution worked for me as well. I also agree that it's poor UX behavior as I can't see any upsides to this behavior.

MicahBrown added a commit to MicahBrown/select2 that referenced this issue Apr 11, 2017
Resolves issue for multiselects using `closeOnSelect: false` that caused the list of results to scroll to the first selection after each select/unselect
MicahBrown added a commit to MicahBrown/select2 that referenced this issue Apr 22, 2017
@emileindik
Copy link

Any progress on this issue? Was it ever corrected in master branch?
Thanks!

@shawty
Copy link

shawty commented Aug 23, 2017

Doesn't appear so, for me, I just wrapped my selects in custom components and managed the datatype marshaling myself.

@MicahBrown
Copy link

@kevin-brown Can we get my #4869 PR merged so we can get a fix for this on master?

@joelworsham
Copy link

I'm eagerly awaiting as well, as I use the CDN hosted source, so I can't fix it. Thanks!

@alexweissman
Copy link
Contributor

Reopening this issue, as apparently it is still happening in 4.0.x.

@alexweissman alexweissman reopened this Sep 25, 2017
@alexweissman
Copy link
Contributor

Actually, I am going to close this issue because there is a newer issue, #4417, which seems to have some insight as to what's causing this issue in 4.0.x.

@smalezan
Copy link

smalezan commented Nov 16, 2017

Find this part of the select2.min.js and delete it.

,f=this.$results.scrollTop()+(e-d),g=e-d;f-=2*a.outerHeight(!1),c<=2?this.$results.scrollTop(0):(g>this.$results.outerHeight()||g<0)&&this.$results.scrollTop(f)

It worked for me

@alexweissman
Copy link
Contributor

Please continue discussion in #4417.

@select2 select2 locked and limited conversation to collaborators Nov 19, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests