3

I have a View of created entities displayed as a block. On the same page I have a form with which new entities can be created, and this form submits through AJAX because I do not want a page reload after submission. The issue of coarse though is that after submission, the View on the same page is not updated which makes it look like that the submit has done nothing. I could set a Rule to reload the page after submission, but I would like the View to update without reload.

What I have tried (Drupal 7)

I have experimented with the Views Auto-Refresh module, but constantly refreshing a View seems a bit of overkill since for performance reasons it would be better to only have this triggered on a form submit. The module Views Flag Refresh does exactly what I need but because it is a Flag and not a form element I am not sure how I could use a flag to submit the contents of a form. The following module does exactly what I need and refreshes a View after a form submit, but there is only a D6 version (Ajax Views Refresh) with no plans for a D7 port. The final possibility I have considered is Node.js integration (Views Node.js), but this seems hugely complex for such a small feature and I am not that confident in my ability to pull this off.

So at this point I'm not quite sure in which direction to go. How could I do a refresh after a form submit?

  • You can do it with a module like 'Ajax Blocks', but I suspect you will end up customizing a lot of the code. Else just write your own Ajax routine. Hook into the javascript to detect when the previous ajax call (entity create) completed, then call your own ajax routine. In this routine you get the block (views_embed_view or any of the myriad ways to do that) and return the result as plain html, and do a replace on the block back in the jquery ajax success handler. If you google 'drupal load block ajax' you will find plenty examples. – J. Reynolds Oct 26 '14 at 4:39
  • Unfortunately I'm not very familiar with writing Ajax calls and routines as such. So there is no module solution to what I need to achieve? It seems like a quite common feature I would think in modern websites, and the Ajax Views Refresh module accomplished this in way back D6 so isn't there a D7 alternative? – FrontEnd Oct 26 '14 at 9:07
  • 1
    I can't definitively say whether there is no module. Ajax Blocks is pretty close. It loads a block using AJAX after the page is loaded, so you can see if you can alter it to load the block on demand. Another option would be a completely client-side solution of using jQuery to create a new row in the view markup. That way there is no round trip to the server at all. Maybe someone else will have a better solution to offer you. – J. Reynolds Oct 26 '14 at 13:05
  • @FrontEnd Did you get it working? I am using "formblock" module. It exposes node forms as blocks. Then I put the node form block in the header area on my view. Now the problem is, How to update the view after submit with ajax. Currently it redirects to the created node page. – Umair Jun 7 '17 at 11:40
1

I think this might be the module you are looking for:- https://www.drupal.org/project/views_nodejs

I has a few dependencies, and you will need to setup a node.js server, but aside from that it's pretty straightforward.

0

You can create a simple module with hook menu which loaded ajax callback function.

I found some useful tutorial on Youtube: https://www.youtube.com/watch?v=zyLmQtiSXgI

You may find sample source code here: https://github.com/seanbuscay/drupal-ajax-demo

  • Hello and Welcome to Drupal Answers. You can improve your answer by providing more detailed information on how to achieve this. – Wtower Oct 27 '14 at 15:55
0

You can write your own ajax (using Drupal Behaviour method) callback to append the updated values to you particular block.

I hope in your block you will be showing title, description and image (if need),

So using your ajax callback you can get submitted data (whatever you want) from the database. Then you can append those values in existing block without refreshing page.

This will give better user experience with less development work.

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.