【问题标题】:Symfony 2 - Update database on clickSymfony 2 - 点击更新数据库
【发布时间】:2017-06-05 07:15:10
【问题描述】:

单击复选框时,我尝试更新数据库的“存档”值。在搜索了网站和谷歌之后,我设法做到了。 但是,通过添加第二篇文章,它只适用于第一篇。如果您有任何想法,我将不胜感激! 谢谢 我已经安装了 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' %}

{% 块内容 %}

<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>

{% 端块 %} {% 阻止 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>

{% 端块 %}

【问题讨论】:

  • 这是基本的 HTML。不能有多个具有相同 ID 的元素。为这些复选框分配一个 css 类并将您的 click 操作挂钩到该复选框而不是 ID。

标签: php jquery forms symfony


【解决方案1】:

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

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

id="archive{{ loop.index }}" //output : archive1, archive2...

树枝文档: http://twig.sensiolabs.org/doc/2.x/tags/for.html#the-loop-variable

【讨论】:

    【解决方案2】:

    您的复选框上的点击监听器可能不正确。当它是一个复选框时,您应该使用更改侦听器。在js中改变你的点击函数来改变:

    $('#archive').change(function() {
       if($(this).is(":checked")) {
          //'checked' event code
          return;
       }
       //'unchecked' event code
    });
    

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

    【讨论】:

    • 感谢您的回答。但是,我的名字是由小球自动生成的。
    • 那你可以尝试把#archive 作为类名吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 2022-01-18
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多