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

Data attribute like data-placeholder & other not working after an upgrade to 4.0.6-rc.1 from 4.0.5 #5320

Closed
india081947 opened this issue Jun 26, 2018 · 15 comments · Fixed by #5486

Comments

@india081947
Copy link

Hello,

After the upgrade from 4.0.5 to 4.0.6-rc.1 data attributes like data-placeholder, minimum-input-length etc not working.

As per 4.0.5 JSBin
As per 4.0.6-rc.1 JSBin

As far as I have checked it seem it is due to change in cache of data property using Utils.GetData

P.S. Creating issue for the first time in this repository, Please Pardon me if I have missed anything special to mention here.

@india081947 india081947 changed the title Data attribute like data-placeholder & other not working after an upgrade to RC.1 Data attribute like data-placeholder & other not working after an upgrade to 4.0.6-rc.1 from 4.0.5 Jun 26, 2018
@deimoks
Copy link

deimoks commented Jul 26, 2018

Can confirm this bug.

I made some debugs and it seems that keys in Utils.__cache are being stored AFTER they are needed, so it always returns undefined when Utils.GetData is looking for keys. I checked that these keys are needed in select2/options but they are created (Utils.StoreData) in select2/core, which runs afterwards.

If you need a workaround so that you get data-* attributes to Select2 options, you can do this:

$('.select2').each(function() {
    var placeholder = $(this).data('placeholder') || 'Default placeholder';
    $(this).select2({
        placeholder:placeholder,
    }); 
});

Just do this with other data-* attributes if you need it, until the issue is solved.

@india081947
Copy link
Author

Thanks @deimoks & @pedrofurtado

@joaopedroraldi
Copy link

joaopedroraldi commented Sep 27, 2018

If you need a workaround so that you get data-* attributes to Select2 options, you can do this:

$('.select2').each(function() {
    var placeholder = $(this).data('placeholder') || 'Default placeholder';
    $(this).select2({
        placeholder:placeholder,
    }); 
});

Just do this with other data-* attributes if you need it, until the issue is solved.

It works, but the templates and matcher messes around 😢

@przemek-pl
Copy link

Workaround from @joaopedroraldi didn't work for me but I solved it this way:

$("#select_field").prepend('<option></option>'); // Empty field for placeholder if not present yet
$("#select_field").attr("data-placeholder", "Placeholder");
$("#select_field").select2({ width: '100%' });

@benr77
Copy link

benr77 commented Oct 24, 2018

Confirmed - 4.0.6-rc.0 also suffers from this bug. I used data-minimum-results-for-search="Infinity" and it's being ignored, but works fine with 4.0.5.

Looks like Utils.__cache was introduced as a bug-fix (??) in the 4.0.6-rc0 release (#4346) and this is where the problem started.

I'm using the 4.0.6 version to get the fix for #4356 which is an ugly problem, but now I suffer from not being able to use the data- attributes.

The last release was almost a year ago. I can't see this issue being fixed or in outstanding pull requests either. Does anybody know if there is an attempt to fix it? The workarounds described above are pretty ugly!

@daveyholler
Copy link

Just wanted to +1 this issue. I'm attempting a workaround too, but specifically for data-allow-clear

$('[data-allow-clear]').each(function() {
    $(this).select2({
        allowClear: true
    })
})

The clear button displays as expected, but on click the ID returns null.

@eqnet
Copy link

eqnet commented Nov 21, 2018

If allowClear: true and NOT placeholder: ""/placeholder: "Some Placeholder" is defined you get and error when clicking on the clear button.

@stale
Copy link

stale bot commented Mar 13, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the status: stale label Mar 13, 2019
@nphilipp
Copy link

As I see it, the issue still exists. Bump.

@stale stale bot removed the status: stale label Mar 13, 2019
@crabnky
Copy link

crabnky commented Apr 3, 2019

Bump.
Does anybody know when this will be fixed?

@jonahgreenthal
Copy link

4.0.6-rc1 is just broken and shouldn't be used. Someone was briefly "maintaining" the project and made that release, but actually they just messed tons of stuff up.

@NadeemAfana
Copy link

All this would have been fixed with the new changes in #4346 more than a year ago, but no action was taken.

@Dannymx
Copy link

Dannymx commented Apr 15, 2019

Is there a version where this works? I'm using 4.0.6-rc.1 and is still broken.

@kevin-brown
Copy link
Member

I've been trying to reproduce this ever since the bug was first submitted (admittedly with some large gaps, as many of you have noticed), and I finally figured out why this wasn't caught by the test suite.

The test suite runs against jQuery 1.x, which happens to be the only version of jQuery that does not have this issue. Modifying the test suite to run against jQuery 2.x verifies that this issue does exist.

@NadeemAfana
Copy link

NadeemAfana commented Apr 24, 2019

@kevin-brown,
I had mentioned that fact before. Also, the PR #4346 I submitted already runs the test suite against both versions of jQuery just in case the issue came back in the future.

PS: I created another PR #5475 with the same changes as #4346 since the latter was closed due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.