2

I have a view displaying a table of related nodes that is embedded on another node's page. The info displayed in embedded view is periodically changing, I want to allow the user to refresh the view by clicking a "Refresh View" link or button without reloading the entire page.

I've checked out the views-hacks module which provides the auto-reload feature, but that doesn't do exactly what I need it to do.

3 Answers 3

1

I'm afraid you won't do without a bit of coding here. What first comes to mind is a combination of

  1. Form API for the refresh button,
  2. views_embed_view() for retrieving the current version of the view.
  3. AHAH javascript replacement for the view output replacing, and

Modules Views Auto-refresh mentioned by you or Ajax Views Refresh may not do exactly what you're after, but their code could be a source of inspiration when implementing views refreshing.

I also recommend having a look at the code of the Drupal core's Poll module, mainly the functions poll_choice_js (the callback function for the path poll/js) and poll_form ($form['choice_wrapper']['poll_more'])

1

You need to use Drupal.behaviors.

For lack of time, I haven't tested the code, but this is basically the logic...

    Drupal.behaviors.myViewsRefresh = {
      attach: function( context , settings) {

        var viewName = 'the-name-of-your-views';

        // Your views should be Ajax enabled,
        // then it will be available as a property of the Drupal object
        // like so...
        var instances = Drupal.views.instances;
        var yourViews;

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

        // Define your button click here...
        $('.yourButtonClass').click( function(){

            if(typeof yourViews !== 'undefined'){
              $(yourViews).trigger('RefreshView'); 
            }

        });            
      }
    }
1
  • If used trigger('RefreshView'), the pager is also running while refreshing page. So we have to stop execution of pager.
    – Guru
    Jun 11, 2015 at 11:01
0

Create an exposed filter. View the html with firebug or a similar extension, then copy the html for the form that submits the exposed filter. Remove all the selection options so you're left with just the submit button, and rename it to something like "Refresh view". Take this html and put it into a header (make sure to set the mode to full html). Voila, Refresh on Demand Form.

6
  • That is an interesting idea! ;-)
    – Jan Tomka
    Aug 3, 2011 at 8:56
  • Oh, it will reload the entire page, however...
    – Jan Tomka
    Aug 3, 2011 at 9:01
  • @Matt Well...if you copy the html from the views filter code, the form action you get <form id="views-exposed-form-task-notice-list-default" class="views-processed" method="get" accept-charset="UTF-8" action="/bridgepm/">
    – user2014
    Aug 3, 2011 at 15:18
  • So the action points to the base path, and clicking the submit button takes you to the home page. Any idea on how to fix that? I'm assuming views_exposed_form function pulls in some jquery to handle the auto-reload, and when you copy pasta the raw html into the header, you're not calling to that script. For now, I think a quick solution is to hide the unwanted input fields from the enabled exposed filter with some css...sounds a lot easier than coding a new module
    – user2014
    Aug 3, 2011 at 15:28
  • When I did this it did not reload the view... the form method should be get, not post.
    – Matt
    Aug 9, 2011 at 3:16

Your Answer

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