我正在尝试使用来自用户的信息来填充集合,但该列表仅来自最后一项。
该系列在Libro中嵌入了Capitulo形式的集合。 LibroType是:
class LibroType extends AbstractType {
private $edita;
public function __construct($edita) {
$this->edita = $edita;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('titulo', 'text', array("label" => "Titulo: ", "required" => true, "attr" => array('class' => 'form-control')))
->add('anio', 'integer', array("label" => "Año: ", "required" => true, "attr" => array('class' => 'form-control')))
->add('numeroPaginas', 'integer', array("label" => "Páginas: ", "required" => true, "attr" => array('min' => 1, 'class' => 'form-control'),'precision' => 0, 'constraints' => array(
new Regex(array(
'pattern' => '/^[0-9]\d*$/',
'message' => 'Use solo números positivos.'
)))))
->add('esPmbok', 'choice', array('choices' => array(1 => 'Sí', 0 => 'No'), 'label' => '¿Es Pmbok? ', 'multiple' => false, 'expanded' => true, 'data' => $this->edita))
->add('idiomas', 'entity', array('class' => 'UciBaseDatosBundle:Idiomas', 'required' => true))
->add('pmbok', new PmbokType(),array('required' => false))
->add('capitulos', 'collection', array(
'type' => new CapituloType(),
'by_reference' => true,
'allow_delete' => true,
'allow_add' => true,
'prototype' => true,
'label' => FALSE))
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'Uci\Bundle\BaseDatosBundle\Entity\Libro'
));
}
/**
* @return string
*/
public function getName() {
return 'uci_bundle_basedatosbundle_libro';
}
}
VistaLibro的一部分:registrarLibro.html.twig:
<!-- /Tabla capitulos -->
<table class="table demo table-bordered" id="tablaCapitulos" data-filter="#filter"
data-prototype="
{% filter escape %}
{{ include('UciAdministradorBundle:VistaLibro:prototipoCapitulo.html.twig', { 'form': form.capitulos.vars.prototype|e }) }}
{% endfilter %}">
<thead>
<tr>
<th data-hide="phone">
Numero
</th>
<th data-hide="phone">
Nombre
</th>
<th data-sort-ignore="true" data-hide="phone" data-name="Acciones"></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<a href="#" id="capitulo_link">Añadir capítulo</a>
<!-- /Acaba el contenido de capitulo-->
表格中调用的prototipoCapitulo.html.twig。
<td class="cols-xs-12 col-sm-10 col-md-8 col-lg-3">
{{ form_widget(form.numeroCapitulo) }}
</td>
<td>
{{ form_widget(form.nombreCapitulo) }}
</td>
动作方法:
public function aRegistrarLibroAction(Request $request) {
$entity = new Libro();
$em = $this->getDoctrine()->getManager();
$form = $this->createForm(new LibroType(0), $entity);
$form->handleRequest($request);
$error = '';
if ($request->getMethod() == 'POST') {
$error = $form->getErrors();
if ($form->isValid()) {
$em->getConnection()->beginTransaction();
try {
$em->persist($entity);
$this->guardarCapitulos($em, $entity);
if ($entity->getEsPmbok() == 1) {
$this->guardarPmbok($em, $entity);
}
$em->flush();
$em->commit();
return $this->redirectToRoute('uci_administrador_indicelibro');
} catch (Exception $e) {
$em->getConnection()->rollback();
$error = $e;
}
}
}
return $this->render('UciAdministradorBundle:VistaLibro:registrarLibro.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
'error' => $error,
));
}
使用js:
function addCapitulo() {
var collectionHolder = $('#tablaCapitulos');
var prototype = collectionHolder.attr('data-prototype');
var form = prototype.replace(/__name__/g, collectionHolder.children().length); //importante
var removeFormA = $('<td href="#" onclick="addTagFormDeleteLink(event, this); "><span class="navigationSpace glyphicon glyphicon-remove" style="color: red; font-size:180%" ></span></td>');
var newTr = $('<tr></tr>');
newTr.append(form);
newTr.append(removeFormA);
collectionHolder.append(newTr);
return prototype;
}
function addTagFormDeleteLink(e, elemento) {
e.preventDefault();
var removeFormA = elemento.parentNode;
removeFormA.remove();
}
$('#capitulo_link').on('click', function (e) {
e.preventDefault();
addCapitulo();
});
我感谢你能给我的任何帮助。