Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hi
Adding the following to template_preprocess_views_views_json_style_simple will give an additional root object 'pager' with keys total and current for the total number of pages and current page respectively if the view has a pager enabled. You can use this to setup your own pagers with ajax.
global $pager_total, $pager_page_array;
$element = $vars['view']->pager['element'];
$vars['rows']['pager'] = array(
'total' => $pager_total[$element],
'current' => $pager_page_array[$element]
);
For those who need this but don't want to hack the module, implement it as hook_preprocess_views_views_json_style_simple (&$vars) in your own module.
Comment | File | Size | Author |
---|---|---|---|
#19 | 905440-19_pager_support.patch | 964 bytes | ZeiP |
#16 | shot_20170303-232821.png | 19.34 KB | msti |
#14 | views_datasource-add_pager_support-D7-905440-14.patch | 1.51 KB | othermachines |
Comments
Comment #1
mcantelon CreditAttribution: mcantelon commentedlarowlan's hack works great. Please add this to this super useful module!
Comment #2
zeemp CreditAttribution: zeemp commentedAny clue on how to setup your own pagers with ajax? How can I print it inside the json view file?
Comment #3
larowlanFor those unsure what implementing the hook in your own module means you can do this in your theme:
From memory, that's all you should need
Comment #4
zeemp CreditAttribution: zeemp commentedThis is what happened in the json file when I paste the code into the template.php
The pager appeared below nodes, is this all the info I need to build the ul class="pager", with the li's inside it? some js code is enough?
Thanks so much!
Comment #5
zeemp CreditAttribution: zeemp commentedFor those who want to know the javascript required to build the pager. I've acomplished it with the code below. Don't forget #3 before pasting the code into you javascript file.
Any improvements are welcome.
Comment #6
larowlanHere's how I did it:
Comment #7
rayvan CreditAttribution: rayvan commentedThere was a syntax error in zeemp's callView function code above (missing closing parenthesis near .each), here is the correct version:
I haven't gone through implementing the rest of the code yet but makes sense from a high level.
Thank you. And yes, this should be added to the module's default data presentation of json.
Comment #8
bodrin CreditAttribution: bodrin commentedHi, I have added the following to my theme (danland) template.php in a drupal 7.17 installation:
I'm getting this output:
{"nodes":[{"node":{"title":"news2","field_news_image":"http://example.com/vakt/sites/v-akt/files/logo.png"}}],"pager":{"total":null,"current":null}}
The reason is that $element = $vars['view']->pager['element']; is null. Any idea about why?
Note that If I use the arrays directly:
I get
{"nodes":[{"node":{"title":"news2","field_news_image":"http://example.com/vakt/sites/v-akt/files/logo.png"}}],"pager":{"total":[2],"current":[0]}}
Which is ok.
I know that I have only a single pager in this page and I can hardcode it probably but ..is there a better way to solve this?
Comment #9
yannickooHere is no patch, so I set it back to "active".
Comment #10
tyler.frankenstein CreditAttribution: tyler.frankenstein commentedFor D7, Views 3.x, add this to your module to get the total number of pages, the current page, the total result count, and the limit per page:
We can assume index 0 on each of the pager arrays since there will only be one pager whenever we are fetching JSON from a View.
The result JSON will now look something like this:
The above result says we have a total of 3 pages, we're currently on the first page (zero), there are a total of 3 results, and 1 result per page.
Thanks to @larowlan and @bodrin for their code above.
Comment #11
rjdjohnston CreditAttribution: rjdjohnston commentedThanks @tyler.frankenstein it works beautifully!
Comment #12
dark_kz CreditAttribution: dark_kz commentedNot worked for me :(
If I add ?page=1 to url it shows next nodes, but #10 code only show 0 and 10 (per page node count).
I have a normal view with output and pager, and it works ok. Query string are the same. Where should I search error?
Comment #13
hubobbb CreditAttribution: hubobbb as a volunteer commentednice !
edit in file: views_views_json_style.theme.inc
...
}
$objects[] = $object;
}
// check if user wants nested arrays
$vars["rows"] = strlen($root_object) != 0 ? array($root_object => $objects) : $objects;
// ADD CODE BEGIN
global $pager_total, $pager_page_array, $pager_total_items, $pager_limits;
$vars['rows']['pager'] = array(
'pages' => $pager_total[0],
'page' => $pager_page_array[0],
'count' => intval($pager_total_items[0]),
'limit' => intval($pager_limits[0])
);
$vars["rows"]["pager"]=$vars["rows"]["pager"];
//print_r($vars['rows']['pager']);
//ADD CODE END
}
function template_preprocess_views_views_json_style_exhibit(&$vars) {
$view = $vars["view"];
$rows = $vars["rows"];......
=======================================
here is result of new json code case:
....,"pager":{"pages":9,"page":2,"count":101,"limit":12}}
Comment #14
othermachines CreditAttribution: othermachines commentedI love me a nice patch. ;)
Comment #15
bburgIf this were used on the views_xml module, would it generate invalid xml? Since the root element is the "nodes" array.
Comment #16
mstiThe patch at #14 works for me with views_json. check screenshot.
Comment #17
Chris CharltonHow many more validations do we need for RTBC?
Comment #18
mstilet's just add the reviewed and tested status and the maintainer will tell us if she/he is alright
Comment #19
ZeiP CreditAttribution: ZeiP as a volunteer commentedThanks for the patch! It seems quite reasonable; however, I think it's best if the pagination element is only shown when pagination is enabled in the views. I also moved all the code to one place. Attached is a new patch.
Comment #21
ZeiP CreditAttribution: ZeiP as a volunteer commentedCommitted to 7.x-1.x. Thanks everyone!