mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-09-11 15:45:34 +08:00
fixes bug with samples sorting [fixes SCI-812]
This commit is contained in:
parent
c8631077df
commit
e2723a288c
5 changed files with 73 additions and 30 deletions
|
@ -220,6 +220,13 @@ table = dataTableInit();
|
||||||
// Enables noSearchHidden plugin
|
// Enables noSearchHidden plugin
|
||||||
$.fn.dataTable.defaults.noSearchHidden = true;
|
$.fn.dataTable.defaults.noSearchHidden = true;
|
||||||
|
|
||||||
|
// update datatable step on colReorder
|
||||||
|
(function() {
|
||||||
|
table.on('column-reorder', function() {
|
||||||
|
table.state.save();
|
||||||
|
});
|
||||||
|
})();
|
||||||
|
|
||||||
// Updates "Select all" control in a data table
|
// Updates "Select all" control in a data table
|
||||||
function updateDataTableSelectAllCtrl(table) {
|
function updateDataTableSelectAllCtrl(table) {
|
||||||
var $table = table.table().node();
|
var $table = table.table().node();
|
||||||
|
|
|
@ -275,7 +275,8 @@ class ExperimentsController < ApplicationController
|
||||||
@organization,
|
@organization,
|
||||||
nil,
|
nil,
|
||||||
nil,
|
nil,
|
||||||
@experiment)
|
@experiment,
|
||||||
|
current_user)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -325,9 +325,14 @@ class MyModulesController < ApplicationController
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html
|
format.html
|
||||||
format.json {
|
format.json do
|
||||||
render json: ::SampleDatatable.new(view_context, @organization, nil, @my_module)
|
render json: ::SampleDatatable.new(view_context,
|
||||||
}
|
@organization,
|
||||||
|
nil,
|
||||||
|
@my_module,
|
||||||
|
nil,
|
||||||
|
current_user)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -280,12 +280,17 @@ class ProjectsController < ApplicationController
|
||||||
|
|
||||||
def samples_index
|
def samples_index
|
||||||
@organization = @project.organization
|
@organization = @project.organization
|
||||||
|
@user = current_user
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html
|
format.html
|
||||||
format.json {
|
format.json do
|
||||||
render json: ::SampleDatatable.new(view_context, @organization, @project, nil)
|
render json: ::SampleDatatable.new(view_context,
|
||||||
}
|
@organization,
|
||||||
|
@project,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
@user)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -33,38 +33,42 @@ class SampleDatatable < AjaxDatatablesRails::Base
|
||||||
organization,
|
organization,
|
||||||
project = nil,
|
project = nil,
|
||||||
my_module = nil,
|
my_module = nil,
|
||||||
experiment = nil)
|
experiment = nil,
|
||||||
|
user = nil)
|
||||||
super(view)
|
super(view)
|
||||||
@organization = organization
|
@organization = organization
|
||||||
@project = project
|
@project = project
|
||||||
@my_module = my_module
|
@my_module = my_module
|
||||||
@experiment = experiment
|
@experiment = experiment
|
||||||
|
@user = user
|
||||||
end
|
end
|
||||||
|
|
||||||
# Define sortable columns, so 1st column will be sorted by attribute in sortable_columns[0]
|
# Define sortable columns, so 1st column will be sorted by attribute in sortable_columns[0]
|
||||||
def sortable_columns
|
def sortable_columns
|
||||||
sort_array = [
|
sort_array = [
|
||||||
ASSIGNED_SORT_COL,
|
ASSIGNED_SORT_COL,
|
||||||
"Sample.name",
|
'Sample.name',
|
||||||
"SampleType.name",
|
'SampleType.name',
|
||||||
"SampleGroup.name",
|
'SampleGroup.name',
|
||||||
"Sample.created_at",
|
'Sample.created_at',
|
||||||
"User.full_name",
|
'User.full_name'
|
||||||
]
|
]
|
||||||
|
|
||||||
sort_array.push(*custom_fields_sort_by)
|
sort_array.push(*custom_fields_sort_by)
|
||||||
|
|
||||||
@sortable_columns ||= sort_array
|
@sortable_columns = sort_array
|
||||||
end
|
end
|
||||||
|
|
||||||
# Define attributes on which we perform search
|
# Define attributes on which we perform search
|
||||||
def searchable_columns
|
def searchable_columns
|
||||||
search_array = [
|
search_array = [
|
||||||
"Sample.name",
|
'Sample.name',
|
||||||
"SampleType.name",
|
'SampleType.name',
|
||||||
"SampleGroup.name",
|
'SampleGroup.name',
|
||||||
"Sample.created_at",
|
'Sample.created_at',
|
||||||
"User.full_name"
|
'User.full_name'
|
||||||
]
|
]
|
||||||
|
|
||||||
search_array.push(*custom_fields_sort_by)
|
search_array.push(*custom_fields_sort_by)
|
||||||
@searchable_columns ||= filter_search_array search_array
|
@searchable_columns ||= filter_search_array search_array
|
||||||
end
|
end
|
||||||
|
@ -72,7 +76,7 @@ class SampleDatatable < AjaxDatatablesRails::Base
|
||||||
private
|
private
|
||||||
|
|
||||||
# filters the search array by checking if the the column is visible
|
# 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
|
param_index = 2
|
||||||
filtered_array =[]
|
filtered_array =[]
|
||||||
input_array.each do |col|
|
input_array.each do |col|
|
||||||
|
@ -90,7 +94,7 @@ class SampleDatatable < AjaxDatatablesRails::Base
|
||||||
array = []
|
array = []
|
||||||
|
|
||||||
for _ in 0..num_cf
|
for _ in 0..num_cf
|
||||||
array << "SampleCustomField.value"
|
array << 'SampleCustomField.value'
|
||||||
end
|
end
|
||||||
array
|
array
|
||||||
end
|
end
|
||||||
|
@ -99,11 +103,11 @@ class SampleDatatable < AjaxDatatablesRails::Base
|
||||||
def data
|
def data
|
||||||
records.map do |record|
|
records.map do |record|
|
||||||
sample = {
|
sample = {
|
||||||
"DT_RowId": record.id,
|
'DT_RowId': record.id,
|
||||||
"1": assigned_cell(record),
|
'1': assigned_cell(record),
|
||||||
"2": record.name,
|
'2': record.name,
|
||||||
"3": record.sample_type.nil? ? I18n.t("samples.table.no_type") : record.sample_type.name,
|
'3': record.sample_type.nil? ? I18n.t('samples.table.no_type') : record.sample_type.name,
|
||||||
"4": record.sample_group.nil? ?
|
'4': record.sample_group.nil? ?
|
||||||
"<span class='glyphicon glyphicon-asterisk'></span> " + I18n.t("samples.table.no_group") :
|
"<span class='glyphicon glyphicon-asterisk'></span> " + I18n.t("samples.table.no_group") :
|
||||||
"<span class='glyphicon glyphicon-asterisk' style='color: #{record.sample_group.color}'></span> " + record.sample_group.name,
|
"<span class='glyphicon glyphicon-asterisk' style='color: #{record.sample_group.color}'></span> " + record.sample_group.name,
|
||||||
"5": I18n.l(record.created_at, format: :full),
|
"5": I18n.l(record.created_at, format: :full),
|
||||||
|
@ -211,12 +215,13 @@ class SampleDatatable < AjaxDatatablesRails::Base
|
||||||
|
|
||||||
# Override default sort method if needed
|
# Override default sort method if needed
|
||||||
def sort_records(records)
|
def sort_records(records)
|
||||||
if params[:order].present? and params[:order].length == 1
|
if params[:order].present? && params[:order].length == 1
|
||||||
if sort_column(params[:order].values[0]) == ASSIGNED_SORT_COL
|
if sort_column(params[:order].values[0]) == ASSIGNED_SORT_COL
|
||||||
# If "assigned" column is sorted
|
# If "assigned" column is sorted
|
||||||
if @my_module then
|
if @my_module then
|
||||||
# Depending on the sort, order nulls first or
|
# Depending on the sort, order nulls first or
|
||||||
# nulls last on sample_my_modules association
|
# nulls last on sample_my_modules association
|
||||||
|
|
||||||
records.order("sample_my_modules.id NULLS #{sort_null_direction(params[:order].values[0])}")
|
records.order("sample_my_modules.id NULLS #{sort_null_direction(params[:order].values[0])}")
|
||||||
elsif @experiment
|
elsif @experiment
|
||||||
# A very elegant solution to sort assigned samples at a experiment level
|
# A very elegant solution to sort assigned samples at a experiment level
|
||||||
|
@ -250,6 +255,7 @@ class SampleDatatable < AjaxDatatablesRails::Base
|
||||||
:sanitize_sql_array,
|
:sanitize_sql_array,
|
||||||
["position((',' || samples.id || ',') in ?)",
|
["position((',' || samples.id || ',') in ?)",
|
||||||
ids.join(',') + ','] )
|
ids.join(',') + ','] )
|
||||||
|
|
||||||
records.where(id: ids).order(order_by_index)
|
records.where(id: ids).order(order_by_index)
|
||||||
elsif @project
|
elsif @project
|
||||||
# A very elegant solution to sort assigned samples at a project level
|
# A very elegant solution to sort assigned samples at a project level
|
||||||
|
@ -331,7 +337,7 @@ class SampleDatatable < AjaxDatatablesRails::Base
|
||||||
super(records)
|
super(records)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
#(I18n.t('time.formats.datatables_date')).gsub("\"", '\'')
|
|
||||||
|
|
||||||
# A hack that overrides the new_search_contition method default behavior of the ajax-datatables-rails gem
|
# A hack that overrides the new_search_contition method default behavior of the ajax-datatables-rails gem
|
||||||
# now the method checks if the column is the created_at and generate a custom SQL to parse
|
# now the method checks if the column is the created_at and generate a custom SQL to parse
|
||||||
|
@ -361,7 +367,7 @@ class SampleDatatable < AjaxDatatablesRails::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def sorting_by_custom_column
|
def sorting_by_custom_column
|
||||||
params[:order].values[0]["column"].to_i > 6
|
sort_column(params[:order].values[0]) == 'SampleCustomField.value'
|
||||||
end
|
end
|
||||||
|
|
||||||
# Escapes special characters in search query
|
# Escapes special characters in search query
|
||||||
|
@ -371,4 +377,23 @@ class SampleDatatable < AjaxDatatablesRails::Base
|
||||||
params[:search][:value]) if params[:search]
|
params[:search][:value]) if params[:search]
|
||||||
.present?
|
.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def new_sort_column(item)
|
||||||
|
coli = item[:column].to_i - 1
|
||||||
|
model, column = sortable_columns[sortable_displayed_columns[coli].to_i]
|
||||||
|
.split('.')
|
||||||
|
col = [model.constantize.table_name, column].join('.')
|
||||||
|
end
|
||||||
|
|
||||||
|
def generate_sortable_displayed_columns
|
||||||
|
sort_order = SamplesTable.where(user: @user,
|
||||||
|
organization: @organization)
|
||||||
|
.pluck(:status)
|
||||||
|
.first['ColReorder']
|
||||||
|
|
||||||
|
sort_order.shift
|
||||||
|
sort_order.map! { |i| (i.to_i - 1).to_s }
|
||||||
|
|
||||||
|
@sortable_displayed_columns = sort_order
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue