5

I have two different blocks. One has a form and the other one is a view with some files uploaded from the form above.

On submit of the form I need to update the view results. I thought I could do it using js but I can't make it work.

Please check my code. Any advice would be helpful!

Drupal.behaviors.myViewsRefresh = {
    attach: function( context , settings) {
        var viewName = 'application_documents';

        // Your views should be Ajax enabled
        var instances = Drupal.views.instances;
        var myViews;

        // then simply need to iterate through the various
        // views instances name to find the view
        $.each( instances , function getInstance( index, element){
            if(element.settings.view_name == viewName ){
                myViews = '.view-dom-id-' + element.settings.view_dom_id;
            }
        });

        $(document).once('mysubmitbutton').ajaxComplete(function(event, xhr, settings){
            if(typeof myViews !== 'undefined'){
                $(myViews).trigger('RefreshView');
            }
        });

    }
}

I can get into the ajaxComplete but the view doesn't reload. The view id is correct.

Thank you in advance.

1 Answer 1

9

The way you construct your myViews variable seems to be the reason this doesn't work for you.

What worked for me was changing this:

            myViews = '.view-dom-id-' + element.settings.view_dom_id;

to

            myViews = '.js-view-dom-id-' + element.settings.view_dom_id;

I just tried a simple version of that without any form submiting, simply calling this from my browser's console:

jQuery('.js-view-dom-id-c58e9660da223aaf7c1645b2746e0b2e16f6a2f3e1de74c8533e6ad6117613db').trigger('RefreshView')

and it worked as expected. As a final remark and as is commented in the code you provided, your views should be Ajax enabled, otherwise, this will not work.

Good luck!

P.S.: The most relevant piece of information from the codebase (this comment says it all):

// Add a trigger to update this view specifically. In order to trigger a
// refresh use the following code.
//
// @code
// $('.view-name').trigger('RefreshView');
// @endcode

(http://cgit.drupalcode.org/drupal/tree/core/modules/views/js/ajax_view.es6.js#n94)

5
  • Thanks!! It worked. But now when refreshes the view triggers an infinite loop. Also the view I get is empty but maybe has to do with the infinite loop.
    – Ismini
    Aug 12, 2017 at 18:11
  • Grand! Did you try simply triggering the RefreshView manually outside of the form submit? In order to narrow down what works and what doesn't? Aug 13, 2017 at 10:46
  • Thank you @Stefanos. I changed a bit my code in order to call my custom function only after ajax submit and now the infinite loop is gone! :) but still I don't get the view results. I tried to trigger the RefreshView directly in my browser's console but still no luck. This view is a combination of 4 different views displayed in the header of my current view. I have also a hook_views_alter which is not triggered when I trigger the refresh(I guess it should). Is there some way to test it? Thanks!
    – Ismini
    Aug 14, 2017 at 8:44
  • This is now a different question (or even more than one). I would start by checking if there is an actual ajax request sent; you can see that in the "Network" tab of your browser. After that, check if the manual triggering of RefreshView works with the View running on it's own page, and not inside a block. Just some ideas. :-) Aug 14, 2017 at 10:33
  • Thanks! Yes I'm checking also the network. I managed to make it work partly. As there are different views I had to refresh also them. So I enabled the Ajax on those and I also refresh them. Two of those doesn't work but I guess it has to do with some configuration so I'm on it at the moment. Thank you so much for your help!
    – Ismini
    Aug 14, 2017 at 10:55

Your Answer

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

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