From fbc2be890c66c6bbb4c369b81ff3669b738a0602 Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Fri, 23 Dec 2016 19:56:54 +0100 Subject: [PATCH] Fix search on hidden columns in samples table Closes SCI-813. --- .../samples/sample_datatable.js.erb | 23 ++++++++++++++++++- app/datatables/sample_datatable.rb | 13 ++++++----- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/samples/sample_datatable.js.erb b/app/assets/javascripts/samples/sample_datatable.js.erb index 05063e1fd..f927323f9 100644 --- a/app/assets/javascripts/samples/sample_datatable.js.erb +++ b/app/assets/javascripts/samples/sample_datatable.js.erb @@ -1,5 +1,22 @@ //= require jquery-ui +// Extend datatables API with searchable options +// (http://stackoverflow.com/questions/39912395/datatables-dynamically-set-columns-searchable) +$.fn.dataTable.Api.register('isColumnSearchable()', function(colSelector) { + var idx = this.column(colSelector).index(); + return this.settings()[0].aoColumns[idx].bSearchable; +}); +$.fn.dataTable.Api.register('setColumnSearchable()', function(colSelector, value) { + if (value !== this.isColumnSearchable(colSelector)) { + var idx = this.column(colSelector).index(); + this.settings()[0].aoColumns[idx].bSearchable = value; + if (value === true) { + this.rows().invalidate(); + } + } + return value; +}); + var rowsSelected = []; // Tells whether we're currently viewing or editing table @@ -81,7 +98,8 @@ function dataTableInit() { columns.push({ data: String(i), defaultContent: '', - visible: visible + visible: visible, + searchable: visible }); } return columns; @@ -140,6 +158,7 @@ function dataTableInit() { visibility = (visibility === 'true'); } table.column(i).visible(visibility); + table.setColumnSearchable(i, visibility); } oSettings._colReorder.fnOrder(myData.ColReorder); table.on('mousedown', function() { @@ -1017,11 +1036,13 @@ function changeToEditMode() { self.removeClass('glyphicon-eye-open'); li.addClass('col-invisible'); column.visible(false); + table.setColumnSearchable(column.index(), false); } else { self.addClass('glyphicon-eye-open'); self.removeClass('glyphicon-eye-close'); li.removeClass('col-invisible'); column.visible(true); + table.setColumnSearchable(column.index(), true); } }); } diff --git a/app/datatables/sample_datatable.rb b/app/datatables/sample_datatable.rb index 6984f9aac..c80c4bd97 100644 --- a/app/datatables/sample_datatable.rb +++ b/app/datatables/sample_datatable.rb @@ -72,14 +72,15 @@ class SampleDatatable < AjaxDatatablesRails::Base private # filters the search array by checking if the the column is visible - def filter_search_array input_array + def filter_search_array(input_array) param_index = 2 - filtered_array =[] + filtered_array = [] input_array.each do |col| - unless params[:columns].to_a[param_index] == nil - filtered_array.push(col) unless params[:columns].to_a[param_index][1]["searchable"] == "false" - param_index += 1 - end + next if params[:columns].to_a[param_index].nil? + params_col = + params[:columns].to_a.find { |v| v[1]['data'] == param_index.to_s } + filtered_array.push(col) unless params_col[1]['searchable'] == 'false' + param_index += 1 end filtered_array end