Use stimulus collection controller for partLots

This commit is contained in:
Jan Böhmer 2022-08-01 23:01:04 +02:00
parent 537b7fad7f
commit 66b7b2e9bf
6 changed files with 41 additions and 59 deletions

View file

@ -1,6 +1,7 @@
import {Controller} from "@hotwired/stimulus";
import * as bootbox from "bootbox";
import "../../css/bootbox_extensions.css";
export default class extends Controller
{

View file

@ -1,8 +1,8 @@
{% form_theme form with ['Parts/edit/edit_form_styles.html.twig', "bootstrap_4_layout.html.twig"] %}
{% form_theme form with ['Parts/edit/edit_form_styles.html.twig'] %}
{% import 'components/collection_type.macro.html.twig' as collection %}
<div {{ collection.controller(form, 'parameter.delete.confirm') }}>
<div {{ collection.controller(form.parameters, 'parameter.delete.confirm') }}>
<table class="table table-striped table-sm table-bordered table-responsive-md" id="specifications_table" {{ collection.target() }}>
<thead>
<tr>

View file

@ -1,59 +1,22 @@
{% set delete_btn %}
<button type="button" class="btn btn-danger lot_btn_delete" onclick="delete_lot_entry(this);"
{% if not is_granted('lots.delete', part) %}disabled{% endif %}>
<i class="fas fa-trash-alt fa-fw"></i>
{% trans %}part_lot.delete{% endtrans %}
</button>
{% endset %}
{% form_theme form with ['Parts/edit/edit_form_styles.html.twig'] %}
{% import 'components/collection_type.macro.html.twig' as collection %}
<table class="table table-striped table-sm" id="lots_table" data-prototype="{% if form.partLots.vars.prototype is defined %}{{ form_widget(form.partLots.vars.prototype)|e('html_attr') }}{% endif %}">
<div {{ collection.controller(form.partLots, 'part_lot.edit.delete.confirm') }}>
<table class="table table-striped table-sm" id="lots_table" {{ collection.target() }}>
<tbody>
{% for lot in form.partLots %}
<tr>
<td>
{{ form_widget(lot) }}
</td>
<td>
{{ delete_btn }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
<button type="button" class="btn btn-success" onclick="create_lot_entry(this)"
<button type="button" class="btn btn-success" {{ collection.create_btn() }}
{% if not is_granted('lots.create', part) %}disabled{% endif %}>
<i class="fas fa-plus-square fa-fw"></i>
{% trans %}part_lot.create{% endtrans %}
</button>
<script>
function delete_lot_entry(btn) {
window.bootbox.confirm('{% trans %}part_lot.edit.delete.confirm{% endtrans %}', function (result) {
if(result) {
$(btn).parents("tr").remove();
}
})
}
function create_lot_entry(btn) {
//Determine the table, so we can determine, how many entries there are already.
$holder = $("#lots_table");
var index = $holder.children("tbody").children("tr").length;
var newForm = $holder.data("prototype");
//Increase the index
newForm = newForm.replace(/__name__/g, index);
newForm = '<td>' + newForm + '</td>';
$newFormRow = $('<tr></tr>').html(newForm);
//Add delete button
$btn = '<td>' + '{{ delete_btn|e('js') }}' + '</td>';
$newFormRow.append($btn);
$holder.append($newFormRow);
}
</script>
</div>

View file

@ -1,8 +1,8 @@
{% form_theme form with ['Parts/edit/edit_form_styles.html.twig', "bootstrap_4_layout.html.twig"] %}
{% form_theme form with ['Parts/edit/edit_form_styles.html.twig'] %}
{% import 'components/collection_type.macro.html.twig' as collection %}
<div {{ collection.controller(form, 'parameter.delete.confirm') }}>
<div {{ collection.controller(form.parameters, 'parameter.delete.confirm') }}>
<table class="table table-striped table-sm table-bordered table-responsive-md" id="specifications_table" {{ collection.target() }}>
<thead>
<tr>

View file

@ -81,3 +81,21 @@
</td>
</tr>
{% endblock %}
{% block part_lot_widget %}
{% import 'components/collection_type.macro.html.twig' as collection %}
<tr>
<td>
{{ form_widget(form) }}
</td>
<td>
{% dump(form) %}
<button type="button" class="btn btn-danger lot_btn_delete" {{ collection.delete_btn() }}
{% if not is_granted('lots.delete', form.parent.parent.vars.data) %}disabled{% endif %}>
<i class="fas fa-trash-alt fa-fw"></i>
{% trans %}part_lot.delete{% endtrans %}
</button>
{{ form_errors(form) }}
</td>
</tr>
{% endblock %}

View file

@ -1,7 +1,7 @@
{% macro controller(form, deleteMessage) %}
{{ stimulus_controller('elements/collection_type', {
'deleteMessage': 'parameter.delete.confirm'|trans,
'prototype': form_widget(form.parameters.vars.prototype)|e('html_attr')
'prototype': form_widget(form.vars.prototype)|e('html_attr')
}) }}
{% endmacro %}