From e0ab580538dc8cad53597d56cc873b1af0470603 Mon Sep 17 00:00:00 2001 From: Agustin Moyano Date: Thu, 30 Apr 2020 11:20:05 -0300 Subject: [PATCH] Bug 24561: (follow-up) Set default matching criteria to 'contains' and add configuration option This patch adds option.criteria as an option. Possible values are 'contains', 'starts_with', 'ends_with' and 'exact'. 'contains' is the default value. Signed-off-by: Jonathan Druart Signed-off-by: Martin Renvoize --- koha-tmpl/intranet-tmpl/prog/js/datatables.js | 7 +++++-- koha-tmpl/opac-tmpl/bootstrap/js/datatables.js | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/js/datatables.js b/koha-tmpl/intranet-tmpl/prog/js/datatables.js index a48112eda5..055bb74b56 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/datatables.js +++ b/koha-tmpl/intranet-tmpl/prog/js/datatables.js @@ -512,6 +512,8 @@ jQuery.fn.dataTable.ext.errMode = function(settings, note, message) { $.fn.api = function(options) { var settings = null; if(options) { + if(!options.criteria || ['contains', 'starts_with', 'ends_with', 'exact'].indexOf(options.criteria.toLowerCase()) === -1) options.criteria = 'contains'; + options.criteria = options.criteria.toLowerCase(); settings = $.extend(true, {}, dataTablesDefaults, { 'deferRender': true, "paging": true, @@ -556,7 +558,8 @@ jQuery.fn.dataTable.ext.errMode = function(settings, note, message) { }) .map(function(col) { var part = {}; - part[!col.data.includes('.')?'me.'+col.data:col.data] = {like: (data.columns[col.idx].search.value != '' ? data.columns[col.idx].search.value : filter)+'%'}; + var value = data.columns[col.idx].search.value != '' ? data.columns[col.idx].search.value : filter; + part[!col.data.includes('.')?'me.'+col.data:col.data] = options.criteria === 'exact'?value:{like: (['contains', 'ends_with'].indexOf(options.criteria) !== -1?'%':'')+value+(['contains', 'starts_with'].indexOf(options.criteria) !== -1?'%':'')}; return part; }); @@ -572,7 +575,7 @@ jQuery.fn.dataTable.ext.errMode = function(settings, note, message) { delete options.query_parameters; } - dataSet._match = 'starts_with'; + dataSet._match = options.criteria; if(options.columns) { var order = data.order; diff --git a/koha-tmpl/opac-tmpl/bootstrap/js/datatables.js b/koha-tmpl/opac-tmpl/bootstrap/js/datatables.js index 10dac7cd00..047fa4c438 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/js/datatables.js +++ b/koha-tmpl/opac-tmpl/bootstrap/js/datatables.js @@ -133,6 +133,8 @@ jQuery.fn.dataTable.ext.errMode = function(settings, note, message) { $.fn.api = function(options) { var settings = null; if(options) { + if(!options.criteria || ['contains', 'starts_with', 'ends_with', 'exact'].indexOf(options.criteria.toLowerCase()) === -1) options.criteria = 'contains'; + options.criteria = options.criteria.toLowerCase(); settings = $.extend(true, {}, dataTablesDefaults, { 'deferRender': true, "paging": true, @@ -177,7 +179,8 @@ jQuery.fn.dataTable.ext.errMode = function(settings, note, message) { }) .map(function(col) { var part = {}; - part[!col.data.includes('.')?'me.'+col.data:col.data] = {like: (data.columns[col.idx].search.value != '' ? data.columns[col.idx].search.value : filter)+'%'}; + var value = data.columns[col.idx].search.value != '' ? data.columns[col.idx].search.value : filter; + part[!col.data.includes('.')?'me.'+col.data:col.data] = options.criteria === 'exact'?value:{like: (['contains', 'ends_with'].indexOf(options.criteria) !== -1?'%':'')+value+(['contains', 'starts_with'].indexOf(options.criteria) !== -1?'%':'')}; return part; }); @@ -193,7 +196,7 @@ jQuery.fn.dataTable.ext.errMode = function(settings, note, message) { delete options.query_parameters; } - dataSet._match = 'starts_with'; + dataSet._match = options.criteria; if(options.columns) { var order = data.order; -- 2.39.5