Title says it all in views 3 is there a short snippet of javascript that will trigger an ajax view refresh.
This is what I have scrapped together so far from ajax_view.js and views_autorefresh, but as soon as the "data" parameter is uncommented it flops and the page will timeout after the js is triggered.
if (Drupal.settings && Drupal.settings.views && Drupal.settings.views.ajaxViews) {
$.each(Drupal.settings.views.ajaxViews, function(i, settings) {
var selector = '.view-dom-id-' + settings.view_dom_id;
this.$view = $(selector);
// Retrieve the path to use for views' ajax.
var ajax_path = Drupal.settings.views.ajax_path;
// If there are multiple views this might've ended up showing up multiple times.
if (ajax_path.constructor.toString().indexOf("Array") != -1) {
ajax_path = ajax_path[0];
}
$.ajax({
url: ajax_path,
type: 'GET',
//data: settings,
success: function(response) {
console.log('success');
console.log(response);
},
error: function(xhr) {
console.log('error');
console.log(xhr);
},
dataType: 'json',
});
});
}
Comments
Comment #1
drewish commentedI had a view with a couple of items that I needed to refresh in place. It had no pagination, just the 3 most recent items so I enabled AJAX on the view and added this little snipped of JS to refresh it every 30 seconds.
Comment #2
mustanggb commentedComment #3
imclean commentedThis keeps coming up in searches so here's an easy method for refreshing an ajax view using the event RefreshView.
See first post for context.
Also see
.trigger()if you'd prefer.Comment #4
rob c commented$(selector).triggerHandler('RefreshView');
is introduced in views 3.8. (for everybody that's like me searching MAO about this). (but awesome this is in! Now we can build some really Great after ajax stuff etc etc.)
Comment #5
rerooting commentedNice!
I had views rendering in multiple blocks and wanted to refresh the first one, and thus I was able to improvise and do this:
This could be handy in case you want to trigger the ajax reload on multiple views as well, since this allows you to iterate through the ajaxView object's properties numerically (which will be your views). The property name is views_dom_id:s89df98sd7f89sd7 if you wanna deal with the hot mess that way as well with a string method. I'm also noticing that you can do things here like manipulate the views argument... hmm!
Comment #6
imclean commentedHere's an example looking for a specific display ID and adding some arguments:
Comment #7
pal4life commentedWhere do we add this code on? Inside the view or in a separate js file that gets loaded on the view page
var views = settings.views.ajaxViews;
var dom_id = views[Object.keys(views)[0]]['view_dom_id'];
var selector = '.view-dom-id-' + dom_id;
$(selector).triggerHandler('RefreshView');
Comment #8
imclean commented@pal4live
In your own module or theme's javascript. Don't forget behaviors etc.
https://www.drupal.org/node/756722
Comment #9
trw commentedThanks to @imclean and @rerooting for great examples! These have gotten me very close to where I need to be. I'm stuck on the last step. I'm getting the dom_id of the view (it's the first and only on the page) but I can't quite figure out how to get the argument to change what shows in the view. In this example, I'm updating the default value in view_args from 8 (set in Contextual Filters) to 9. All of that seems to be happening, but the view doesn't every seem to update.
Comment #10
trw commentedHi all,
I've been digging further and I think the place I'm stuck is that I don't understand in this line:
jQuery(selector).triggerHandler('RefreshView');where "RefreshView" is defined. In all of the examples I've seen, triggerHandler() is calling something that was previously defined.
Any help appreciated.
Tim
Comment #11
imclean commentedRefreshView is defined in Views. #4 mentions it was introduced in Views 3.8.
Comment #12
trw commentedThanks, @imclean. I missed that. Do you have any ideas for troubleshooting this? It really sea like I've got we're thing but the last step working and am just stumped.
Comment #13
trw commentedI'm hoping someone can jump in and help me figure out how to troubleshoot this. Feeling like I've hit a brick wall even though it seems like I'm really close!
Comment #14
leolandotan commentedI'm also trying to refresh a view after an ajax popup form submits but I cant seem to make the jQuery code work. I just tried to trigger the refresh when I click the table because initially I want it to be part of an ajax_command array.
I'd like to replace this line $output[] = ajax_command_replace('#user-list', mymod_user_list()); to a line that calls the jQuery file but right now i'm just keeping it simple but still doesn't seem to work.
This is the simple jQuery code that doesn't work but the console "passed" is always printed.
Anything I missed here?
Comment #15
anybodyThis blog article I've just written about it shows implementation of a Views AJAX refresh via JavaScript (jQuery) or even PHP and may help you and others in the future:
http://julian.pustkuchen.com/en/drupal-7-api-trigger-views-ajax-refresh-...
Happy coding!
Comment #16
awasson commentedCAUTION: The link in the comment by Anybody #15 leads to a page that is affected by a phishing attack. Do not follow it.Julian: You better have a look into what that's about.EDIT: It looks like this isn't a phishing attempt after all but maybe the result of an overly aggressive advertisement mechanism on the blog.
Comment #17
anybody@awasson: I've sent you a pm! It's my private blog and there should be no phishing. Please lets have a quick conversation. The link should definitely be clean!
Virus / security checks are all absolutely okay, see:
https://www.virustotal.com/de/url/71dcea9b401a496a8da9a8a76f06f2c858c176...
http://scanurl.net/?u=http%3A%2F%2Fjulian.pustkuchen.com%2Fen%2Fdrupal-7...
Comment #18
awasson commentedSounds good.
Comment #19
phponwebsites commentedWhen i tried to refresh view in jquery, pager in views is run automatically. how to stop running of pager in views?
Comment #20
core44 commentedhaving the same issue as phponwebsites above. When using
$(selector).triggerHandler('RefreshView');my view refreshes nicely, but the pager also triggers and changes the view to the next page.
Comment #21
edodusi commentedI found a hack that solves the pager issue for me https://www.drupal.org/node/2199049#comment-10265551
Comment #22
maximpodorov commentedNew module is released for views reloading: https://www.drupal.org/project/views_refresh
Comment #23
ugintl commentedHas anybody landed on Ajax Views Refresh module? This is a very much needed module but unfortunately not for D7. May be someone who knows coding can copy some of the code and reuse it for D7
Comment #24
ugintl commentedI have also asked a question on statckexchange