diff --git a/app/assets/javascripts/samples/sample_datatable.js b/app/assets/javascripts/samples/sample_datatable.js index 5a00a6347..69d2d68b1 100644 --- a/app/assets/javascripts/samples/sample_datatable.js +++ b/app/assets/javascripts/samples/sample_datatable.js @@ -1133,11 +1133,13 @@ function changeToEditMode() { var self = $(this); var li = self.closest('li'); var url = li.attr('data-destroy-html-url'); + var colIndex = originalHeader.find('#' + li.attr('data-id')).index(); $.ajax({ url: url, type: 'GET', dataType: 'json', + data: {column_index: colIndex}, success: function(data) { var modalBody = modal.find('.modal-body'); diff --git a/app/controllers/custom_fields_controller.rb b/app/controllers/custom_fields_controller.rb index f549e60a2..04b64a91c 100644 --- a/app/controllers/custom_fields_controller.rb +++ b/app/controllers/custom_fields_controller.rb @@ -53,7 +53,8 @@ class CustomFieldsController < ApplicationController format.json do render json: { html: render_to_string( - partial: 'samples/delete_custom_field_modal_body.html.erb' + partial: 'samples/delete_custom_field_modal_body.html.erb', + locals: { column_index: params[:column_index] } ) } end @@ -61,9 +62,14 @@ class CustomFieldsController < ApplicationController end def destroy + @del_custom_field = @custom_field.dup respond_to do |format| format.json do if @custom_field.destroy + SamplesTable.update_samples_table_state( + @del_custom_field, + params[:custom_field][:column_index] + ) render json: { status: :ok } else render json: { status: :unprocessable_entity } diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index f43975f57..039a0a3aa 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -18,6 +18,6 @@ class CustomField < ActiveRecord::Base after_create :update_samples_table_state def update_samples_table_state - SamplesTable.update_samples_table_state(self) + SamplesTable.update_samples_table_state(self, nil) end end diff --git a/app/models/samples_table.rb b/app/models/samples_table.rb index 40ce10181..2eb6cb147 100644 --- a/app/models/samples_table.rb +++ b/app/models/samples_table.rb @@ -7,14 +7,35 @@ class SamplesTable < ActiveRecord::Base scope :find_status, ->(org, user) { where(user: user, organization: org).pluck(:status) } - def self.update_samples_table_state(custom_field) + def self.update_samples_table_state(custom_field, column_index) samples_table = SamplesTable.where(user: custom_field.user, organization: custom_field.organization) org_status = samples_table.first['status'] - index = org_status['columns'].count - org_status['columns'][index] = SampleDatatable:: - SAMPLES_TABLE_DEFAULT_STATE['columns'].first - org_status['ColReorder'] << index.to_s + if column_index + org_status['columns'].delete(column_index) + org_status['columns'].keys.each do |index| + p index + if index.to_i > column_index.to_i + org_status['columns'][(index.to_i - 1).to_s] = + org_status['columns'].delete(index) + else + index + end + end + org_status['ColReorder'].delete(column_index) + org_status['ColReorder'].map! do |index| + if index.to_i > column_index.to_i + (index.to_i - 1).to_s + else + index + end + end + else + index = org_status['columns'].count + org_status['columns'][index] = SampleDatatable:: + SAMPLES_TABLE_DEFAULT_STATE['columns'].first + org_status['ColReorder'] << index.to_s + end samples_table.first.update(status: org_status) end diff --git a/app/views/samples/_delete_custom_field_modal_body.html.erb b/app/views/samples/_delete_custom_field_modal_body.html.erb index 0df63cde8..d0e64a4f5 100644 --- a/app/views/samples/_delete_custom_field_modal_body.html.erb +++ b/app/views/samples/_delete_custom_field_modal_body.html.erb @@ -1,4 +1,5 @@ <%= bootstrap_form_for @custom_field, url: organization_custom_field_path(@organization, @custom_field, format: :json), remote: :true, method: :delete, data: { role: "destroy-custom-field-form", id: @custom_field.id } do |f| %> + <%= f.hidden_field :column_index, value: column_index %>

<%= t("samples.modal_delete_custom_field.message", cf: @custom_field.name) %>

-<% end %> \ No newline at end of file +<% end %>