Views exposed filters show the chosen selects correctly when the page first loads. However, after the submit the ajax call rewrites the view and the select fields lose their "chosen" functionality. How can we trigger the chosen jquery after the view ajax call?

Thanks,
Scott

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Hydra’s picture

Status: Active » Postponed (maintainer needs more info)

Hm not exactly sure what you did there. I tryed using a simple view, added an exposed filter wich uses select. Enabeld ajax_load and autosubmit on exposed forms. Everything worked well.
Could you please provide some more detailed info? Perheps an export of a simple view, showing your problem would help me reproduce it.

duckydan’s picture

This is not just ajax. Here is my issue:

http://dev.chattlibrary.org/calendar-node-field-event-date/month

If you pick a month and apply it, then go an pick another month, it reverts to the first month you picked most of the time.

I tried updating to the dev version of the module and the latest chosen library just in case that as it. No dice.

Dan

Dan

duckydan’s picture

OK, my issue seems to be the caching. If I turn off caching for the view, this seems to stop the issue.

Hope that helps someone.

Hydra’s picture

Can you try if the caching is a problem when not using chosen, too? I think that would be helpful, because it would be a views bug then. Otherwise I would rename this issue to something like "views cache compatibily" or I don't know :/ Sounds ugly :D

junedkazi’s picture

I just checked the page in the link above on chrome and it is working fine for me. Did u disable the cache ??

duckydan’s picture

The issue was also present without chosen. Sorry, I jumped to thew wrong conclusion.

Hydra’s picture

Status: Postponed (maintainer needs more info) » Fixed

Okay, since this issue does not exists, I think we can mark this as "fixed" :)
If however this issue returns and somehow can be related to chosen, please reopen.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

beardedgeek’s picture

Could I suggest this is reopened as this is still happening for me.

If I have ajax enabled, cache disabled, when I hit apply all the select lists revert to standard styling.

areynolds’s picture

Issue summary: View changes
Status: Closed (fixed) » Active

I've been experiencing this issue with 7.x-2.x-dev; after submitting on the exposed Views filter, the filters refresh via AJAX and return filters that aren't styled by Chosen.

Investigating a bit further.

creatile’s picture

I have the same issue. Is anyone found a solution ?

diamondsea’s picture

I am also having this problem.

Create a block view using Ajax and a Select box as an exposed filter. When "Apply" is clicked to rerun/apply the exposed filter, the block is redrawn, including the Select box, which no longer has Select applied.

We need to re-fire the Chosen() function on the reload of the block somehow.

diamondsea’s picture

This is hackish, but it works to at least address this on my particular page. In my .../themename/js/script.js file, I added the following function

$(document).ready(function () {
   if (document.location.pathname == '/page/with/my/block/view') {
     $(document).ajaxComplete(function(){
       $("select").chosen();
     });
   }
});

What it does:

1. On page load completion
2. It checks to see if it is the page I have my Views Block w/ Ajax and Exposed Filter
3. If so, it attaches to all ajaxComplete events on the page (of which the block view refresh will be one)
4. And re-applies the Chosen() call to all selects (add more specific selectors if needed)

What it probably should do:
- Attach to only the views blocks' Ajax calls.
- Use the Chosen module's defined selection rules
- Use the other Chosen module's settings (width, labels, etc)
- Be integrated into the Chosen module

I must unfortunately leave those steps to those without a fast-approaching deadline (and who are more fluent in proper Drupal module updating than I), but at least it should provide a workaround for people until then.

Ajtis.pl’s picture

Thanks @diamondsea

Only one small change in selectors and it works nice :)

$(document).ready(function () {
    $(document).ajaxComplete(function(){
                $(".view-filters select").not('.chosen-processed').chosen();
            });
});
glynster’s picture

I have had no luck on my end adding the following tweak to our custom js.
We are using adv aggregate, as soon as we disable this the chosen theming remains after an ajax request.

When I inspect the code all hints of chosen have been completely removed.

Here is the snippet I used but no luck:

jQuery(document).ready(function($){
    $(document).ajaxComplete(function(){
        $("select").chosen();
    });
});

Also tried

jQuery(document).ready(function($){
    $(document).ajaxComplete(function(){
        $(".view-filters select").not('.chosen-processed').chosen();
    });
});

No luck on either.

Any other ideas?

glynster’s picture

OK my issue was caused by the Form Placeholder module. Once I excluded the form class the problem was resolved.

madhukarkr’s picture

I am somehow missing chosen. I have created a view and kept taxonomy terms in exposed filters as dropdown list. I surely have more than 20 options in drop down. I have placed required library files at /sites/all/libraries/chosen/chosen and version is 1.1.0. I don't know why but am not getting select list modified. what am I missing here?

madhukarkr’s picture

