harvesthq / chosen

Deprecated - Chosen is a library for making long, unwieldy select boxes more friendly.
http://harvesthq.github.io/chosen/
Other
21.85k stars 4.1k forks source link

if select is initially hidden, doesn't work #92

Open iamnader opened 13 years ago

iamnader commented 13 years ago

This is because when setting the width it will return 0. Using this technique works: http://www.foliotek.com/devblog/getting-the-width-of-a-hidden-element-with-jquery-using-width/

The code should check if the elmt.is(:visible) and if so use the above technique. I'm happy to fix in a fork. @hat is the procedure? Just patch the coffeescript? How do you generate js and js min?

reconbot commented 13 years ago

Yes you should patch the coffee script. Also I'm for the ability to setup hidden elements.

obie commented 13 years ago

+1 ran into this issue today

techpeace commented 13 years ago

+1 as did I

As a workaround, you can set the width of the created div with an !important flag:

#my_select_chzn {
  width: 100% !important;
}
techpeace commented 13 years ago

Actually, this will get you closer to where you need to be. The widths aren't perfect, but everything will be visible.

#my_select_chzn {
  /* This is the only width setting you'll need to change. */
  width: 100% !important;
}
#my_select_chzn .chzn-drop {
  width: 100% !important;
}
#my_select_chzn .chzn-drop .chzn-search {
  width: 99% !important;
}
#web_template_exploit_name_chzn .chzn-drop .chzn-search input {
  width: 98% !important;
}
pfiller commented 13 years ago

An earlier version of Chosen actually tested to see if the form field width was set via css. If so, it didn't use the width calculation. I think I prefer this method of fix to adding 30 lines of code for testing hidden elements. Would something like this work for you?

Set the width:

  <select style="width:350px;" class="chzn-select">

JS tests for set width first:

  @f_width = if @form_field_jq.css('width')? then parseInt(@form_field_jq.css('width'),10) else @form_field_jq.width()
aziz commented 13 years ago

@pfiller there's a small bug in your code. If the inline style has a width in any unit other than pixels (like em) the code will interprete it as pixels. see line 56 in chosen.jquery.coffee

I think people will continuously complain about it and you'll ultimately need to add that 30 lines of code.

iamnader commented 13 years ago

Hi @pfiller, that doesn't work. Both width() and css("width") return 0 for hidden elements. I'm using the getHiddenDimensions technique and it's working well.

btw, you need to use it both for f_width and also in get_side_border_padding

enhughesiasm commented 13 years ago

+1 for this.

A lot of the logic on the site I'm integrating Chosen on relies on some selects being hidden on first load, so we get all kinds of problems with this.

v15h commented 13 years ago

+1 as well. Visibility checks should be built in.

aziz commented 13 years ago

:+1: :+1: :+1:

AndrewRayCode commented 13 years ago

What about the solution where the dropdown width isn't set permanently on first startup, and is just resized to the width of the textbox when it opens? That way you don't have to ever touch the width of the input, just assume it's set with css and read it every time you open the dropdown.

johndwells commented 13 years ago

+1 for this. I've decided to turn Chosen into a fieldtype for the ExpressionEngine CMS, but the publish layouts of EE allows for fields to be hidden on page load.

I'm not sure if this is related, but in the case of EE it would be best if the width were fluid, or simply was always 100% of its containing element...

vespakoen commented 13 years ago

http://www.alistapart.com/articles/now-you-see-me/

AlexMC commented 13 years ago

Bumped into this one... it's still happening in the latest version

joesondow commented 13 years ago

+1 This bug is blocking usage of the Chosen plugin for internal apps at Netflix

DarkPreacher commented 13 years ago

:+1: on this issue, ran into this problem today

vespakoen commented 13 years ago

You can hide your content with: position: absolute; left: -999em; This way, screen readers and Google can "read" them too, and your Chosen dropdown WILL be visible when showing it again.

AndrewRayCode commented 13 years ago

My pull request fixes this issue, but like my other one and many of the ones for chosen they are being ignored. This project has too many contributors with too small of a code base.

bewest commented 13 years ago

DelvarWorld, thanks for your patch. This has been bugging me, and I was just about to implement a similar solution and thought I'd double check to see if anyone had already done so.

I'll be deploying this to production code shortly after I test it.

pfiller commented 13 years ago

@DelvarWorld what pull request are you referring to? I see one open pull request from you and it does not address this issue.

AndrewRayCode commented 13 years ago

@pfiller The one listed above in this thread, https://github.com/DelvarWorld/chosen-hacks/commit/e9501568acae1988ba51b5a29269023d3400db42

bewest commented 13 years ago

More important than using the css is using the "current" width of the element when rendering the results, instead of using an incorrectly cached width from the construction of Chosen.

There have been many changes, and I'm not quite sure of the best way to merge them myself. Will you have a merged version shortly?

AndrewRayCode commented 13 years ago

@bewest my pull request does that, every time the dropdown is shown it matches the width of the element

bewest commented 13 years ago

@delvarworld I know, just making sure the right distinction is made.

