Better handling of columns deleting with fixing columns ordering [SCI-783]

This commit is contained in:
Oleksii Kriuchykhin 2016-12-14 15:23:00 +01:00
parent e414af2fc3
commit fa1cf1f04d
5 changed files with 35 additions and 16 deletions

View file

@ -1120,11 +1120,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');
@ -1147,8 +1149,6 @@ function changeToEditMode() {
form
.on('ajax:success', function() {
// Preserve columns ordering before destroying the table
var colOrder = table.colReorder.order();
// Destroy datatable
table.destroy();
@ -1161,10 +1161,6 @@ function changeToEditMode() {
// Remove column from table (=table header) & rows
var th = originalHeader.find('#' + id);
var index = th.index();
var colIndex = $('#samples thead').find('#' + id).index();
if (colIndex >= 0) {
colOrder.splice(colIndex, 1);
}
th.remove();
$('#samples tbody td:nth-child(' + (index + 1) + ')').remove();
@ -1181,8 +1177,6 @@ function changeToEditMode() {
// Re-initialize datatable
table = dataTableInit();
// Restore columns ordering
table.colReorder.order(colOrder);
loadColumnsNames();
// Hide modal

View file

@ -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,11 @@ 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 }

View file

@ -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

View file

@ -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']
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

View file

@ -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 %>
<p><%= t("samples.modal_delete_custom_field.message", cf: @custom_field.name) %></p>
<div class="alert alert-danger" role="alert">
<span class="glyphicon glyphicon-exclamation-sign"></span>