Got it...my bad! i did not give the required settings for apply chosen. In my content type-> settings for taxonomy-> Apply Chosen to the select fields in this widget? here select apply and you are ready to go :)

OwilliwO’s picture

Hi guys,

I currently have the same problems.
I've exposed term filter, with AJAx enable, and chosen on term lists..
When i select one term and click Apply, chosen feature disappear and filter are only simple select list.
Here is a little patch on chosen.js, in which i wrap chosen acitvation in a function called two times : on page load and after ajax complete.

Have a look and tell me if it's OK.

zmove’s picture

Status: Active » Needs review

#19 works well for me.

Status: Needs review » Needs work

The last submitted patch, 19: apply-chosen-after-ajax-also-2023727-19.patch, failed testing.

erozqba’s picture

Version: 7.x-2.0-alpha2 » 7.x-2.x-dev

The patch #19 work well in Version: 7.x-2.x-dev not in 7.x-2.0-alpha2.

erozqba’s picture

Status: Needs work » Needs review
Watergate’s picture

Thanks, the patch in #19 works for me.

drusims’s picture

The patch #19 works like a charm in latest version of chosen : 7.x-2.0-alpha4

OwilliwO’s picture

FileSize
40.7 KB

Hi.

I've been trying to understand why my patch is not valid.
When validating, they used chosen 2.0-alpha2, and I've created my patch for alpha4 version.
Plus, this patch still work with alpha5 version.

How can we make tests OK, and up with chosen current version ?

maxocub’s picture

Status: Needs review » Needs work
maxocub’s picture

Status: Needs work » Needs review
maxocub’s picture

FileSize
2.58 KB

Trying to re-upload the path to make the tests run on the right branch.

Status: Needs review » Needs work

The last submitted patch, 29: apply-chosen-after-ajax-also-2023727-29.patch, failed testing.

maxocub’s picture

Status: Needs work » Needs review
FileSize
2.58 KB

Sorry, still the same patch as #19, but we should not trigger the tests since there's no tests.

Neograph734’s picture

Status: Needs review » Reviewed & tested by the community

Great, this also fixes an issue where different Drupal commerce product variations would loose the chosen attribute for properties (eg color).

Considering that many people confirmed the patch from #19 worked and this is the same patch and it still applies... I guess it is save to set this to RTBC.

cilefen’s picture

Title: Views exposed filters with ajax enabled looses chosen property after submit » Views exposed filters with ajax enabled loses chosen property after submit
Issue summary: View changes
nagy.balint’s picture

Hi!

We have tested on a site, and also on simplytestme, and Unfortunately we could not reproduce this issue.
The views is indeed replaced completely after the ajax call, but the chosen stays.

Either way since the js is implemented in a behavior, that is supposed to be called on each ajax request again on the context, so in theory this should work fine.

I guess something on the affected sites simply break the behavior of chosen. Maybe they cause a js error, or some other way.

So likely the issue is in additional contribs or custom code.

Can someone reproduce this issue on simplytestme?

nagy.balint’s picture

I will have to close it as cannot reproduce unless we can figure out which other module interferes with the functionality, as it should work out of the box due the behaviors.

Neograph734’s picture

Status: Reviewed & tested by the community » Needs work

It took me some time to reproduce, but I've found #32 not to be a bug indeed... at least to some point.

My chosen settings (admin/config/user-interface/chosen) were set to trigger for the following elements body:not(.page-admin) select. This worked great, removing chosen from all admin settings form (most of then are not built for it and become too wide or too narrow).

The thing I suppose that is happeling, is that there was no body element in the AJAX content (just some div's). So the selector no longer matched and Chosen was not applied.

No one can ask if that is desired behavior or not (perhaps Chosen should look at the entire DOM and not just the 'new' part), but I leave that to the maintainers.

Neograph734’s picture

I switched to 'needs work' because I believe this patch does not solve the issue and the maintainers might need to have another look at the behaviors.

nagy.balint’s picture

I think that the module only applies chosen to the markup that is replaced is "works as intended", as otherwise that would mean to apply chosen to elements where chosen is already applied.

If excluding all admin pages is not possible in any other way, then likely we need a new option in admin to be able to do so.
Related issues:
#2534756: Enable Chosen on admin page only
#2089987: Disable chosen on admin pages

nagy.balint’s picture

Status: Needs work » Closed (cannot reproduce)
andyg5000’s picture

I was able to reproduce this when my chosen selectors used the class from the body tag. For example ".page-my-view select".

Since the AJAX response does not return the full page, the context to reapply chosen doesn't exits. Changing the selector to use markup that is returned fixed the issue ".views-exposed-form select".

Not sure if it's worth re-opening this one, but hopefully that helps pinpoint the issue for others.

andyg5000’s picture