TWIG布局中的jQuery函数;工作不正常


jQuery function in TWIG layout doesn't work properly

我正在尝试为我的博客创建一个简单的"admin"页面。现在我想创建一些"动态"行为来锁定/解锁用户。然后,我想用另一个取自json的表行替换单个表行。我写了一个简单的函数,但不幸的是它不能正常工作。。。

当我为用户id使用常量值(仅用于测试)时,它是有效的,但仅用于一行。其他按钮没有任何作用。然后我试图将id作为参数发送给我的函数,但现在它说它不存在于我的TWIG中(这是真的)。

我想让它发挥作用,因为当您只锁定一个用户或执行另一个单一操作时,重新加载所有页面不是一个好主意。

如何使我的功能以良好的方式工作?

脚本

$(document).ready(function(){
        $(".LockUserButton").click(function(id){
            $(this).closest("tr").toggleClass('locked progress-bar-striped');
            $.ajax({
                type: 'POST',
                url: "{{ path('lock_ajax', {'id': id }) }}",
                data: { user_id: "{{ user.id }}" },
                dataType: 'json',
                success: function () {
                    $(this).closest("tr").toggleClass('locked progress-bar-striped');
                }
            });
        });
    });

TWIG

<div class="table-content">
    {% for u in users %}
            {% if u.locked %}
                <tr id="tableRow" class="locked progress-bar-striped">
            {% else %}
                <tr id="tableRow" class="">
            {% endif %}
            {% if u.roles[0] == 'ROLE_SUPER_ADMIN' %}
                <td id="roles">
                    <h4><span class="glyphicon glyphicon-star admin-star" data-toggle="tooltip" data-placement="right" title="{{ u.roles[0] }}"></span></h4>
                </td>
                <td>{{ u.username }}</td>
                <td>{{ u.email }}</td>
                <td>
                    <span class="glyphicon glyphicon-lock admin-lock" data-toggle="tooltip" data-placement="right" title="Cannot modyfi this user!"></span>
                </td>
                <td>
                    <span class="glyphicon glyphicon-lock admin-lock" data-toggle="tooltip" data-placement="right" title="Cannot modyfi this user!"></span>
                </td>
            {% else %}
                <td id="roles">
                    <h4><span class="glyphicon glyphicon-star-empty user-star" data-toggle="tooltip" data-placement="right" title="{{ u.roles[0] }}"></span></h4>
                </td>
                <td>{{ u.username }}</td>
                <td>{{ u.email }}</td>
                <td>
                    <div class="btn btn-custom LockUserButton">LOCK USER WITHOUT RELOADING</div>
                </td>
                <td>
                    <a href="/profile/admin/delete_user/{{ u.id }}" onclick="return confirm('{{ 'user.deleting'|trans }}')">
                        <div class="btn btn-custom hvr-grow">
                            <span class="glyphicon glyphicon-trash"></span>
                        </div>
                    </a>
                </td>
            {% endif %}
            </tr>
    {% endfor %}
</div>

控制器操作

/**
 * @Route("/profile/ajax/{id}", name="lock_ajax")
 */
public function ajaxLock($id, Request $request)
{
    $entityManager = $this->getDoctrine()->getManager();
    $user = $entityManager->getRepository('AppBundle:User')->find($id);
    if($user->isLocked()){
        $user->setLocked(false);
    }else{
        $user->setLocked(true);
    }
    $entityManager->persist($user);
    $entityManager->flush();
    $result = array();
    $result[0] = $user;
    return new JsonResponse($result);
}

多亏了Artamiel,我终于明白了!以下是我的案例:

1) 我已将data-id="{{ u.id }}"属性添加到我的按钮中。现在我可以访问我的实体ID。

2) 我修改了一点我的脚本:

$(document).ready(function(){
        $(".LockUserButton").click(function(){
            //get user id from twig layout
            var user = $(this).data("id");
            //ajax request
            $.ajax({
                type: 'POST',
                url: "/profile/ajax/"+user,
                data: { user_id: "user" },
                dataType: 'json',
                context: this,
                success: function () {
                    //i've wrote some function to toggle html
                    $(this).toggleText('{{ 'user.unlocked'|trans }}', '{{ 'user.locked'|trans }}');
                    //find table row and toggle classes when button clicked and request ok
                    $(this).closest("tr").toggleClass('locked progress-bar-striped');
                },
                error: function(){
                    alert("Error!");
                }
            });
        });
    });