From 4e5e04a20425e8051ea421cd14ce331891ace968 Mon Sep 17 00:00:00 2001 From: Agustin Moyano Date: Wed, 19 Feb 2020 11:33:14 -0300 Subject: [PATCH] Bug 24561: Datatables api wrapper also filter and order embedded columns This patch adds the ability to filter and order by embedded columns. To use it you must in JS: $('datatable_selector').api({datatables_options}) where datatables_options are all datatables options plus: 1. embed: [list of embeddable tables] This option adds x-koha-embed header to request. 2. header_filter: true|false This option if true sets x-koha-query header with stringyfied json of filters Oderable and searchable columns must define data option as string, otherwise filter and order won't be possible. If you must custom the output, use the render function. For example: * Don't > $('.table_selector').api({ > columns: [ > { > data: function(row, type, val, meta) { > return ''+row.holds.patron.firstname+''; > }, > orderable: true, > searchable: true > } > ] > }); * Do > $('.table_selector').api({ > columns: [ > { > data: 'holds.patron.firstname', > render: function(row, type, val, meta) { > return ''+row.holds.patron.firstname+''; > }, > orderable: true, > searchable: true > } > ] > }); To test you must implement and test bug 20936, where it will be used. --- koha-tmpl/intranet-tmpl/prog/js/datatables.js | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/js/datatables.js b/koha-tmpl/intranet-tmpl/prog/js/datatables.js index c95f78cbe7..77804eca3b 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/datatables.js +++ b/koha-tmpl/intranet-tmpl/prog/js/datatables.js @@ -527,6 +527,10 @@ jQuery.fn.dataTable.ext.errMode = function(settings, note, message) { if(options.embed) { xhr.setRequestHeader('x-koha-embed', Array.isArray(options.embed)?options.embed.join(','):options.embed); } + if(options.header_filter && options.query_parameters) { + xhr.setRequestHeader('x-koha-query', options.query_parameters); + delete options.query_parameters; + } }, 'dataFilter': function(data, type) { var json = {data: JSON.parse(data)}; @@ -547,8 +551,28 @@ jQuery.fn.dataTable.ext.errMode = function(settings, note, message) { var filter = data.search.value; if (filter != '') { - dataSet.name = filter; + var query_parameters = settings.aoColumns + .filter(function(col) { + return col.bSearchable && typeof col.data == 'string' + }) + .map(function(col) { + var part = {}; + part[!col.data.includes('.')?'me.'+col.data:col.data] = {like: filter+'%'}; + return part; + }); + + if(query_parameters.length) { + query_parameters = JSON.stringify(query_parameters.length === 1?query_parameters[0]:query_parameters); + if(options.header_filter) { + options.query_parameters = query_parameters; + } else { + dataSet.q = query_parameters; + } + } + dataSet._match = 'starts_with'; + } else { + delete options.query_parameters; } if(options.columns) { @@ -557,7 +581,7 @@ jQuery.fn.dataTable.ext.errMode = function(settings, note, message) { var order_col = e.column; var order_by = options.columns[order_col].data; var order_dir = e.dir == 'asc' ? '+' : '-'; - dataSet._order_by = order_dir + order_by; + dataSet._order_by = order_dir + (!order_by.includes('.')?'me.'+order_by:order_by); }); } -- 2.17.1