Save handson table metadata [SCI-7834]

This commit is contained in:
Anton 2023-02-06 12:55:02 +01:00
parent 958f9784b0
commit 3849b1a7a1
11 changed files with 46 additions and 21 deletions

View file

@ -18,7 +18,7 @@
root.find('div.hot-table').each(function() {
var $container = $(this).find('.step-result-hot-table');
var contents = $(this).find('.hot-contents');
var metadata = $(this).find('.hot-metadata');
$container.handsontable({
width: '100%',
startRows: 5,
@ -27,23 +27,14 @@
colHeaders: true,
fillHandle: false,
formulas: true,
cells: function(row, col) {
var cellProperties = {};
if (col >= 0) {
cellProperties.readOnly = true;
} else {
cellProperties.readOnly = false;
}
return cellProperties;
}
data: JSON.parse(contents.attr('value')).data,
cell: JSON.parse(metadata.val() || '{}').cells || [],
readOnly: true
});
let hot = $container.handsontable('getInstance');
let data = JSON.parse(contents.attr('value'));
if (Array.isArray(data.data)) hot.loadData(data.data);
setTimeout(() => {
hot.render()
}, 0)
hot.render();
}, 500);
});
}

View file

@ -8,6 +8,7 @@
var $container = $(this).find('.hot');
var data = null;
var contents = $(this).find('.hot-contents');
var metadata = JSON.parse($(this).find('.hot-metadata').val() || '{}');
if (contents.attr('value')) {
data = JSON.parse(contents.attr('value')).data;
}
@ -21,6 +22,7 @@
rowHeaders: true,
colHeaders: true,
contextMenu: true,
cell: metadata.cells || [],
formulas: true,
preventOverflow: 'horizontal',
afterChange: function() {
@ -34,8 +36,16 @@
$form.submit(function(){
var hot = $('.hot').handsontable('getInstance');
var contents = $('.hot-contents');
var metadata = $('.hot-metadata');
var data = JSON.stringify({data: hot.getData()});
contents.attr('value', data);
metadata.attr('value', JSON.stringify({cells: hot.getCellsMeta().map((x) => {
return {
col: x.col,
row: x.row,
className: x.className || ''
}
})}))
return true;
});
}

View file

@ -170,7 +170,8 @@ class ResultTablesController < ApplicationController
:name, :archived,
table_attributes: [
:id,
:contents
:contents,
:metadata
]
)
end

View file

@ -60,7 +60,7 @@ module StepElements
private
def table_params
params.permit(:name, :contents)
params.permit(:name, :contents, metadata: {})
end
def load_table

View file

@ -158,6 +158,13 @@
let tableData = JSON.stringify({data: this.tableObject.getData()});
this.element.attributes.orderable.contents = tableData;
this.element.attributes.orderable.metadata = {cells: this.tableObject.getCellsMeta().map((x) => {
return {
col: x.col,
row: x.row,
className: x.className || ''
}
})};
this.update();
this.editingTable = false;
},
@ -167,6 +174,7 @@
loadTableData() {
let container = this.$refs.hotTable;
let data = JSON.parse(this.element.attributes.orderable.contents);
let metadata = this.element.attributes.orderable.metadata || {};
this.tableObject = new Handsontable(container, {
data: data.data,
width: '100%',
@ -174,6 +182,7 @@
startCols: 5,
rowHeaders: true,
colHeaders: true,
cell: metadata.cells || [],
contextMenu: this.editingTable,
formulas: true,
preventOverflow: 'horizontal',

View file

@ -4,7 +4,7 @@ class TableSerializer < ActiveModel::Serializer
include Canaid::Helpers::PermissionsHelper
include Rails.application.routes.url_helpers
attributes :name, :contents, :urls, :icon
attributes :name, :contents, :urls, :icon, :metadata
def contents
object.contents_utf_8

View file

@ -6,6 +6,7 @@
<div class="editable-table">
<%= f.fields_for :table do |ff| %>
<%= ff.hidden_field(:contents, value: ff.object.contents_utf_8, class: "hot-contents" ) %>
<%= ff.hidden_field(:metadata, value: ff.object.metadata, class: "hot-metadata" ) %>
<div class="hot">
</div>
<% end %>

View file

@ -6,6 +6,7 @@
<div class="editable-table" style="margin-bottom: 25px;">
<%= f.fields_for :table do |ff| %>
<%= ff.hidden_field(:contents, value: ff.object.contents, class: "hot-contents" ) %>
<%= ff.hidden_field(:metadata, value: ff.object.metadata, class: "hot-metadata" ) %>
<div class="hot">
</div>
<% end %>

View file

@ -1,5 +1,6 @@
<div class="hot-table">
<%= hidden_field(result.table, :contents, value: result.table.contents_utf_8, class: "hot-contents" ) %>
<%= hidden_field(result.table, :metadata, value: result.table.metadata, class: "hot-metadata" ) %>
<div class="step-result-hot-table">
</div>
</div>

View file

@ -0,0 +1,7 @@
# frozen_string_literal: true
class AddMetadataToTable < ActiveRecord::Migration[6.1]
def change
add_column :tables, :metadata, :jsonb
end
end

View file

@ -1031,7 +1031,8 @@ CREATE TABLE public.my_modules (
my_module_status_id bigint,
status_changing boolean DEFAULT false,
changing_from_my_module_status_id bigint,
last_transition_error jsonb
last_transition_error jsonb,
provisioning_status integer
);
@ -2681,7 +2682,8 @@ CREATE TABLE public.tables (
last_modified_by_id bigint,
data_vector tsvector,
name character varying DEFAULT ''::character varying,
team_id integer
team_id integer,
metadata jsonb
);
@ -8595,6 +8597,8 @@ INSERT INTO "schema_migrations" (version) VALUES
('20220914124900'),
('20221007113010'),
('20221028085051'),
('20221222123021');
('20221122132857'),
('20221222123021'),
('20230206095817');