I am working on a Profile display panel. The Panel has quite a few different panes on it. Is there a way to use ajax so that when you click on a link in a far right panel (just a custom content panel), the content in the center panel changes? I've got 3 different blocks set up, and what I want to do is have it switch the content in the middle panel to 1 of those 3 blocks without refreshing the page. Any suggestions?

Thanks for making panels! Its a life saver.

Comments

merlinofchaos’s picture

Right now there isn't a tool that will do this, but it's a popular want.

It seems likely that at some point this kind of thing will be written but it is a lot more complicated than it sounds. The biggest difficulty, of course, is that the content has to be somewhere, and it has to be somewhere that it can be fetched from. A lot of the time this is going to be highly specialized to what the content is, so I don't know that we'll be able to come up with a general tool to do it, but we shall see. I predict that it'll be easiest to do with Views which already has good support for ajax updating views with arguments, and should only be a little bit of code to make something like that work.

gbilling’s picture

Any suggestions as to sources I might look at to get a feel for how to write some code to hook ajax up to the panel view so that it would switch out the content? Thanks!

frankcarey’s picture

subscribing, will report back if i find some solutions

milksamsa’s picture

subscribing

gbilling’s picture

ok, we figured it out by doing some ajax coding and good stuff like that.

First, we set up some page views to output the data that we wanted to replace. We gave them unique urls too. for us, they needed to be associated with specific nodes as well, so the address was something like ?q= faculty_bio/% (the view has an argument for node id, so that is what we would pass to the pages)

then, using the devel module, we were able to find the name for the most specific .tpl.php file. We then took the page.tpl.php file and copied only:

<div id="content-area">
          <?php	  
		  print $content; ?>
       </div>

that made it so following the link would bring up a page that only had the text of the information we wanted to dynamically display in ajax.

Next, we made a custom php pane on the panel view and inside it put this code:


$block->content =  "<div id=\"faculty_ajax\"><a href=\"?q=faculty_rep/" . arg(1). "\">Representative Publications</a><br><br><a href=\"?q=faculty_other/" . arg(1). "\">Book Reviews and Other Publications</a><br><br>  <a href=\"?q=faculty_bio/" . arg(1). "\">Biography</a></div>";

Note: $block->content is the variable where you store all the content you want that panel to display.

So, in here, we created a div and gave it an id. in our case "faculty_ajax" this is so that we can use a script.js file to find it and be triggered on a click event. this Div encompasses all 3 of the links we wanted to use to dynamically change the page. Do note, at the end of each link, where the node_id is supposed to be, we could just drop in arg(1) or whatever, because in our case, that is the 2nd argument in the current url. (the first being "node" since it is a node we are looking at)

You want to be sure before you set the ajax up, that each link will go and display only the data that you want to replace the current data.

ok, next was taking each of the pages that we set up in the first step and rewriting their output to include a div with an id. For us, that id was "faculty_content_ajax". It will need an id so that your script.js file can find it when one of your links is clicked and replace the entire div with the information at the url of the clicked on link. make sure that the closing div tag is after your [content] brackets.

Now, with that all working, you are ready to make your script.js file for drupal to find. you need to make sure that in your current theme's .info file ( {theme_name}.info ) the section where it says "Optionally add some jquery scripts to your theme." then lists scripts[] = (it might be commented out on yours if you havent loaded any java script files) just add this line:

scripts[] = script.js

now you need to in the same theme folder create yourself a script.js file.
here is the code that ended up going into our script.js file:

$(document).ready(function(){
						   
$('#faculty_ajax a').click(function(){
 $('#faculty_ajax_content').load($(this).attr('href'));
 return false;
});
}); 

ok, what does it do?
the:

 "$(document).ready(function(){})"

makes sure the page is ready.

the:

"$('#faculty_ajax a').click(function(){" 

adds a click event for all a tags with id of "faculty_ajax" (if you remember, we put that id on our links in the php pane.)

the:

 "$('#faculty_ajax_content').load($(this).attr('href'));
 return false;"

tells the script what to do on a click event for any a tag with id "faculty_ajax" to find an element (in our case a div) with the id "faculty_ajax_content" and replace it with the content we pull from the referenced url of the a tag.

and with that, you get a panels page that has links in one place, that when you click on them, it replaces the content of your choosing with that in another page without loading the current page again.

