I'm using this code to output a view in a block:

$view = views_get_view('bloggar');
$view->init();
$view->set_display('block_1');
$view->set_use_pager('mini');
$view->set_pager_element(0);
$view->set_arguments(array($arg));
$view->set_use_ajax(TRUE);
$view->set_offset(1);
$view->set_items_per_page(2);
return $view->render();

When use_ajax is set to TRUE, the pager bugs out, just appending results to the original rows, not paging them. When set to FALSE, paging works as expected.

I'm not really sure about what methods to call when rendering a view this way, so it might be that I'm missing something.

Comments

blackdog’s picture

Title: Pages not working with AJAX » Paging not working with AJAX

Wrong title.

nedjo’s picture

I haven't looked at if/how ajax would work in this context.

Can you explain in more detail the result you get? Any Javascript errors? Can you point me (via my contact form) to an example site?

merlinofchaos’s picture

What if you use $view->preview() rather than $view->render()?

blackdog’s picture

Status: Active » Fixed

So now I've got the paging working, not really sure why though, because it's the same code as above, I've only changed the argument in the view to use user names, which works great.

Might have been a caching issue before, not sure. So it seems AJAX can work in this context, which is great! Thanks!

I've exported the view if you want to have a look.

$view = new view;
$view->name = 'bloggar';
$view->description = 'bloggar';
$view->tag = 'bloggar';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Standard', 'default');
$handler->override_option('fields', array(
  'title' => array(
    'label' => '',
    'link_to_node' => 1,
    'exclude' => 0,
    'id' => 'title',
    'table' => 'node',
    'field' => 'title',
    'relationship' => 'none',
  ),
  'name' => array(
    'label' => '',
    'link_to_user' => 0,
    'exclude' => 0,
    'id' => 'name',
    'table' => 'users',
    'field' => 'name',
    'relationship' => 'none',
  ),
  'created' => array(
    'label' => '',
    'date_format' => 'small',
    'custom_date_format' => '',
    'exclude' => 0,
    'id' => 'created',
    'table' => 'node',
    'field' => 'created',
    'relationship' => 'none',
  ),
  'teaser' => array(
    'label' => '',
    'exclude' => 0,
    'id' => 'teaser',
    'table' => 'node_revisions',
    'field' => 'teaser',
    'relationship' => 'none',
  ),
));
$handler->override_option('sorts', array(
  'created' => array(
    'order' => 'DESC',
    'granularity' => 'minute',
    'id' => 'created',
    'table' => 'node',
    'field' => 'created',
    'relationship' => 'none',
  ),
));
$handler->override_option('filters', array(
  'type' => array(
    'operator' => 'in',
    'value' => array(
      'blog' => 'blog',
    ),
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'type',
    'table' => 'node',
    'field' => 'type',
    'relationship' => 'none',
  ),
));
$handler->override_option('access', array(
  'type' => 'none',
));
$handler->override_option('title', 'Bloggar');
$handler->override_option('use_ajax', TRUE);
$handler = $view->new_display('page', 'Huvudsida', 'page_1');
$handler->override_option('items_per_page', 1);
$handler->override_option('path', 'bloggar');
$handler->override_option('menu', array(
  'type' => 'normal',
  'title' => 'Bloggar',
  'weight' => '7',
  'name' => 'primary-links',
));
$handler->override_option('tab_options', array(
  'type' => 'none',
  'title' => '',
  'weight' => 0,
));
$handler = $view->new_display('block', 'Block', 'block_1');
$handler->override_option('arguments', array(
  'name' => array(
    'default_action' => 'ignore',
    'style_plugin' => 'default_summary',
    'style_options' => array(),
    'wildcard' => 'all',
    'wildcard_substitution' => 'Alla',
    'title' => '%1',
    'default_argument_type' => 'fixed',
    'default_argument' => '',
    'validate_type' => 'none',
    'validate_fail' => 'ignore',
    'glossary' => 0,
    'limit' => '0',
    'case' => 'ucfirst',
    'path_case' => 'ucfirst',
    'transform_dash' => 0,
    'id' => 'name',
    'table' => 'users',
    'field' => 'name',
    'override' => array(
      'button' => 'Use default',
    ),
    'relationship' => 'none',
    'default_options_div_prefix' => '',
    'default_argument_user' => 0,
    'default_argument_fixed' => '',
    'default_argument_php' => '',
    'validate_argument_node_type' => array(
      'blog' => 0,
      'forum' => 0,
      'artikel' => 0,
      'event' => 0,
      'image' => 0,
      'nyhet' => 0,
      'page' => 0,
      'video' => 0,
    ),
    'validate_argument_node_access' => 0,
    'validate_argument_nid_type' => 'nid',
    'validate_argument_vocabulary' => array(
      '1' => 0,
      '2' => 0,
      '4' => 0,
    ),
    'validate_argument_type' => 'tid',
    'validate_argument_node_flag_name' => '*relationship*',
    'validate_argument_node_flag_test' => 'flaggable',
    'validate_argument_node_flag_id_type' => 'id',
    'validate_argument_user_flag_name' => '*relationship*',
    'validate_argument_user_flag_test' => 'flaggable',
    'validate_argument_user_flag_id_type' => 'id',
    'validate_argument_php' => '',
  ),
));
$handler->override_option('items_per_page', 2);
$handler->override_option('offset', 1);
$handler->override_option('use_pager', 'mini');
$handler->override_option('block_description', 'Views: bloggar under annons');
$handler->override_option('block_caching', -1);
$handler = $view->new_display('page', 'Personsida', 'page_2');
$handler->override_option('arguments', array(
  'name' => array(
    'default_action' => 'not found',
    'style_plugin' => 'default_summary',
    'style_options' => array(),
    'wildcard' => 'all',
    'wildcard_substitution' => 'Alla',
    'title' => '%1\'s blog',
    'default_argument_type' => 'fixed',
    'default_argument' => '',
    'validate_type' => 'php',
    'validate_fail' => 'not found',
    'glossary' => 0,
    'limit' => '0',
    'case' => 'ucfirst',
    'path_case' => 'ucfirst',
    'transform_dash' => 0,
    'id' => 'name',
    'table' => 'users',
    'field' => 'name',
    'override' => array(
      'button' => 'Use default',
    ),
    'relationship' => 'none',
    'default_options_div_prefix' => '',
    'default_argument_user' => 0,
    'default_argument_fixed' => '',
    'default_argument_php' => '',
    'validate_argument_node_type' => array(
      'blog' => 0,
      'forum' => 0,
      'artikel' => 0,
      'event' => 0,
      'image' => 0,
      'nyhet' => 0,
      'page' => 0,
      'video' => 0,
    ),
    'validate_argument_node_access' => 0,
    'validate_argument_nid_type' => 'nid',
    'validate_argument_vocabulary' => array(
      '1' => 0,
      '2' => 0,
      '4' => 0,
    ),
    'validate_argument_type' => 'tid',
    'validate_argument_node_flag_name' => '*relationship*',
    'validate_argument_node_flag_test' => 'flaggable',
    'validate_argument_node_flag_id_type' => 'id',
    'validate_argument_user_flag_name' => '*relationship*',
    'validate_argument_user_flag_test' => 'flaggable',
    'validate_argument_user_flag_id_type' => 'id',
    'validate_argument_php' => '$result = db_result(db_query("SELECT name FROM {users} WHERE name = \'%s\'", $argument));
    if ($result) {
    return TRUE;
    }
    else {
    return FALSE;
    }',
  ),
));
$handler->override_option('items_per_page', 1);
$handler->override_option('path', 'blogg');
$handler->override_option('menu', array(
  'type' => 'none',
  'title' => '',
  'weight' => 0,
  'name' => 'navigation',
));
$handler->override_option('tab_options', array(
  'type' => 'none',
  'title' => '',
  'weight' => 0,
));

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

summit’s picture

Interesting, bookmarking, greetings, Martijn