Bugzilla – Attachment 115039 Details for
Bug 27353
Return the number of total records
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 27353: Set X-Base-Total-Count header for REST API
Bug-27353-Set-X-Base-Total-Count-header-for-REST-A.patch (text/plain), 5.45 KB, created by
Jonathan Druart
on 2021-01-11 15:50:01 UTC
(
hide
)
Description:
Bug 27353: Set X-Base-Total-Count header for REST API
Filename:
MIME Type:
Creator:
Jonathan Druart
Created:
2021-01-11 15:50:01 UTC
Size:
5.45 KB
patch
obsolete
>From 1591982aca608c16bc2cf140f959799c2b2a4d13 Mon Sep 17 00:00:00 2001 >From: Jonathan Druart <jonathan.druart@bugs.koha-community.org> >Date: Wed, 6 Jan 2021 16:34:23 +0100 >Subject: [PATCH] Bug 27353: Set X-Base-Total-Count header for REST API > >We already set X-Total-Count to the total number of filtered rows, >but we don't have the total number of non-filtered rows. > >Test plan: >This is easy to test on top of bug 27352 or bug 27251, apply them if not >pushed yet. >1. Create 40 items with public notes = "xxx" for biblionumber=4 >then, using Postman (or whatever you prefer): >http://kohadev-intra.mydnsname.org:8081/api/v1/biblios/4/items?_page=1&_per_page=20&q=[{"me.public_notes"%3A{"like"%3A"%25x%25"}}]&_match=contains >Check the headers and confirm you see X-Total-Count=40 and >X-Base-Total-Count=44 > >2. go to /cgi-bin/koha/tools/quotes.pl >You see "Showing 1 to 20 of 28 entries" >Search "he" >Showing 1 to 20 of 22 entries (filtered from 28 total entries) >--- > Koha/REST/Plugin/Objects.pm | 20 ++++++++----------- > Koha/REST/Plugin/Pagination.pm | 4 +++- > koha-tmpl/intranet-tmpl/prog/js/datatables.js | 3 +++ > .../opac-tmpl/bootstrap/js/datatables.js | 3 +++ > 4 files changed, 17 insertions(+), 13 deletions(-) > >diff --git a/Koha/REST/Plugin/Objects.pm b/Koha/REST/Plugin/Objects.pm >index 0f08a2d37a..36f647833a 100644 >--- a/Koha/REST/Plugin/Objects.pm >+++ b/Koha/REST/Plugin/Objects.pm >@@ -124,19 +124,15 @@ sub register { > } > # Perform search > my $objects = $result_set->search( $filtered_params, $attributes ); >+ my $total = $result_set->search->count; > >- if ($objects->is_paged) { >- $c->add_pagination_headers({ >- total => $objects->pager->total_entries, >- params => $args, >- }); >- } >- else { >- $c->add_pagination_headers({ >- total => $objects->count, >- params => $args, >- }); >- } >+ $c->add_pagination_headers( >+ { >+ total => ($objects->is_paged ? $objects->pager->total_entries : $objects->count), >+ base_total => $total, >+ params => $args, >+ } >+ ); > > return $objects->to_api({ embed => $embed }); > } >diff --git a/Koha/REST/Plugin/Pagination.pm b/Koha/REST/Plugin/Pagination.pm >index 911aa43aaf..90d220c1cf 100644 >--- a/Koha/REST/Plugin/Pagination.pm >+++ b/Koha/REST/Plugin/Pagination.pm >@@ -50,7 +50,7 @@ sub register { > > Adds a Link header to the response message $c carries, following RFC5988, including > the following relation types: 'prev', 'next', 'first' and 'last'. >-It also adds X-Total-Count, containing the total results count. >+It also adds X-Total-Count containing the total results count, and X-Base-Total-Count containing the total of the non-filtered results count. > > If page size is omitted, it defaults to the value of the RESTdefaultPageSize syspref. > >@@ -61,6 +61,7 @@ If page size is omitted, it defaults to the value of the RESTdefaultPageSize sys > my ( $c, $args ) = @_; > > my $total = $args->{total}; >+ my $base_total = $args->{base_total}; > my $req_page = $args->{params}->{_page} // 1; > my $per_page = $args->{params}->{_per_page} // > C4::Context->preference('RESTdefaultPageSize') // 20; >@@ -114,6 +115,7 @@ If page size is omitted, it defaults to the value of the RESTdefaultPageSize sys > > # Add X-Total-Count header > $c->res->headers->add( 'X-Total-Count' => $total ); >+ $c->res->headers->add( 'X-Base-Total-Count' => $base_total ); > return $c; > } > ); >diff --git a/koha-tmpl/intranet-tmpl/prog/js/datatables.js b/koha-tmpl/intranet-tmpl/prog/js/datatables.js >index 13db0e495c..69fc5a998f 100644 >--- a/koha-tmpl/intranet-tmpl/prog/js/datatables.js >+++ b/koha-tmpl/intranet-tmpl/prog/js/datatables.js >@@ -544,6 +544,9 @@ jQuery.fn.dataTable.ext.errMode = function(settings, note, message) { > json.recordsTotal = total; > json.recordsFiltered = total; > } >+ if(total = this._xhr.getResponseHeader('x-base-total-count')) { >+ json.recordsTotal = total; >+ } > return JSON.stringify(json); > }, > 'data': function( data, settings ) { >diff --git a/koha-tmpl/opac-tmpl/bootstrap/js/datatables.js b/koha-tmpl/opac-tmpl/bootstrap/js/datatables.js >index 231ef74219..8a38fa11e8 100644 >--- a/koha-tmpl/opac-tmpl/bootstrap/js/datatables.js >+++ b/koha-tmpl/opac-tmpl/bootstrap/js/datatables.js >@@ -544,6 +544,9 @@ jQuery.fn.dataTable.ext.errMode = function(settings, note, message) { > json.recordsTotal = total; > json.recordsFiltered = total; > } >+ if(total = this._xhr.getResponseHeader('x-base-total-count')) { >+ json.recordsTotal = total; >+ } > return JSON.stringify(json); > }, > 'data': function( data, settings ) { >-- >2.20.1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 27353
:
114893
|
114896
|
114897
|
115039
|
115058
|
115059
|
115769
|
115771
|
115784
|
115785
|
115786
|
115807
|
115808
|
115809