0

From this great answer I have a working jQuery implementation of a simple call to an existing view using AJAX. Which is brilliant, but I want to specify the number of results differently to as is setup in the existing view, and I want to know what other options I can use under view_args.

I can't find documentation/a full example of what's available and how to use it. Is there any?

$.ajax({
    url: url,
    type: 'post',
    data: {
        view_display_id: v_display,
        view_name: v_name,
        //   view_args: {}, // your views arguments
        view_args: v_args,
    },
    dataType: 'json',
    success: function (response) {
        if (response[1] !== undefined) {
            return response[1].data; // do something with the view
        }
    }
});

I inspected the HTML of the Views admin page and saw the pager was encapsulated from the other arguments, and tried this but it carried on supplying 6 items (which is odd because the view is saved to display 10):

var data = {
    'view_name':'ajax_test',
    'view_display_id':'block',
    'pager_options': {
        'items_per_page':'1',
    }
};
$.ajax({
  url: '/views/ajax',
  type: 'post',
  data: data,
  //pager_options: {items_per_page:1},
  dataType: 'json',
  success: function (response) {
    if (response[1] !== undefined) {
      document.write(response[1].data); // do something with the view
    }
  }
});
0

Previous experience tells me with Drupal 7 that if I don't find an answer after a day of research then people probably haven't approached it like this and I'm unlikely to find much assistance, so I'm doing the custom menu item approach instead, as people have suggested in the discussions I've read.

/**
 * Implements hook_menu().
 */
function mymodule_menu() {
    $items['ajax/%'] = array(
        'page callback' => 'get_view_with_args_get_ajax',
        'page arguments' => array(1),
        'type' => MENU_CALLBACK,
        'delivery callback' => 'ajax_deliver',
        'access arguments' => array('access content'),
    );

    [....etc....]


function get_view_with_args_get_ajax($args) {
    views_get_view($name);
    return 'Args: ' . $args;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.