Symfony 2 - 点击更新数据库

时间:2017-01-20 10:24:41

标签: php jquery forms symfony

单击复选框时,我尝试更新数据库的'archive'值。搜索网站和谷歌后,我设法做到了。 但是,通过添加第二篇文章,它只适用于第一篇文章。如果你有想法,我将不胜感激! 谢谢 我安装了fosjsroutingbundle。

这是我的控制器:

 public function archiveAction($id, Request $request)
{

    if ($request->isXmlHttpRequest()) {

        $em = $this->getDoctrine()->getManager();
        $glasse = $em->getRepository('SosMontagesBundle:Glasse')->find($id);
        $glasseArchiveStat = $glasse->getArchive();

        if ($glasseArchiveStat == false) {
            $glasse->setArchive(true);
            $em->flush();
        } else {
            $glasse->setArchive(false);
            $em->flush();
        }

        return new Response('d');

    }


}

我的路线:

sos_montage_archive:
path:    /admin/archive/{id}
defaults: { _controller: SosMontagesBundle:Admin:archive }
requirements:
    id: \d+
options:
    expose: true

我的观点:

{% extends '@SosMontages/layout.html.twig' %}

{%block content%}

<div class="col-md-6 col-md-offset-3">
    <h2>Liste des lunettes</h2>
    <table class="table table-striped">
        <thead>
        <tr>
            <th>Id</th>
            <th>Marque - Model</th>
            <th>Description</th>
            <th>Archive</th>
            <th>Ordre</th>
            <th>Actions</th>

        </tr>
        </thead>
        <tbody>
        {% for article in pagination %}
            <tr {% if loop.index is odd %}class="color"{% endif %}>
                <td>{{ article.id }}</td>
                <td>{{ article.name ~ ' - ' ~ article.brand }}</td>
                <td>{{ article.content }}</td>
                {% if article.archive == false %}
                    <td><input type="checkbox" name="{{ article.id }}" id="archive"></td>
                {% else %}
                    <td><input type="checkbox" name="{{ article.id }}" id="archive" checked></td>
                {% endif %}
                <td></td>
                <td><a href=""><i class="fa fa-pencil fa-2x" aria-hidden="true"></i></a>
                    <a href=""><i class="fa fa-trash-o fa-2x" aria-hidden="true"></i></a></td>

            </tr>
        {% endfor %}

        </tbody>
    </table>
    <div class="navigation">
        {{ knp_pagination_render(pagination) }}
    </div>
</div>

{%endblock%} {%block javascript%}

<script src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>
<script src="{{ path('fos_js_routing_js', { callback: 'fos.Router.setData' }) }}"></script>
<script>
    $(document).ready(function () {
        $("#archive").click(function () {

            var id = $("#archive").attr('name');


            var DATA = 'sentValue=' + id;
            $.ajax({
                type: "POST",
                url: Routing.generate('sos_montage_archive', { id: id }),
                data: DATA,
                cache: false,
                success: function (data) {
                    alert("database has been updated");

                }
            });
        });

    });

</script>

{%endblock%}

2 个答案:

答案 0 :(得分:0)

您的复选框上的点击监听器可能不正确。您应该使用更改侦听器,而它是一个复选框。在js中更改您的点击功能以更改:

AppBundle

但是,您还有两个复选框,只有一个ID。这可能会产生奇怪的错误。将您的ID更改为#archive1和#archive2可能会修复您的代码。

答案 1 :(得分:0)

您必须修复HTML并区分您的#archive id选择器。

你可以使用loop.index来做到这一点,例如:

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="300" Width="400">
    <Grid>
        <Label Foreground="Red" FontSize="72" FontWeight="Bold">Title
            <Label.RenderTransform>
                <TranslateTransform x:Name="TitleLeftToRight"/>
            </Label.RenderTransform>
            <Label.Triggers>
                <EventTrigger RoutedEvent="Label.Loaded">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation DecelerationRatio="1.0" Storyboard.TargetName="TitleLeftToRight" Storyboard.TargetProperty="X" From="-300" To="100" Duration="00:00:00.5" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Label.Triggers>
        </Label>
        <Label Foreground="Blue" FontSize="24" Margin="0,85,0,0">Sub Title
            <Label.RenderTransform>
                <TranslateTransform x:Name="SubTitleLeftToRight" X="-200"/>
            </Label.RenderTransform>
            <Label.Triggers>
                <EventTrigger RoutedEvent="Label.Loaded">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation DecelerationRatio="1.0" Storyboard.TargetName="SubTitleLeftToRight" Storyboard.TargetProperty="X" From="-300" To="125" BeginTime="0:0:0.5" Duration="00:00:00.5" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Label.Triggers>
        </Label>
    </Grid>
</Window>

Twig doc: http://twig.sensiolabs.org/doc/2.x/tags/for.html#the-loop-variable