views:

8

answers:

1

Hello fellow drupalers, I have a question about ajax loaded views. Lets say i have 15 items, and i have the pager to show 5. Now i want to have a button that says show "more". when the button is pressed i want the view to load an additional 5 items in the view. So if i have lets say animals. cat,dog,panda,snake,worm,zebra,lion if the pagers is at 3 it should show cat,dog,panda. On button press it should load 3 more items, and the view should now say cat,dog,panda,snake,worm,zebra, if i press again it should show the whole list. I don't know how to do this, someone could possibly point me in the right direction? The content is rather heavy, so a display none solution and jquery would be unacceptable.

A: 

Do the ajax callback yourself but use views for the actual query.

Set up a URL for the ajax callback in hook_menu() and then in the callback figure out which "page" is being requested and set it programatically in the callback & return just what you need.

function general_menu() {
    $item['callback/url'] = array(
        'type' => MENU_CALLBACK,
        'page callback' => 'my_ajax_callback',
        'access callback' => TRUE,
    );
}

function my_ajax_callback() {
    $page = $_REQUEST['page'] ? $_REQUEST['page'] : 0;
    $view = views_get_view('{view_name}');
    $view->set_display('{display_name}');
    $view->set_current_page($page);
    $view->pre_execute();
    $view->execute();
    print $view->render();
    exit;
}

You'll have to do the ajax request yourself and manage the current page on the client-side, too, but it shouldn't be that hard. Also, you might need to override some view template files so you don't get all the view wrappers when requesting more items, only the items themselves. When the ajax call returns you can just insert the result into the DOM below the other items.

jonathanpglick

related questions