From 3a235908f102e03a1c79a7911770897de054b7f7 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 24 Oct 2023 11:12:56 +0200 Subject: [PATCH] Bug 34913: WIP - item search This is not working, the columnfilter plugin seems to break the sorting. If you sort twice on a colum, the request will be GET (instead of POSTed, which is the historical behaviour) and columns will contain [object Object] There are more to do here, but this problem blocks the other steps. --- catalogue/itemsearch.pl | 11 ++++---- .../prog/en/modules/catalogue/itemsearch.tt | 27 +++++++++---------- .../en/modules/catalogue/itemsearch_json.tt | 6 ++--- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/catalogue/itemsearch.pl b/catalogue/itemsearch.pl index fad7d1a8aee..adba83d91dc 100755 --- a/catalogue/itemsearch.pl +++ b/catalogue/itemsearch.pl @@ -19,7 +19,7 @@ use Modern::Perl; use CGI; -use JSON qw( to_json ); +use JSON qw( to_json from_json ); use C4::Auth qw( get_template_and_user ); use C4::Circulation qw( barcodedecode ); @@ -34,7 +34,6 @@ use Koha::ItemTypes; use Koha::Libraries; my $cgi = CGI->new; -my %params = $cgi->Vars; my $format = $cgi->param('format'); my $template_name = 'catalogue/itemsearch.tt'; @@ -43,9 +42,9 @@ if (defined $format and $format eq 'json') { $template_name = 'catalogue/itemsearch_json.tt'; # Map DataTables parameters with 'regular' parameters - $cgi->param('rows', scalar $cgi->param('iDisplayLength')); - $cgi->param('page', (scalar $cgi->param('iDisplayStart') / scalar $cgi->param('iDisplayLength')) + 1); - my @columns = split /,/, scalar $cgi->param('sColumns'); + $cgi->param('rows', scalar $cgi->param('length')); + $cgi->param('page', (scalar $cgi->param('start') / scalar $cgi->param('length')) + 1); + my @columns = split /,/, from_json scalar $cgi->param('columns'); $cgi->param('sortby', $columns[ scalar $cgi->param('iSortCol_0') ]); $cgi->param('sortorder', scalar $cgi->param('sSortDir_0')); @@ -275,7 +274,7 @@ if ( defined $format ) { print "$line\n" unless $line =~ m|^\s*$|; } } elsif ($format eq 'json') { - $template->param(sEcho => scalar $cgi->param('sEcho')); + $template->param(draw => scalar $cgi->param('draw')); output_with_http_headers $cgi, $cookie, $template->output, 'json'; } diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/itemsearch.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/itemsearch.tt index 0680a231dcf..4d9347f7d29 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/itemsearch.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/itemsearch.tt @@ -469,23 +469,20 @@ $('#results').dataTable($.extend(true, {}, dataTablesDefaults, { "destroy": true, - "serverSide": true, + "serverSide": true, "processing": true, - "ajax": '/cgi-bin/koha/catalogue/itemsearch.pl', - 'fnServerData': function(sSource, aoData, fnCallback) { - aoData.push( { 'name': 'format', 'value': 'json' } ); - for (i in params) { - aoData.push(params[i]); - } - $.ajax({ - 'dataType': 'json', - 'type': 'POST', - 'url': sSource, - 'data': aoData, - 'success': function(json){ - fnCallback(json); + "ajax": { + url: '/cgi-bin/koha/catalogue/itemsearch.pl', + type: 'POST', + data: function ( d ) { + for (i in params) { + d[params[i].name] = params[i].value; } - }); + d.format = 'json'; + d.columns = JSON.stringify( d.columns ); + console.log(d.draw); + return d; + } }, "dom": '<"top pager"ilp>t<"bottom pager"ip>r', "order": [[1, 'asc']], diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/itemsearch_json.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/itemsearch_json.tt index fdf7ad63220..f0df4fe4584 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/itemsearch_json.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/itemsearch_json.tt @@ -1,7 +1,7 @@ { - "sEcho": [% sEcho | html %], - "iTotalRecords": [% total_rows | html %], - "iTotalDisplayRecords": [% total_rows | html %], + "draw": [% draw | html %], + "recordsTotal": [% total_rows | html %], + "recordsFiltered": [% total_rows | html %], "data": [ [%- FOREACH item IN results -%] [%- INCLUDE 'catalogue/itemsearch_item.json.inc' item = item, user = user -%] -- 2.34.1