Drupal 7 API: Trigger views AJAX refresh via JavaScript or from PHP using ajax_command_invoke

Drupal 7 views.module allows to refresh ajaxified views
dynamically via a javascript call.

Implementation in JavaScript

The JavaScript call is simple:

  1.   jQuery('.view-id-XXX').trigger('RefreshView');

Implementation in PHP

The AJAX refresh is even possible from PHP code, for example if you have an ajaxified form and want to refresh a view after it was saved, as the following examples show.

In an ajax callback function (..._ajax_callback):

  1. $commands[] = ajax_command_invoke('.view-id-XXX', 'trigger', array('RefreshView'));
  2.  
  3. return array(
  4.       '#type' => 'ajax',
  5.       '#commands' => $commands,
  6.     );

OR

  1. $commands[] = ajax_command_invoke('.view-id-XXX', 'trigger', array('RefreshView'));
  2.  
  3. ajax_deliver(array(
  4.       '#type' => 'ajax',
  5.       '#commands' => $commands,
  6.     ));

Comments

PS: The best solution for

PS: The best solution for AJAXified entity forms (nodes, ...) is this, because it already handles several typical problems out of the box:

https://www.drupal.org/project/ajax_form_entity

Can a form have multiple ajax

Can a form have multiple ajax callback functions? What is best way to invoke contributed module ajax callback ?

Thanks,

I have tried ajax form entity module

I want to attach or embed a node form to a view and submit the form via ajax and the view must also refresh via ajax in realtime. I have tried ajax form entity module. It is working, but I want some flexibility. Can you help? I have also asked a question on statckexchange. I cannot post link here so pasting it like this /questions/238447/attach-or-embed-node-add-form-to-a-view

Post new comment

The content of this field is kept private and will not be shown publicly.

More information about formatting options