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
Comment #1
merlinofchaos commentedRight 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.
Comment #2
gbilling commentedAny 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!
Comment #3
frankcarey commentedsubscribing, will report back if i find some solutions
Comment #4
milksamsa commentedsubscribing
Comment #5
gbilling commentedok, 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:
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:
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:
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:
ok, what does it do?
the:
makes sure the page is ready.
the:
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:
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.
Comment #6
frankcarey commentedThanks! We'll give this a shot next week
Comment #7
ChrisBryant commentedSubscribing... 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.
Comment #8
dkruglyak commentedThere 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.
Comment #9
nco71 commentedSubscribe. 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.
Comment #10
nco71 commentedDynamic 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
Comment #11
cgjohnson commentedIs 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.
Comment #12
gbilling commentedWell, 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.)Comment #13
Parkes Design commentedsubscribing
Comment #14
mrfelton commentedsubscribing
Comment #15
LEternity commentedsubscribing
Comment #16
pribeh commentedsubscribing.
Comment #17
crea commentedSubscribing
Comment #18
colin_young commentedOne 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.
Comment #19
ppcc commentedsubscribing
Comment #20
Nestor commentedsubscribing
Comment #21
pankaj01 commentedsubscribing
Comment #22
g76 commentedsubscribing
Comment #23
ryanrain commentedsubscribing
Comment #24
thrash632 commentedsubscribing
Comment #25
hutch120 commentedsubscribing
Comment #26
mtpultz commentedsubscribing
Comment #27
tugis commentedsubscribing
Comment #28
mazdakaps commentedsubscribing
Comment #29
goatsee commentedsubscribing
Comment #30
director87 commentedsubscriber #30! :)
Comment #31
geosmaga commentedsubscribing
Comment #32
Countzero commentedSubscribing
Comment #33
drupalok commentedSubscribing
Comment #34
mirie commentedI 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.
Comment #35
flavio_tito commentedSubscribing
Comment #36
aaronelborg commented#34 is for D6. Just sayin.
Comment #37
neRok commentedI 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.
Comment #38
drupalye commentedhttp://www.gizra.com/content/views-and-panels-ajaxified
I guess you guys may want this.
Comment #39
cyberzeus commentedFYI - the Ajax w/o Javascript link is busted - here's the new one: http://zroger.com/blog/ajax-without-javascript/
Comment #40
japerryFixed in D6.