0

Goals:
1. have a button in a View that refreshes that View via AJAX on click event.
2. Load refresh script only on pages that have View named "my_view".

I was able to achieve both of the following with the code shown below.

template.php

function mytheme_preprocess_views_view(&$variables) {
  $view = &$variables['view'];
  // Make sure it's the correct view
  if ($view->name == 'products_left_in_stock') {  //EDITED
    // add needed javascript
    drupal_add_js(drupal_get_path('theme', 'mytheme') . '/js/views-refresh.js');
  }
}

viewsrefresh.js

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

        var viewName = 'products_left_in_stock';

        // View is Ajax enabled,
        // and is available as a property of the Drupal object
        // like so...
        var instances = Drupal.views.instances;
        var targetedView;

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

        // Define your button click here...
        $('.viewsrefreshbtn').click( function(){
                if(typeof targetedView !== 'undefined'){
                    $(targetedView).trigger('RefreshView'); 
                    console.log ('Clicked refresh btn on view. View Refreshed');
                }
                else {console.log ('Can\'t find viewName on this page');}
        });            
    }
}
})(jQuery);

refresh button added to Views footer in Global Text

<div class="viewsrefreshbtn">refresh</div>

ISSUE IS: Refresh button works only for logged in users. When Anonymous user clicks Refresh button - following line from viewsrefresh.js executes

else {console.log ('Can\'t find viewName on this page');}

and nothing else happens. Targeted View is present on a page and markup generated for that view is exactly the same as for logged in users. Seems like my function can not find the view when user is not logged in.

QUESTION: How can I make this function to detect targeted view when the user is not logged in? Am I missing something fundamental?

I understand this is a lengthy question. Greatly appreciate any insight on this if anybody has time to spare. thanks

  • Hello. I think that problem is one of two reasons. The first one - due the some reason views ajax is not available for anonymous (very strange, maybe cache). The second one is that your view-dom-id is wrong. Also for me it looks strange that you have different names of view in your preprocess ana behavior. Do you have live example of this problem to let me check? – Eugene Aug 15 '15 at 5:11
  • Hi Eugene. I modified view_name for example purposes before posting it here and I totally missed that I made the name in preprocess different. Sorry for this confusion. In my local test site the view name is same in preprocess and behavior. Unfortunately I do not have a live example to share with you. What do you mean by "view-dom-id is wrong" - Here: targetedView = '.view-dom-id-' + element.settings.view_dom_id;? – oksana-c Aug 15 '15 at 9:25
  • about caching: I do not cache any pages right now, also in View setting I have "caching:none, block caching: do not cache". Checked all permission on content generated by Views: all accessible for anonymous, still have a problem, so it's not cache or permission issue... – oksana-c Aug 15 '15 at 9:47
  • I mean that view-dom-id can be changed due the some reasons. But it's not simple to check it without live example. – Eugene Aug 15 '15 at 11:59
  • You can try enable Bypass Access check in your view if it's not critical. Also you can try grant access for all roles you need in access settings manually. If I got things right – ar7max Aug 15 '15 at 13:10
1

I found that solution that works for me even under anonym. Stars - changes

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

        **var viewName = 'ajax_test';**

        // View is Ajax enabled,
        // and is available as a property of the Drupal object
        // like so...
        **var instances = settings.views.ajaxViews;**
        var targetedView;

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

        // Define your button click here...
        $('.viewsrefreshbtn').click( function(){
                if(typeof targetedView !== 'undefined'){
                    $(targetedView).trigger('RefreshView'); 
                    console.log ('Clicked refresh btn on view. View Refreshed');
                }
                else {console.log ('Can\'t find viewName on this page');}
        });            
    }
}
})(jQuery);
  • Your solution works wonderful! and I need to spend more time improving my JS :) Thanks for all your help! – oksana-c Aug 15 '15 at 15:35
  • You are welcome, glad to help – ar7max Aug 15 '15 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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