我已经按照如何为集合类型制作表单/树枝的教程。我现在遇到的问题是我喜欢改变表单设计,但我不知道如何做到这一点。我希望'Artikel'旁边有'Aantal'属性。所以我希望你们可以帮助我。
树枝形式
{% extends 'layout/default.html.twig' %}
{% block title %}{{ title }}{% endblock %}
{% block content %}
<div class="container">
<div class="row">
<div class="col-md-12">
<br>
<h1>{{ title }}</h1>
<form method="post" action="">
{{ form_start(form) }}
{{ form_row(form.bestelordernummer) }}
{{ form_row(form.leverancier) }}
{{ form_row(form.keuringseisen) }}
{{ form_label(form.bestelregels) }}
<ul id="bestelregels-fields-list"
data-prototype="{{ form_widget(form.bestelregels.vars.prototype)|e }}"
data-widget-tags="{{ '<li></li>'|e }}">
{% for bestelregelField in form.bestelregels %}
<li>
{{ form_errors(bestelregelField) }}
{{ form_widget(bestelregelField) }}
</li>
{% endfor %}
</ul>
<a href="#"
class="add-another-collection-widget"
data-list="#bestelregels-fields-list">Bestelregel toevoegen</a>
<hr />
<input class="btn btn-default" type="submit">
<a href="javascript:history.back()">Annuleren</a>
<div style="display: none;">
{{ form_end(form) }}
</div>
</form>
</div>
</div>
<hr>
</div>
{% endblock %}
Custom.js
jQuery(document).ready(function () {
jQuery('.add-another-collection-widget').click(function (e) {
e.preventDefault();
var list = jQuery(jQuery(this).attr('data-list'));
// Try to find the counter of the list
var counter = list.data('widget-counter') | list.children().length;
// If the counter does not exist, use the length of the list
if (!counter) { counter = list.children().length; }
// grab the prototype template
var newWidget = list.attr('data-prototype');
// replace the "__name__" used in the id and name of the prototype
// with a number that's unique to your emails
// end name attribute looks like name="contact[emails][2]"
newWidget = newWidget.replace(/__name__/g, counter);
// Increase the counter
counter++;
// And store it, the length cannot be used if deleting widgets is allowed
list.data(' widget-counter', counter);
// create a new list element and add it to the list
var newElem = jQuery(list.attr('data-widget-tags')).html(newWidget);
newElem.appendTo(list);
});
});
'Bestelordernummer,Leverancier和Keuringseisen'的表格
<?php
namespace AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class BestellingType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
//gebruiken wat je nodig hebt, de id hoeft er niet bij als deze auto increment is
$builder
->add('bestelordernummer', IntegerType::class) //naam is b.v. een attribuut of variabele van klant
;
$builder
->add('leverancier', TextType::class) //naam is b.v. een attribuut of variabele van klant
;
$builder
->add('keuringseisen', TextType::class, array ('required' => false)) //naam is b.v. een attribuut of variabele van klant
;
$builder
->add('bestelregels', CollectionType::class, [
'entry_type' => BestelregelType::class,
'allow_add' => true
])
;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Bestelling'
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_bestelregel';
}
}
'Artikel和Aantal'的表格
<?php
namespace AppBundle\Form\Type;
use AppBundle\Entity\Artikel;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class BestelregelType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
//gebruiken wat je nodig hebt, de id hoeft er niet bij als deze auto increment is
$builder
->add('artikel', EntityType::class, [
'class' => 'AppBundle:Artikel',
'choice_label' => function (Artikel $artikel) {
return $artikel->getArtikelnummer() . ' - ' . $artikel->getOmschrijving() . ' - Bestelserie: ' . $artikel->getBestelserie();
}
])
->add('aantal');
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Bestelregel'
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_bestelregel';
}
}
提前致谢。
答案 0 :(得分:1)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper-children">
<!-- Additional contents by Ajax will come here -->
<div class="myContainer">
<div class="spinner"></div>
<span class="inner"></span>
<span class="inner"></span>
<span class="inner"></span>
</div>
</div>
<div class="...">
<a class="load-more-btn">View More</a>
</div>