Good luck! Let me know if anyone has any questions.

frankcarey’s picture

Thanks! We'll give this a shot next week

ChrisBryant’s picture

Subscribing... We're also looking to do something like this on a project soon so tracking this. Will post back on what we figure out as well.

dkruglyak’s picture

Title: AJAX to switch content in panel display » AJAX to load content in panel display
Version: 6.x-3.0-alpha4 » 6.x-3.x-dev
Category: support » task

There should also be an option to not just "update" but always load original content via AJAX. There are cases when you have content that should never show up in source HTML.

This probably should be a feature built-in the module, at the pane level.

nco71’s picture

Subscribe. This should be usefull for me too has my boss want to get dynamic sub menu display ( but on every link the page refresh to load the news view or fiter it )
I m using taxonomy menu with customized path for that.

nco71’s picture

Dynamic views look promising for what I would need to do ... ( limited to the same view )
However it s an old module only for drupal 5.x

http://drupal.org/project/dynamic_views

cgjohnson’s picture

Is there an easy way to use panels to load an iframe into a separate panel pane? (so when a user clicks on the link URL in a left-hand pane, it loads the iframe into a right-hand pane?) thanks.

gbilling’s picture

Well, if you have a knowledge of how AJAX works and simply create a page (you can create it outside of drupal if you want, all you have to do is reference an address, so really, any existing webpage anywhere on the internet can be included.) if you do it in drupal though, you will probably have to mess with a .tpl.php file too so you can cut out all the drupal stuff from it.

You will have to create a div in the destination panel with an id that you will then use in your ajax call. You will also need an <a> tag somewhere with an href that calls a javascript function (or even the ajax itself) to set the named div's content. to do Javascript in an href, it looks like this: <a href="javascript: .... CODE HERE .... ;">My Ajax</a> (no semi-colon necessary if its just 1 function or statement.)

Parkes Design’s picture

subscribing

mrfelton’s picture

subscribing

LEternity’s picture

subscribing

pribeh’s picture

subscribing.

crea’s picture

Subscribing

colin_young’s picture

One way I see this working is to use a context in the target panel (that's the one where you want to update the content). Then there would be some mechanism (probably a jQuery event to allow multiple panels to register listeners for their contexts) that would update the context of the target panel. Then the target panel would run through whatever it does to render only the html for that panel and return it to the browser.

This approach leverages the features that already provide the flexibility and power of panels, and the only work required from the user would be wiring up the registration of the context changes. At the very least it would solve my needs :)

I am of course assuming that the primary use case for this functionality would be to change things like arguments to views or nodes, rather than major modifications like switching from a view to a node for instance.

ppcc’s picture

subscribing

Nestor’s picture

subscribing

pankaj01’s picture

subscribing

g76’s picture

subscribing

ryanrain’s picture

subscribing

thrash632’s picture

subscribing

hutch120’s picture

subscribing

mtpultz’s picture

subscribing

tugis’s picture

subscribing

mazdakaps’s picture

subscribing

goatsee’s picture

subscribing

director87’s picture

subscriber #30! :)

geosmaga’s picture

subscribing

Countzero’s picture

Subscribing

drupalok’s picture

Subscribing

mirie’s picture

I saw an interesting tutorial on one way to accomplish this: http://www.gizra.com/content/views-and-panels-ajaxified. The article referred to in the beginning of the tutorial is very useful to read (http://zroger.com/2009/12/ajax-without-javascript/) as it gives an overview on using CTools commands.

flavio_tito’s picture

Subscribing

aaronelborg’s picture

#34 is for D6. Just sayin.

neRok’s picture

I have a similar problem on D7 and have been searching for solutions. This issue came up on google, so thought I would post what I have found so far.

One option is with the module Load content via JQuery Ajax, which is for D6 and D7. You can make an ajax link to replace a specified div on the current page with the page that ajax loads in the link. The only problem with this is the content you want to load has to be obtainable at its own URL (not really suitable for a block).

Another module that is D7 only at the moment is Panels Ajax Tabs. It provides a set of tabs and loads mini panels on demand 'within' itself.

drupalye’s picture

cyberzeus’s picture

Issue summary: View changes

FYI - the Ajax w/o Javascript link is busted - here's the new one: http://zroger.com/blog/ajax-without-javascript/

japerry’s picture

Status: Active » Closed (works as designed)

Fixed in D6.