0

I was wondering how I am able to find the position of an item in a views pager?

I have nodes listed using views (with pager, 20 every page), and I'd like to find out for example where in the pager one node is?

  • Take look here stackoverflow.com/q/5271121 – niksmac Apr 27 '12 at 7:20
  • Sorry but that didn't help either? – Luca Apr 30 '12 at 19:19
  • Do you want to have the position of the node in the current view (like 1 - 20), the overall position among all pages or the page number? – Paul May 4 '12 at 15:49
  • I don't have an answer at this point, but this is a very interesting issue, perhaps a candidate for a small module. I'm just wondering how you would like to refer to the node you want to find, by nid or some other parameter (text, title)...? And I'm BTW jealous you managed to get a pager to work. – Artur May 11 '12 at 12:11
1

EDIT (2015-04-05): Updated the snippet to work with newer version of Views.

Us too had to find the page number of a views result on which a certain node appears, so we can redirect visitors to this page.

By loading a view for a specific display and executing it's query manually, we retrieve an array with all node ids which we can search for a specific node id. When we have got the position of the node id, we divide it by the amount of items per page to get the page number the node is on.

I understand that loading all node ids at once in an array probably should not be done on large amounts of records, but for this exercise it's okay.

/**
 * Custom function to get the page number of a views result a node appears on.
 * Based on Drupal's views_get_view_result().
 */
function yourmodulename_views_get_item_page_nr($nid, $name, $display_id = NULL) {
  $args = func_get_args();
  array_shift($args); // remove $nid
  array_shift($args); // remove $name

  if (count($args)) {
    array_shift($args); // remove $display_id
  }

  $view = views_get_view($name);
  if (is_object($view)) {
    if (is_array($args)) {
      $view->set_arguments($args);
    }

    if (is_string($display_id)) {
      $view->set_display($display_id);
    }
    else {
      $view->init_display();
    }

    // Set the limit to 0 to retrieve all node ID's.
    $view->set_items_per_page(0);

    $view->pre_execute();
    $view->build();

    // Get the views query.
    $query = $view->build_info['query'];

    // Get the return assoc array with return objects, keyed by nids.
    $nids = $query->execute()->fetchAllAssoc('nid');

    // Get position of nid in nids array.
    $index = array_search($nid, array_keys($nids));

    // Get amount of items per page.
    $items_per_page = $view->display[$view->current_display]->handler->get_option('items_per_page'); // As seen at http://api.drupal.org/api/views/includes%21view.inc/function/view%3A%3Aexecute/7.

    // Calculate page number.
    $page_nr = $index === null ? 0 : intval($index / $items_per_page);

    return $page_nr;
  }
  else {
    return 0;
  }
}
| improve this answer | |
1

If you want to look at the css, it will tell you that in that each "item" is a row, and each are surrounded with a div that has classes like:

<div class="views-row views-row-## ... ">

and the views-row-## is the item number in that pager.

You can also use the Global: View result counter as a field, but that gives you the position in ALL of the results, not the page in question, but a quick manipulation of that and the page number in question will give you the result, too. You might be able to use ctools' Global: Math expression to do that for you if it isn't already there for you (you didn't say what versions/modules you are running.)

If you want to dig into the guts of the view in a template, you could also get this information from $view->result.

| improve this answer | |
0

Your problem is that you don't need that. I mean knowing the precise position of a node in a views query, makes you search on the entire record set, which would result in a huge performance issue.

The good question is : why do you need that ? There is 100% another, more accurate solution to your functional problem.

| improve this answer | |
  • I have a leaderboard with many scores on it. I want a "jump to my position" button, so that people who are 10-20 pages in can find themselves quickly. What else could you do for that scenario? – tigertrussell Feb 7 '14 at 0:15
  • This should be a comment, not an answer. – Blazemonger Jul 17 '15 at 16:15
-2

How to get that depends on what you will be using for. If you will be using for calculation under the hud. You could get it using simple php:

$position = $absolute_position % $pager_limit;

$absolute_position is position of node in not paged the whole list if Drupal does not give you that by default at worst case scenario you could run a simple foreatch loop which would check for the node`s title or id to match.

If you will use it for theming Javacript can do the trick. Would personally use use jQuery library to make easier for my self. A simple script could find desired element by it`s tags and then change it accordingly.

| improve this answer | |

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.