symfony 3.4将变量传递给树枝中的模态 [英] symfony 3.4 pass variable to a modal in twig

查看:68
本文介绍了symfony 3.4将变量传递给树枝中的模态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助来解决我的问题.我有一个页面,上面有"equipos"列表.表格中的每个"equipo"都有一个编辑"按钮,页面上会显示另一个按钮以添加新的"equipos".

I need help to solve my problem. I have a page with my list of "equipos". Every "equipo" in the table has an Edit buttom, and the page show another buttom to add new "equipos".

到目前为止,我设法使用{{render()}}语法为NEW调用了一个模态,equipoNewModal可以正常工作,因为它是Symfony中的静态"路由(/equipo/new).但是EDIT无效,因为我无法将"equipo"变量传递给equipoEditModal并获取ID以完成路由(/equipo/{id}/edit)并调用控制器.

So far I manage to call a modal for NEW using the {{ render() }} syntax, the equipoNewModal works fine because is an "static" route (/equipo/new) in Symfony; but the EDIT don't work because I can't pass the "equipo" variable to the equipoEditModal and get the id to complete the route (/equipo/{id}/edit) and call the controller.

Symfony无法呈现页面并返回错误:变量"equipo"不存在.

Symfony can't render the page and return an error: Variable "equipo" does not exist.

如果我在list.html.twig模板中使用href = {{path('edit_equipo',{'id':equipo.id})}}创建标签,则控制器不是问题,并跳过模态我可以编辑每个设备.要解散控制器,如果我对行进行了硬编码:

The controller isn't the problem, if I create an tag with href={{ path('edit_equipo', {'id': equipo.id}) }} in the list.html.twig template and skip the modal I can edit every equipo. To dismiss the controller, if I hardcoded the line:

{{ form_start(equipoForm, {'action': path('edit_equipo', {'id': equipo.id})}) }}

在edit.html.twig中:

in the edit.html.twig to:

 {{ form_start(equipoForm, {'action': path('edit_equipo', {'id': 1})}) }}

编辑动作有效,当然,对于每一个装备,编辑动作都会调用数据库中id = 1的项目的版本,但它表示控制器可以正常工作.

the edit action works, of course for every equipo the edit action call the edition of the item with id=1 in the database, but it say that the controller works fine.

我正在丢失一些东西,希望社区能够找到解决方案...对不起我的英语.

I'm missing something and hope the community find the solution... sorry my english.

==============

==============

<table id="datatable-buttoms" class="table table-striped table-bordered">
    <thead>
        <tr>
            <th>ID</th>
            <th>EQUIPOS</th>
        </tr>
    </thead>
    <tbody>
            {% for equipo in equipos %}
                <tr>
                    <td>{{ equipo.id }}</td>
                    <td>{{ equipo.equipo }}</td>
                    <td>{{ equipo.nomenclador }}</td>
                    <td>{{ equipo.nomenclador.especialidad }}</td>
                    <td>
                    <button type="button" class="btn btn-primary" href="" data-toggle="modal" data-target="#equipoEditModal">
                        Editar
                    </button>
                    <button type="button" class="btn btn-danger" href="" data-toggle="modal" data-target="#equipoDeleteModal">
                        Eliminar
                    </button>
                </td>
            </tr>
           {{ render(controller('AppBundle:Equipo:edit', {'id': equipo.id})) }}
        {% endfor %}
    </tbody>
</table>

<button type="button" class="btn btn-primary" href="" data-toggle="modal" data-target="#equipoNewModal">
    Agregar
</button>
{{ render(controller('AppBundle:Equipo:new')) }}

=============

=============

<div class="modal fade" id="equipoNewModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">NUEVO</h4>
            </div>
            <div class="modal-body">
                {{ form_start(equipoForm, {'action': path('new_equipo')}) }}
                {{ form_widget(equipoForm) }}
                <button type="submit" class="btn btn-primary">Guardar</button>
                {{ form_end(equipoForm) }}
            </div>
        </div>
    </div>
</div>

==============

==============

<div class="modal fade" id="equipoEditModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">EDITAR</h4>
            </div>
                <div class="modal-body">
                    {{ form_start(equipoForm, {'action': path('edit_equipo', {'id': equipo.id})}) }}
                        {{ form_widget(equipoForm) }}
                        <button type="submit" class="btn btn-primary">Guardar</button>
                    {{ form_end(equipoForm) }}
                </div>
        </div>
    </div>
</div>

===============

===============

    /**
     * @Route("/equipo/{id}/edit", name="edit_equipo")
     */
    public function editAction(Request $request, Equipo $equipo)
    {
        $form = $this->createForm(EquipoFormType::class, $equipo);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $equipo = $form->getData();

            $em = $this->getDoctrine()->getManager();
            $em->persist($equipo);
            $em->flush();

            return $this->redirectToRoute('list_equipos');
        }

        return $this->render('sysreport/equipos/edit.html.twig', [
            'equipoForm' => $form->createView(),
        ]);
    }

要解决该问题,只需在editAction控制器中添加@Nobady所说的行即可.

To solve the problem only add the line that @Nobady says in the editAction controller...

要根据list.html.twig文件中的Equipo更改数据目标调用每个模式:

To call every modal depending of the equipo change data-target in the list.html.twig file:

<button type="button" class="btn btn-primary" href="" data-toggle="modal" data-target="#equipo{{ equipo.id }}">

,当然也包含在edit.html.twig文件中:

and of course in the edit.html.twig file too:

<div class="modal fade" id="equipo{{ equipo.id }}">

推荐答案

要解决,则必须将equipo作为参数传递,例如在Edit Controller中:

to solve then you have to pass equipo as parameter, like this in Edit Controller:

/**
 * @Route("/equipo/{id}/edit", name="edit_equipo")
 */
public function editAction(Request $request, Equipo $equipo)
{
    $form = $this->createForm(EquipoFormType::class, $equipo);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $equipo = $form->getData();

        $em = $this->getDoctrine()->getManager();
        $em->persist($equipo);
        $em->flush();

        return $this->redirectToRoute('list_equipos');
    }

    return $this->render('sysreport/equipos/edit.html.twig', [
        'equipoForm' => $form->createView(),
        'equipo' => $equipo
    ]);
}

这篇关于symfony 3.4将变量传递给树枝中的模态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