Use datatable ajax to send version param [SCI-8007]

This commit is contained in:
sboursen-scinote 2023-03-09 21:59:14 +01:00
parent c4bdbdec9a
commit 506c750eee
2 changed files with 43 additions and 31 deletions

View file

@ -5,6 +5,7 @@
// Global variables
var ProtocolsIndex = (function() {
const ALL_VERSIONS_VALUE = 'All';
var PERMISSIONS = ['archivable', 'restorable', 'copyable'];
var rowsSelected = [];
var protocolsTableEl = null;
@ -566,10 +567,27 @@ var ProtocolsIndex = (function() {
modal.modal('show');
let childrenTableEl = modalBody.find('#linked-children-table');
let versionFromDropdown = ALL_VERSIONS_VALUE;
const initVersionsDropdown = (childrenDatatable) => {
const versionSelector = $('#version-selector');
dropdownSelector.init(versionSelector, {
noEmptyOption: true,
singleSelect: true,
selectAppearance: 'simple',
closeOnSelect: true,
onSelect: function() {
versionFromDropdown = dropdownSelector.getValues(versionSelector);
childrenDatatable.ajax.reload();
}
});
};
let childrenDatatable;
if (childrenTableEl) {
// Only initialize table if it's present
childrenTableEl.DataTable({
childrenDatatable = childrenTableEl.DataTable({
autoWidth: false,
dom: 'RBtpl',
stateSave: false,
@ -578,7 +596,14 @@ var ProtocolsIndex = (function() {
serverSide: true,
ajax: {
url: childrenTableEl.data('source'),
type: 'POST'
type: 'POST',
data: function(d) {
if (versionFromDropdown !== ALL_VERSIONS_VALUE) {
d.version = versionFromDropdown;
}
return d;
}
},
colReorder: {
fixedColumnsLeft: 1000000 // Disable reordering
@ -595,34 +620,6 @@ var ProtocolsIndex = (function() {
language: {
lengthMenu: '_MENU_'
},
initComplete: function() {
const versionSelector = $('#version-selector');
const table = childrenTableEl.DataTable();
dropdownSelector.init(versionSelector, {
noEmptyOption: true,
singleSelect: true,
selectAppearance: 'simple',
closeOnSelect: true,
onSelect: function() {
const selectedValue = dropdownSelector.getValues(versionSelector);
if (selectedValue === 'All') {
table.rows().eq(0).each(index => {
const row = table.row(index);
row.nodes().to$().show();
});
} else {
table.rows().eq(0).each(index => {
const row = table.row(index);
if (Number(row.data().version) === Number(selectedValue)) {
row.nodes().to$().show();
} else {
row.nodes().to$().hide();
}
});
}
}
});
},
fnDrawCallback: function() {
animateSpinner(this, false);
},
@ -631,6 +628,8 @@ var ProtocolsIndex = (function() {
}
});
}
initVersionsDropdown(childrenDatatable);
},
error: function() {
// TODO

View file

@ -32,7 +32,6 @@ class ProtocolLinkedChildrenDatatable < CustomDatatable
records.map do |record|
{
'DT_RowId' => record.id,
'version' => record.parent.version_number,
'1' => record_html(record)
}
end
@ -46,11 +45,15 @@ class ProtocolLinkedChildrenDatatable < CustomDatatable
.includes(my_module: { experiment: :project })
.references(my_module: { experiment: :project })
.where(protocol_type: Protocol.protocol_types[:linked])
records = linked_protocols
.where(parent: @protocol)
.or(
linked_protocols.where(parent: Protocol.where(parent: @protocol))
)
records = filter_child_records(records)
records.distinct
end
@ -80,4 +83,14 @@ class ProtocolLinkedChildrenDatatable < CustomDatatable
res += '</ol>'
res
end
def filter_child_records(records)
if params[:version].present?
version = params[:version]
records = records.joins("LEFT JOIN protocols protocol_parents " \
"ON protocols.parent_id = protocol_parents.id ")
.where("protocol_parents.version_number = #{version}")
end
records
end
end