From a4ff347d6e24521ad66d80df435ac3e7f4006186 Mon Sep 17 00:00:00 2001 From: ajugo Date: Tue, 21 Jun 2022 10:49:03 +0200 Subject: [PATCH] Fix datatable sort in task assigned repository to task and on first repository load [SCI-6904] (#4164) * Enable repository sorting on assigned repository [SCI-6904] * Fix order at repository table initialization [SCI-6904] --- .../javascripts/my_modules/repositories.js | 7 ++-- app/services/repository_datatable_service.rb | 32 ++++++++++++++++--- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/my_modules/repositories.js b/app/assets/javascripts/my_modules/repositories.js index afad98a4d..e6b480ffb 100644 --- a/app/assets/javascripts/my_modules/repositories.js +++ b/app/assets/javascripts/my_modules/repositories.js @@ -222,8 +222,7 @@ var MyModuleRepositories = (function() { url: $(tableContainer).data('source'), contentType: 'application/json', data: function(d) { - d.order[0].column = tableContainer.data('name-column-id'); - d.assigned = 'assigned'; + d.assigned = 'assigned_simple'; d.view_mode = true; d.simple_view = true; return JSON.stringify(d); @@ -333,6 +332,10 @@ var MyModuleRepositories = (function() { json.state.columns[7].visible = false; } if (json.state.search) delete json.state.search; + if ($(tableContainer).data('stockConsumptionColumn')) { + json.state.columns.push({}); + json.state.ColReorder.push(json.state.ColReorder.length); + } callback(json.state); }); }, diff --git a/app/services/repository_datatable_service.rb b/app/services/repository_datatable_service.rb index 610c351a7..5d9a53d56 100644 --- a/app/services/repository_datatable_service.rb +++ b/app/services/repository_datatable_service.rb @@ -7,6 +7,7 @@ module RepositoryFilters end class RepositoryDatatableService + include MyModulesHelper attr_reader :repository_rows, :all_count, :mappings PREDEFINED_COLUMNS = %w(row_id row_name added_on added_by archived_on archived_by assigned).freeze @@ -16,6 +17,7 @@ class RepositoryDatatableService @user = user @my_module = my_module @params = params + @assigned_view = @params[:assigned].in?(%w(assigned assigned_simple)) @sortable_columns = build_sortable_columns create_columns_mappings @repository_rows = process_query @@ -43,7 +45,7 @@ class RepositoryDatatableService # Adding assigned counters if @my_module - if @params[:assigned] == 'assigned' + if @assigned_view repository_rows = repository_rows.joins(:my_module_repository_rows) .where(my_module_repository_rows: { my_module_id: @my_module }) if @repository.has_stock_management? @@ -80,7 +82,7 @@ class RepositoryDatatableService end @all_count = - if @my_module && @params[:assigned] == 'assigned' + if @my_module && @assigned_view repository_rows.joins(:my_module_repository_rows) .where(my_module_repository_rows: { my_module_id: @my_module }) .count @@ -409,9 +411,24 @@ class RepositoryDatatableService @repository.repository_columns.count.times do array << 'repository_cell.value' end + + array << 'consumed_stock' if @repository.has_stock_management? && @assigned_view array end + def map_simple_view_column(index) + return assigned_repository_simple_view_name_column_id(@repository) unless @repository.has_stock_management? + + case index + when 0 + assigned_repository_simple_view_name_column_id(@repository) + when 1 + @mappings[@repository.repository_stock_column.id] + when 2 + @sortable_columns.length + end + end + def sort_rows(column_obj, records) dir = %w(DESC ASC).find do |direction| direction == column_obj[:dir].upcase @@ -420,11 +437,16 @@ class RepositoryDatatableService column_index = column_obj[:column] service = RepositoryTableStateService.new(@user, @repository) col_order = service.load_state.state['ColReorder'] - column_id = col_order[column_index].to_i + column_id = case @params[:assigned] + when 'assigned_simple' + map_simple_view_column(column_index).to_i + else + @params[:draw].to_i == 1 ? column_index.to_i : col_order[column_index].to_i + end case @sortable_columns[column_id - 1] when 'assigned' - return records if @my_module && @params[:assigned] == 'assigned' + return records if @my_module && @assigned_view records.order("assigned_my_modules_count #{dir}") when 'repository_cell.value' @@ -459,6 +481,8 @@ class RepositoryDatatableService .order("values.value #{dir}") when 'users.full_name' records.group('users.full_name').order("users.full_name #{dir}") + when 'consumed_stock' + records.order("#{@sortable_columns[column_id - 1]} #{dir}") else records.group(@sortable_columns[column_id - 1]).order("#{@sortable_columns[column_id - 1]} #{dir}") end