Can you try bringing your auto-width branch up to date with HEAD? I tried merging from a similar checkout around the end of August, but the input box is now the wrong width (it is too wide, overflowing the width of chosen's container).

I'm taking a look, but there's also been some re-organization... the constructor has been removed in favor of a setup and finish_setup methods. I'm also not familiar with coffeescript so your progress in merging might be faster than mine and submitting a pull request my help, since there seems to be a lot of interest in this issue.

AndrewRayCode commented 13 years ago

Actually, my branch has diverged past just that change, and eventually I gave up working on the prototype version. My fault, and I think at this point it's not mergeable.

bewest commented 13 years ago

@DevarWorld Yeah, it needs to be reworked a bit so that some bits go in the Abstract implementation. This should make it easier to maintain the differences between prototype and jquery.

Would you be willing to give it another go?

sagacity commented 13 years ago

A polite bump...is anyone working on this?

kevinelliott commented 12 years ago

Chosen doesn't work for me since it's hiding my select, and then unable to calculate the width. I have to hack it by arbitrarily setting a width on each of the chzn components in CSS. This does not seem like how this is suppose to work, and it really doesn't even look normal like it does on the websites I've seen it on.

RichardBlair commented 12 years ago

I also ran into this problem. As it turns out, the method that chosen uses to get the width of an element is broken in earlier version of jquery (I'm sorry, but I don't have the exact version numbers). As noted above, the bug was when an element is not visible the width would return 0. This bug is very well documented, and there are many ways around it.

The solution for us was to simply upgrade to the newest version of Jquery. Yes, we had to do some patching and testing of old code that used jquery, but in the long run it fixes a lot of issues and reduces the need for little hacks throughout your js/css.

kevinelliott commented 12 years ago

I am installing the latest jquery that is packaged in the rails jquery gem and jquery-ujs. I suppose this is still too old?

sagacity commented 12 years ago

Yes, an actual version number would be nice to have. I can confirm 1.6.2 is too old as well.

On Wed, Nov 16, 2011 at 11:23, Kevin Elliott < reply@reply.github.com

wrote:

I am installing the latest jquery that is packaged in the rails jquery gem and jquery-ujs. I suppose this is still too old?

stof commented 12 years ago

@RoyJacobs newest currently is 1.7 as it has been released.

RichardBlair commented 12 years ago

@RoyJacobs I can confirm that we are using 1.6.4 without issue.

kevinelliott commented 12 years ago

This is strange. Yeah the jquery-rails gem is using jQuery 1.7. But it's still not working for me.

You can see that it is not rendering with a width. Inspection shows all the calculated widths to be 0px. This occurs even if I set .chzn-select to have a width in CSS or directly in the style parameter of the select tag.

https://skitch.com/kevinelliott/gjufg/winetribe https://skitch.com/kevinelliott/gjufp/winetribe

Certainly annoying. The select seems to be getting display:none (I assume from the chosen javascript), and I can only imagine that it's then not able to calculate the width properly.

Thoughts?

kevinelliott commented 12 years ago

I really wish I could get this to work in ANY circumstance (even if I have to force it). Nothing seems to work. I will be forced to abandon this entirely (and I certainly won't want to deal with this broken stuff again).

KidkArolis commented 12 years ago

Make sure your select has some CSS applied, stylesheet or inline. If you do it with stylesheet it means you have to inject the element into the DOM before you call chosen().

But I agree that getting this right is very difficult. I would really like to be able to apply a width to the chosen select via CSS. One class, one width, px or %. My CSS would then look like

.example select, .example .chzn-smth { width: 50%; }

And this would apply the width correctly no matter how or when or where you call chosen.

kevinelliott commented 12 years ago

I have tried it inline and via stylesheet with no change in behavior though.

cballou commented 12 years ago

Pretty substantial problem for lightboxes and accordions. Anybody working on a patch?

kevinelliott commented 12 years ago

I don't think anyone is! I'm shocked because I don't understand how this is usable to anyone. I'm not even artificially hiding my content. The chosen() call seems to be doing it. So strange.

RichardBlair commented 12 years ago

@kevinelliott I think those that were having this issue have either moved on, or found a fix. As I said, in our case it was simply a matter of upgrading jquery. Keep in mind also that we have styled our element at all? If so are you using percentage or pixels?

kevinelliott commented 12 years ago

I am. I'm setting it with pixels. I have tried setting it on the element directly with the style attribute, and I have also tried it in CSS.

RichardBlair commented 12 years ago

@kevinelliott To be honest I'm at a bit of a loss. The only remaining suggestions I have is 1) wrap the select element in a element (we do this, and our works fine), and 2) stepping through the chosen library to find out at what point chosen is hiding your select element... I also had to do this when we ran into our issues.

Sorry I couldnt be more help.

tlfbrito commented 12 years ago

I have a case, that even with a fixed width in select the chosen create a width: 0px. I can't figured why, doesn't work on this situation, and work in others...

kevinelliott commented 12 years ago

@tiagobrito: I have the exact same problem and have never been able to solve it, fixed or not.

kevinelliott commented 12 years ago

@RichardBlair Thanks for your help. If you think of anything else, let us know :)

dhampik commented 12 years ago

+ same problem, please, fix it

bewest commented 12 years ago

@tiagobrito, @dhampik can you put together a small gist that demonstrates the problem?

tlfbrito commented 12 years ago

@bewest I already figured out! It append when I use jquery 1.4.x in my projects. Next monday I will tell you the correct version of jquery that cause me the width: 0px