5

I am embedding a view as such:

function theme_module_name_element($element) {
  $view = views_get_view('view_name');
  $view->set_use_ajax(TRUE);
  $output = $view->render();
  return $output;
}

The view is displayed then by the filefield sources module but when you try to search it won't use ajax. I'm assuming this has something to do with the js not being loaded but I just can't figure out how to get this going!

Any ideas?

Edit: This might be related to this: http://drupal.org/node/386388

Edit 2: The pager works and when it has been used the search works...

Edit 3: When the pager is pressed (and everything works fine) there is html added to the form so it goes from:

(Edit 3 update: If I had to guess I would say "Drupal.behaviors.ViewsAjaxView" from ajaxViews.js isn't being executed when the filefield_sources source is being chosen.)

<div class="view-filters">
    <div><div class="views-exposed-form">
        <div class="views-exposed-widgets clear-block">
            <div class="views-exposed-widget views-widget-filter-title">
                <label for="edit-image-title">Search</label>
                    <div class="views-widget">
                        <div class="form-item" id="edit-image-title-wrapper">
                            <input type="text" maxlength="128" name="image-title" id="edit-image-title" size="30" value="" class="form-text">
                        </div>
                    </div>
                </div>
                <div class="views-exposed-widget views-submit-button">
                    <input type="submit" id="edit-submit-Filefield-Sources-Imagebrowser" value="Apply" class="form-submit">
                </div>
            </div>
        </div>
    </div>
</div>

to:

<div class="view-filters">
    <form action="/" accept-charset="UTF-8" method="get" id="views-exposed-form-Filefield-Sources-Imagebrowser-default" class="views-processed">
        <div><div class="views-exposed-form">
            <div class="views-exposed-widgets clear-block">
                <div class="views-exposed-widget views-widget-filter-title">
                    <label for="edit-image-title">Search</label>
                        <div class="views-widget">
                            <div class="form-item" id="edit-image-title-wrapper">
                                <input type="text" maxlength="128" name="image-title" id="edit-image-title" size="30" value="arnar" class="form-text">
                            </div>
                        </div>
                    </div>
                    <div class="views-exposed-widget views-submit-button">
                        <input type="submit" id="edit-submit-Filefield-Sources-Imagebrowser" value="Apply" class="form-submit">
                    </div>
                </div>
            </div>
        </div>
        <input type="hidden" name="view_name" value="Filefield_Sources_Imagebrowser">
        <input type="hidden" name="view_display_id" value="default">
        <input type="hidden" name="view_args" value="">
        <input type="hidden" name="view_path" value="node/add/dagskrafaerslur">
        <input type="hidden" name="view_base_path" value="null">
        <input type="hidden" name="view_dom_id" value="655136b3027e2ef4608dfbaa7605db4c">
        <input type="hidden" name="pager_element" value="0">
    </form>
</div>

(Notice the addition of the form tag and input elements.)

4
  • Did you checked whether the required/necessary views js files are loaded? Please confirm the same by viewing the page source. Dec 7, 2012 at 16:57
  • Yes, they are loaded. If I print the view directly (in module code or directly) everything works as it should but when the view is printed by the Filefield Sources module the js isn't applied. Dec 9, 2012 at 8:42
  • Did you check in firebug or chrome dev tools weather the javascript has errors?
    – s.Daniel
    Dec 20, 2012 at 12:37
  • Yes, there are no errors. Thanks for any help though :) Dec 20, 2012 at 14:12

4 Answers 4

2

Enable the Ajax in Views Basic Setting and remove $view->set_use_ajax(TRUE); and check.

4
  • I think both methods are doing the same :( Dec 7, 2012 at 16:58
  • yeah but there can something missing in code.. I also worked in exposed filter yesterday. I added more than one block. But ajax was working on only one block. Dec 7, 2012 at 17:01
  • Nope, that doesn't do the trick, Unfortunately. Dec 7, 2012 at 19:08
  • @KarlJóhann Your other code can be the reason of this? Dec 8, 2012 at 2:15
2

Not sure what you mean by: "the js isn't applied"

So the shotgun approach...


Check the HTML source for .js files:

misc/ajax.js
sites/all/modules/views/js/ajax_view.js


views_add_js() is the command that loads the javascript.

http://api.drupal.org/api/views/views.module/function/views_add_js/7


If the js files aren't there maybe the scripts section in the header doesn't get updated:

$vars['scripts'] = drupal_get_js();

From: http://drupal.org/node/386388#comment-3176034

4
  • I have tried adding the js into the header but that doesn't help, probably because, as I then found out, the js is already there. Dec 13, 2012 at 11:40
  • Did you mean misc/ahah.js? What I mean by js isn't applied is that the "apply" button does not use ajax to filter the view. Dec 13, 2012 at 11:42
  • Not sure then, are there any js errors on the page? The pager works and when it has been used the search works... kinda sounds like Drupal.attachBehaviors() isn't being run. Try opening a javascript console and running Drupal.attachBehaviors(), or combo of Drupal.detachBehaviors() / Drupal.attachBehaviors()
    – jhr
    Dec 13, 2012 at 23:28
  • No sorry, that's not it either... unfortunately Dec 14, 2012 at 10:58
0

I just fixed a similar issue in my page. From your listing:

<input class="form-submit" type="submit" id="edit-submit-Filefield-Sources-Imagebrowser" value="Apply">

What is missing here (and was also missing in my case) are the classes which make autosubmitting code work. From my code (after fixes):

<input class="ctools-use-ajax ctools-auto-submit-click js-hide form-submit" type="submit" id="edit-submit-view-path" name="" value="Apply" />

In order to get the classes ctools-use-ajax ctools-auto-submit-click js-hide applied on input I had to fix theming. Check where your buttons are themed and if the classes aren't removed. I suspect this might be the reason because the attributes of your input are rendered in different order as in original Drupal theme. As for quick fix you can use a standard template coming with standard Drupal installation. Hope that helps.

0

I had the exact same problem down the same experience where using the pager caused the exposed filters to start using Ajax. In my case it was because the code was making the view get rendered inside of another form. If the views code was moved to render above the other form the JS was properly applied and the exposed form used Ajax submission. This makes sense since forms are not allowed inside of other forms.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.