Hello Viziontech,
This tutorial is amazing, I made some alterations to fit my site but everything worked 100% with the first cut/paste, keep up the good work!
B Jordan
function dynamic_products_get_by_category_id($cat_id){
$items = '';
switch($cat_id){
case 12:
$items = '<ul><li>Product 1</li><li>Product 2</li></ul>';
break;
case 20:
$items = '<ul><li>Product 3</li><li>Product 4</li></ul>';
break;
}
// create a JSON object. The object will contain a property named “products” that will be set with the $items variable.
return drupal_json(array('products'=>$items));
exit;
}
function dynamic_products_get_by_category_id($category){
$viewName = 'products_by_category'; // The name of the view we are going to load
$args = array($category); // Array of arguments we set for the view. Only one argument in our example. your actual view may require additional arguments which you may need to set
$displayId = 'page'; // The display id of for the view.
// Call the views_embed_view function to returned themed view output
$res = views_embed_view($viewName, $displayId, $args);
// create a JSON object. The object will contain a property named “products” that will be set with the themed result of the executed view.
return drupal_json(array('products'=>$res));
exit;
}
Few notes about the code:$args = array($category);
$res = views_embed_view($viewName, $displayId, $args);
return drupal_json(array('products'=>$res));
The views_embed_view function, which you can read more about it here, is called to return a themed result.
<div id=”topDiv”> <a class="categoryLink" href="/products/get/12">Cat. 1</a> <a class="categoryLink" href="/products/get/20">Cat. 2</a> </div> <div id="divProducts”></div>We are going to change it to pass the correct category according to what we defined in step 2 ("books" or "movies"):
<div id=”topDiv”> <a class="categoryLink" href="/products/get/books">Books</a> <a class="categoryLink" href="/products/get/movies">Movies</a> </div> <div id="divProducts”></div>
Hello Viziontech,
This tutorial is amazing, I made some alterations to fit my site but everything worked 100% with the first cut/paste, keep up the good work!
B Jordan
First of all, thank you for this tutorial! It's exactly what I was looking for and works great. On that note, people could be having problems populating the empty div because the code example is missing a closing > after "divProducts"
<div id="divProducts”</div>
should be
<div id="divProducts”></div>
Thanks again for both the Basic Ajax and Views tutorial.
I have been trying to figure out something like this for so long now, I've searched all over and asked for help numerous times on IRC with no luck! SO grateful!!
I just had to ask one question though, I implemented this on a site where several arguments are being passed into the view. The view displays teasers of the particular nodes, which are supposed to open modals (using popups module) when clicked. When I click to filter my view the correct markup is there for popups, but the JS doesn't work anymore.. any ideas?
I'm not having any luck getting a response from clicking the links as well.
i am kind of new to the whole ajax process and i have a view containing images on a page
i would like for you to be able to click on one of the images in the view and it is displayed on the same
page probably above the view so that image would change as long as you click on an image in the view. how would
i modify the code above to accomplish this
Hello Pabo,
There are couples of way to implement what you are looking for, but I think you better explorer existing solutions that are out there.
There are few modules that already implement this type of functionality, and you would save time using one of them.
Take a look at:
- Views Galleria - http://drupal.org/project/views_galleria
- Album Photos - http://drupal.org/project/photos
If you still need help, let me know.
Zion
Hey, great couple of tutorials :)
Thanks to things like this Drupal evolves!
I'm trying to make my self this, I have follow the steps, but I have a problem. I have created a view with a pager and AJAX enabled (in the View), and it doesn't work how is supposed to work, if loads a ""empty page" with only raw data from view. not header, footer, etc.
View works great in the preview, but not when loaded AJAX way
Does it happens to you?
Thank you
Anna.
Hello Anna,
Thanks for your comment about the tutorials.
Regarding the issues you are having, did you follow the first tutorial(http://www.viziontech.co.il/tutorial1)?
I suggest you follow it carefully, as it describes in details what should be done to implement Ajax requests with Drupal.
If the first tutorial works for you, and you’re still having problems with “view” implementation, give me a shout and I’ll try to do my best to help.
Best regards
Zion
Two points
Submitted by Joel (not verified) on Mon, 06/21/2010 - 17:19.Excellent tutorial. One suggestion and one gotcha:
1) SUGGESTION
To find the display_id, go to the Views page, where you define the View. Open Firebug and get to the Net tab. Preview the view you are interested in. Check the Post parameters that are listed in Firebug. You will that the display_id is listed there.
2) GOTCHA
When calling views_embed_view, note that if the $args parameter is a single string, DO NOT pass it as an array; just pass it as a string.
$viewName = 'my_custom_view'; // The name of the view we are going to load
$args = array('FOOBAR'); // Array of arguments we set for the view.
$displayId = 'page_1'; // The display id of for the view obtained with Firebug
// WRONG: passing the $args array will result in problems
$custom_view = views_embed_view($viewName, $displayId, $args);
// RIGHT: passing the string will work
$custom_view = views_embed_view($viewName, $displayId, 'FOOBAR');
In fact, this single line works for me:
$custom_view = views_embed_view('my_custom_view, 'page_1, 'FOOBAR');