There is a nice event to trigger view reload.
Simple call $('.view.view-some-id').trigger('RefreshView'); handles all automatically.
But there is one small bug: if pager is enabled, view reloads as if the last pager link was clicked.
js/ajax_view.js line 63
// Add the ajax to pagers.
this.$view
// Don't attach to nested views. Doing so would attach multiple behaviors
// to a given element.
.filter(jQuery.proxy(this.filterNestedViews, this))
.once(jQuery.proxy(this.attachPagerAjax, this));
// Add a trigger to update this view specifically.
var self_settings = this.element_settings;
self_settings.event = 'RefreshView';
this.refreshViewAjax = new Drupal.ajax(this.selector, this.$view, self_settings);
attachPagerAjax function alters this.element_settings for each pager link.
So var self_settings = this.element_settings; receives same settings as the last processed pager link.
Swapping this two parts of code solves the problem, patch attached.
Comments
Comment #1
lunazoid commentedI was noticing the same issue, and the patch resolved the problem.
Comment #2
lunazoid commentedScratch my last comment. It appears that when this patch is applied, it breaks the ajax pager.
Comment #3
phponwebsites commentedI've also same issue. I tried to add this patch but ajax pager is not working. Is any other solutions to solve this problem?
Comment #4
Niremizov commentedConfirm, after applying this patch ajax pager - works as normal (not ajax) pager.
Comment #5
Niremizov commentedComment #6
Niremizov commentedAttached patch fixes issue partially. Pager behaviour fixed, but after triggering RefreshView filters are not applied. Also solution looks crappy, but that is for now.
Comment #7
edodusi commentedPatch in #6 only force the view to be refreshed with pager 0, which can be ok but it's not the main issue.
I've done some debugging and it seems to me that after refreshing a view with a pager, the argument settings here remains unchanged even if you apply new arguments or filters:
Drupal.views.ajaxView = function(settings) {...}So after you refresh a view with pager, you cannot filter anymore.
It seems a very strange behavior and I was not able to patch it correctly, so for now, I cannot use the handler 'RefreshView' and a pager.
Comment #8
edodusi commentedFound a hack but it doesn't work well.
Comment #9
edodusi commentedBefore calling
$(view_selector).triggerHandler('RefreshView');I save my current filters:Drupal.settings.views.ajaxViews['views_dom_id:' + dom_id].current_view_args = myCurrentArgs;Then in ajax_view.js I apply the patch in #6 and recover my saved args:
First click on refresh link that returns a result with pager, view refreshed, pager 0.
Second click on refresh link that returns a result with or without pager, view NOT refreshed, pager 0.
Third click on refresh link that returns a result with or without pager, view refreshed, pager 0.
Hope this helps.
Comment #10
ctmun commentedIn Drupal.views.ajaxView.prototype.attachPagerLinkAjax function, make a copy of this.element_settings to use within the function instead of altering it directly.
js/ajax_view.js replace line 124-125 with:
Comment #11
ctmun commentedAttaching patch with changes to js/ajax_view.js for testing.
Comment #12
ctmun commentedComment #13
matysek145 commentedI had same problem with Views Load More module.
With patch #11 everything works fine - thanks!
Comment #14
edodusi commentedI can confirm thath the patch in #11 fixes the issue.
Comment #15
cvharris commentedCan confirm #11 works for me too!
Comment #16
maximpodorov commentedNew module is released for views reloading: https://www.drupal.org/project/views_refresh
Comment #17
huntelaer commented#11 Also solved my problem with a table view which was automatically sorted after triggering 'RefreshView'
Comment #18
daveferrara1 commentedUpdated patch for 7.x-3.16
Comment #19
daveferrara1 commentedComment #20
ugintl commentedInstead of updating the view, how to show a link "Click to see new items". When user clicks, it should then update the view with new items. I am using statuses module which shows status updates of a user and it has built-in refresh AHAH, but it is not working.
Comment #21
chris matthews commentedThe 2 year old patch in #18 to ajax_view.js does not apply to the latest views 7.x-3.x-dev.
Comment #22
kengilb commentedRerolled #18 against 7.x-3.21
Comment #23
kengilb commentedComment #24
indigoxela commentedIn the meantime views 7.x-3.23 (with a lot of bugfixes) has been released. Patch #22 doesn't apply anymore, we need to reroll it.
My ajax skills aren't sufficient for that. Hopefully one of the issue followers can do it.
Comment #25
indigoxela commentedI took some time to read this thread thoroughly. It seems, patch #11 (attachPagerLinkAjax) is the right approach, as it has been confirmed to work properly by 4 people.
So I created a new patch following that concept - credits should go to @ctmun
It works fine here and doesn't seem to break anything. Please review.
Comment #26
marcuscoia commentedI just started having this issue in Views 7.x-3.24; not sure why it wasn't an issue in 3.23, but patch #25 fixed it.
Comment #27
alex.bukach commented#25 worked for me too. I believe it's should be considered as RTBC.
Comment #28
mustanggb commentedI ran into this as well, and created patch similar to #11/#25 in #3172741: RefreshView has a forever changing click_sort.
Comment #29
mustanggb commentedNow the approach has some good support and confirmation of success, let's get this in.
Comment #31
damienmckennaCommitted. Thank you all for your work to fixing this.