1
0

Dynamic thresholds listing in targets form

This commit is contained in:
cryptogopher
2021-02-20 13:55:04 +01:00
parent 8b17b33603
commit 9e3dc24a14
8 changed files with 47 additions and 40 deletions

View File

@@ -22,16 +22,9 @@
{include_blank: true, required: true, label: :field_target},
onchange: "showQuantityPath(event);" %>
<%= target_f.fields_for :thresholds do |threshold_f| %>
<%= render partial: 'thresholds/form', locals: {threshold_f: threshold_f} %>
<% end %>
<% last_quantity = target_f.object.thresholds.last.quantity %>
<% unless last_quantity.leaf? %>
<%= target_f.fields_for 'thresholds_attributes', Threshold.new do |threshold_f| %>
<%= render partial: 'thresholds/form',
locals: {threshold_f: threshold_f, parent: last_quantity} %>
<% end %>
<% end %>
<%= render partial: 'targets/thresholds_form',
locals: {target_f: target_f,
last_quantity: target_f.object.thresholds.last.quantity} %>
<%= link_to t(".button_delete_target"), '#', class: 'icon icon-del',
style: (@targets.many? ? "" : "display:none"),

View File

@@ -0,0 +1,28 @@
<% target_f.object.thresholds.new unless last_quantity.leaf? %>
<%= target_f.fields_for :thresholds, child_index: '' do |threshold_f| %>
<% threshold_q = threshold_f.object.quantity %>
<% parent_id = threshold_q.nil? ? last_quantity.id : threshold_q.parent_id %>
<%= threshold_f.collection_select :quantity_id,
@project.quantities.target.children_of(parent_id),
:id, :name,
{prompt: parent_id.nil? ? false : '.', required: true, no_label: true},
{autocomplete: 'off',
onchange: "$.ajax({
url: '#{subthresholds_project_targets_path(parent_id: parent_id)}',
data: 'quantity_id=' + $(this).val(),
dataType: 'html',
success: function(data) {
$(event.target).nextAll().addBack().last().after(data).end().remove();
}
});
return false;"} %>
<%# {autocomplete: 'off',
data: {remote: true, url: subthresholds_project_targets_path(parent_id: parent_id)}} %>
<% unless threshold_q.nil? %>
<%= threshold_f.hidden_field :_destroy %>
<%= threshold_f.number_field :value, {size: 8, step: :any, no_label: true} %>
<%= threshold_f.collection_select :unit_id, @project.units, :id, :shortname,
{no_label: true} %>
<% end %>
<% end %>

View File

@@ -0,0 +1,4 @@
<%= fields_for 'goal[targets_attributes][]', @target do |target_f| %>
<%= render partial: 'targets/thresholds_form',
locals: {target_f: target_f, last_quantity: @last_quantity} %>
<% end %>

View File

@@ -1,5 +0,0 @@
<% if @threshold %>
$('#targets').html('<%= j render partial: 'targets/index' %>');
<% else %>
$(event.target).empty();
<% end %>